dimah-s3v1.5.5

Upload Hooks

Presign, authorize, confirm, and persist verified upload metadata.

Enable upload operations on a named route with upload: true or an UploadConfig object.

lib/s3.ts
import { S3Client } from "@aws-sdk/client-s3";
import { dimahS3, errors, route } from "@dimah-s3/server";

export const awsS3 = new S3Client({/* env */});

export const s3 = dimahS3({
  client: awsS3,
  bucket: process.env.S3_BUCKET!,
  routes: {
    avatar: route({
      upload: {
        fileTypes: ["image/*"],
        maxFileSize: 2 * 1024 * 1024,
        guard: async ({ request }) => {
          const session = await getSession(request);
          if (!session) throw errors.unauthorized();
        },
        onConfirmed: async ({ key, contentLength, request }) => {
          const session = await getSession(request);
          await db.user.update({
            where: { id: session.userId },
            data: { avatarKey: key, avatarSize: contentLength },
          });
        },
      },
    }),
  },
});

Upload lifecycle flow

Upload lifecycle
  1. route.guardhook
  2. objecthook
  3. upload.guardhook
  4. onPresignedhook
  5. Upload to S3S3
  6. confirmGuardhook
  7. HeadObjectserver
  8. onConfirmedhook
  1. guard: Evaluates session and permissions before key generation.
  2. object: Computes destination folder, key, metadata, ACL, storage class, cache control, or tags.
  3. onPresigned: Fires after the presigned URL is created.
  4. confirmGuard: Authorizes the confirmation request after bytes land in S3.
  5. onConfirmed: Reads verified metadata from HeadObject and persists the record.

Type reference

import type {
  UploadGuardContext,
  UploadOnConfirmedContext,
  UploadConfirmGuardContext,
  UploadObjectContext,
  UploadObjectInfo,
  UploadOnPresignedContext,
} from "@dimah-s3/server";

UploadGuardContext

Runs after upload.object assigns the key (and after global / route guards).

Prop

Type


UploadOnConfirmedContext

Runs after HeadObject validates the uploaded file size and content type.

Prop

Type


UploadConfirmGuardContext

Runs before HeadObject when confirming single-shot uploads or completing multipart uploads.

Prop

Type


UploadObjectContext

Input passed to upload.object to generate custom destination keys and metadata.

Prop

Type

Frequently asked questions

On this page