-
Notifications
You must be signed in to change notification settings - Fork 1
/
s3.js
52 lines (41 loc) · 1.22 KB
/
s3.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
const { S3Client, PutObjectCommand, DeleteObjectCommand, GetObjectCommand } = require("@aws-sdk/client-s3");
const { getSignedUrl } = require("@aws-sdk/s3-request-presigner");
require('dotenv').config()
const bucketName = process.env.AWS_BUCKET_NAME
const bucketRegion = process.env.AWS_REGION
const accessKey = process.env.AWS_ACCESS_KEY_ID
const secretKey = process.env.AWS_SECRET_ACCESS_KEY
const s3Client = new S3Client({
region: bucketRegion,
credentials: {
accessKeyId: accessKey,
secretAccessKey: secretKey
}
})
function uploadFile(fileBuffer, fileName, mimetype) {
const uploadParams = {
Bucket: bucketName,
Body: fileBuffer,
Key: fileName,
ContentType: mimetype
}
return s3Client.send(new PutObjectCommand(uploadParams));
}
function deleteFile(fileName) {
const deleteParams = {
Bucket: bucketName,
Key: fileName,
}
return s3Client.send(new DeleteObjectCommand(deleteParams));
}
async function getObjectSignedUrl(key) {
const params = {
Bucket: bucketName,
Key: key
}
const command = new GetObjectCommand(params);
const seconds = 604800
const url = await getSignedUrl(s3Client, command, { expiresIn: seconds });
return url
}
module.exports = { uploadFile, deleteFile, getObjectSignedUrl };