-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy paths3.js
64 lines (52 loc) · 1.43 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
53
54
55
56
57
58
59
60
61
62
63
64
require("dotenv").config();
const fs = require("fs");
const S3 = require("aws-sdk/clients/s3");
const bucketName = process.env.AWS_BUCKET_NAME;
const region = process.env.AWS_BUCKET_REGION;
const accessKeyId = process.env.AWS_ACCESS_KEY;
const secretAccessKey = process.env.AWS_SECRET_KEY;
const s3 = new S3({
region,
accessKeyId,
secretAccessKey,
});
function uploadFile(path, file, largeTitle) {
const fileStream = fs.createReadStream(path);
const fileKey = `${file.filename}${largeTitle}`;
console.log(fileKey);
const uploadParams = {
Bucket: bucketName,
Body: fileStream,
Key: fileKey,
};
return s3.upload(uploadParams).promise();
}
exports.uploadFile = uploadFile;
function uploadGif(path, fileName) {
const fileStream = fs.createReadStream(path);
const uploadParams = {
Bucket: bucketName,
Body: fileStream,
Key: fileName,
};
return s3.upload(uploadParams).promise();
}
exports.uploadGif = uploadGif;
function getFileStream(fileKey) {
const downloadParams = {
Key: fileKey,
Bucket: bucketName,
};
return s3.getObject(downloadParams).createReadStream();
}
exports.getFileStream = getFileStream;
function deleteFiles(id) {
const deleteParams = {
Bucket: bucketName,
Delete: {
Objects: [{ Key: id }],
},
};
return s3.deleteObjects(deleteParams).promise();
}
exports.deleteFiles = deleteFiles;