Add file handlers, s3 support

This commit is contained in:
2024-12-27 12:46:28 +05:30
parent 88046d6810
commit 40b2ab54f0
11 changed files with 1533 additions and 0 deletions

View File

@@ -10,6 +10,7 @@ export type Claim =
| "permit:delete"
| "file:upload"
| "file:download"
| "file:delete"
| "token:read"
| "token:write"
| "token:delete";

46
src/utils/s3.ts Normal file
View File

@@ -0,0 +1,46 @@
import {
S3Client,
PutObjectCommand,
GetObjectCommand,
DeleteObjectCommand,
} from "@aws-sdk/client-s3";
import { getSignedUrl } from "@aws-sdk/s3-request-presigner";
const BUCKET = process.env.BUCKET || "";
const client = new S3Client({
region: process.env.REGION || "",
credentials: {
accessKeyId: process.env.ACCESS_KEY_ID || "",
secretAccessKey: process.env.SECRET_ACCESS_KEY || "",
},
});
export async function uploadFileS3(key: string, body: Buffer) {
const command = new PutObjectCommand({
Body: body,
Bucket: BUCKET,
Key: key,
});
const response = await client.send(command);
}
export async function getFileUrlS3(key: string, name: string) {
const command = new GetObjectCommand({
Bucket: BUCKET,
Key: key,
ResponseContentDisposition: `attachment; filename=${name}`,
});
return await getSignedUrl(client, command, { expiresIn: 300 });
}
export async function deleteFileS3(key: string) {
const command = new DeleteObjectCommand({
Bucket: BUCKET,
Key: key,
});
return await client.send(command);
}