Add file handlers, s3 support
This commit is contained in:
@@ -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
46
src/utils/s3.ts
Normal 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);
|
||||
}
|
||||
Reference in New Issue
Block a user