-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathawsS3.js
100 lines (84 loc) · 2.72 KB
/
awsS3.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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
const AWS = require("aws-sdk");
// name of your bucket here
const NAME_OF_BUCKET = "aws-s3-pern-demo";
const multer = require("multer");
// make sure to set environment variables in production for:
// AWS_ACCESS_KEY_ID
// AWS_SECRET_ACCESS_KEY
// and aws will automatically use those environment variables
const s3 = new AWS.S3({ apiVersion: "2006-03-01" });
// --------------------------- Public UPLOAD ------------------------
const singlePublicFileUpload = async (file) => {
const { originalname, mimetype, buffer } = await file;
const path = require("path");
// name of the file in your S3 bucket will be the date in ms plus the extension name
const Key = new Date().getTime().toString() + path.extname(originalname);
const uploadParams = {
Bucket: NAME_OF_BUCKET,
Key,
Body: buffer,
ACL: "public-read",
};
const result = await s3.upload(uploadParams).promise();
// save the name of the file in your bucket as the key in your database to retrieve for later
return result.Location;
};
const multiplePublicFileUpload = async (files) => {
return await Promise.all(
files.map((file) => {
return singlePublicFileUpload(file);
})
);
};
// --------------------------- Prviate UPLOAD ------------------------
const singlePrivateFileUpload = async (file) => {
const { originalname, mimetype, buffer } = await file;
const path = require("path");
// name of the file in your S3 bucket will be the date in ms plus the extension name
const Key = new Date().getTime().toString() + path.extname(originalname);
const uploadParams = {
Bucket: NAME_OF_BUCKET,
Key,
Body: buffer,
};
const result = await s3.upload(uploadParams).promise();
// save the name of the file in your bucket as the key in your database to retrieve for later
return result.Key;
};
const multiplePrivateFileUpload = async (files) => {
return await Promise.all(
files.map((file) => {
return singlePrivateFileUpload(file);
})
);
};
const retrievePrivateFile = (key) => {
let fileUrl;
if (key) {
fileUrl = s3.getSignedUrl("getObject", {
Bucket: NAME_OF_BUCKET,
Key: key,
});
}
return fileUrl || key;
};
// --------------------------- Storage ------------------------
const storage = multer.memoryStorage({
destination: function (req, file, callback) {
callback(null, "");
},
});
const singleMulterUpload = (nameOfKey) =>
multer({ storage: storage }).single(nameOfKey);
const multipleMulterUpload = (nameOfKey) =>
multer({ storage: storage }).array(nameOfKey);
module.exports = {
s3,
singlePublicFileUpload,
multiplePublicFileUpload,
singlePrivateFileUpload,
multiplePrivateFileUpload,
retrievePrivateFile,
singleMulterUpload,
multipleMulterUpload,
};