dimah-s3v1.5.5

Setup

Install `@dimah-s3/server`, configure S3, and mount your route handler.

Install

npm i @dimah-s3/server @aws-sdk/client-s3

Configure S3 & routes

Initialize dimahS3 by passing your @aws-sdk/client-s3 instance and defining named routes.

Operations (upload, download, delete, multipart) are disabled by default. Enable only what each route needs.

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,
  endpoint: process.env.S3_ENDPOINT,
  credentials: {
    accessKeyId: process.env.S3_ACCESS_KEY_ID!,
    secretAccessKey: process.env.S3_SECRET_ACCESS_KEY!,
  },
});

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

Mount route handler

Adapters are available for all major JavaScript runtimes:

app/api/s3/[...s3]/route.ts
import { toNextJsHandler } from "@dimah-s3/server/next";
import { s3 } from "@/lib/s3";

export const { GET, POST, PUT, PATCH, DELETE } = toNextJsHandler(s3);

Server-side API (s3.api)

You can invoke S3 actions directly on the server without going through HTTP endpoints. Pass request headers so that route guards and auth hooks execute properly:

app/actions.ts
"use server";

import { headers } from "next/headers";
import { s3 } from "@/lib/s3";

export async function getAvatarDownloadUrl(key: string) {
  return s3.api.download({
    query: { route: "avatar", key },
    headers: await headers(),
  });
}

To upload files directly from the server (e.g. background jobs, image processing) while still executing route validation and onConfirmed hooks:

await s3.put({
  route: "avatar",
  fileName: "profile.png",
  contentType: "image/png",
  body: imageBuffer,
  headers: await headers(),
});

Type reference

import type { DimahS3Config, DimahS3Logger } from "@dimah-s3/server";

DimahS3Config

Prop

Type

Frequently asked questions

On this page