47 lines
1.1 KiB
TypeScript
47 lines
1.1 KiB
TypeScript
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);
|
|
}
|