2.0 beta — new releases can include breaking changes. Stable 1.x docs
dimah-s3v2.0.0-beta.0
Providers

Amazon S3

Configuration, CORS, and bucket policies for AWS S3.

Amazon S3 supports presigned PUT and POST uploads, object ACLs, SHA-256 checksums, and multipart uploads.

Bucket CORS configuration

Because the browser uploads directly to your S3 bucket, configure a bucket CORS policy:

[
  {
    "AllowedOrigins": ["https://your-app.example", "http://localhost:3000"],
    "AllowedMethods": ["GET", "PUT", "POST", "HEAD"],
    "AllowedHeaders": ["*"],
    "ExposeHeaders": ["ETag", "Content-Type"],
    "MaxAgeSeconds": 3000
  }
]

Server configuration

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

export const awsS3 = new S3Client({
  region: process.env.S3_REGION!,
  credentials: {
    accessKeyId: process.env.S3_ACCESS_KEY_ID!,
    secretAccessKey: process.env.S3_SECRET_ACCESS_KEY!,
  },
  requestChecksumCalculation: "WHEN_REQUIRED", 
});

export const s3 = dimahS3({
  client: awsS3,
  bucket: process.env.S3_BUCKET!,
  routes: {
    avatar: route({
      upload: {
        fileTypes: ["image/*"],
        maxFileSize: 2 * 1024 * 1024,
      },
    }),
  },
});

AWS SDK versions from 3.729 default requestChecksumCalculation to "WHEN_SUPPORTED". Presigned browser uploads cannot match the checksum that mode adds, so dimah-s3 refuses to presign with such a client.

Frequently asked questions

On this page