-
Notifications
You must be signed in to change notification settings - Fork 0
/
s3Service.js
36 lines (27 loc) · 891 Bytes
/
s3Service.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
const { S3Client, PutObjectCommand } = require("@aws-sdk/client-s3");
const uuid = require("uuid").v4;
exports.s3Uploadv3 = async (files) => {
const s3client = new S3Client();
const urls = [];
const uploadPromises = files.map(async (file) => {
const originalKey = `uploads/${uuid()}-${file.originalname}`;
// Replace spaces with '+'
const modifiedKey = originalKey.replace(/ /g, "+");
// console.log(
// "Generated Key:",
// `https://nafasi.s3.amazonaws.com/${modifiedKey}`
// );
const params = {
Bucket: process.env.AWS_BUCKET_NAME,
Key: originalKey,
Body: file.buffer,
};
await s3client.send(new PutObjectCommand(params));
urls.push({
originalKey,
url: `https://${process.env.AWS_BUCKET_NAME}.s3.amazonaws.com/${modifiedKey}`,
});
});
await Promise.all(uploadPromises);
return urls;
};