Download Hooks
Authorize and sign GET download URLs or proxy streams.
Enable download operations with download: true or a DownloadConfig object on a named route.
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({
download: {
disposition: "inline",
guard: async ({ key, request }) => {
const session = await getSession(request);
if (!session) throw errors.unauthorized();
},
},
}),
},
});Download lifecycle flow
- download.resolvehook
- download.guardhook
- onPresignedhook
- Fetch from S3S3
resolve: Optionally rewrites downloadfileName,disposition(inlinevsattachment), orexpiresIn.guard: Validates request permissions for the specifiedkey.onPresigned: Receives the signed URL and expiration details.
Type reference
import type {
DownloadGuardContext,
DownloadOnPresignedContext,
DownloadResolveInfo,
} from "@dimah-s3/server";DownloadGuardContext
Prop
Type
DownloadOnPresignedContext
Prop
Type
DownloadResolveInfo
Prop
Type
Frequently asked questions
Use download.resolve to set the downloaded file name per request:
avatar: route({
download: {
resolve: async ({ key, request }) => {
const user = await getUserByKey(key);
return {
fileName: `${user.username}-avatar.png`,
disposition: "attachment",
};
},
},
});Set mode: "proxy". The client receives a same-origin URL (/api/s3/file?key=...) and your server streams the response:
avatar: route({
download: {
mode: "proxy",
},
});