-
Notifications
You must be signed in to change notification settings - Fork 0
/
s3.js
43 lines (38 loc) · 1.07 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
const aws = require("aws-sdk");
const fs = require("fs");
let secrets;
if (process.env.NODE_ENV == "production") {
secrets = process.env;
} else {
secrets = require("./secrets.json");
}
const s3 = new aws.S3({
accessKeyId: secrets.AWS_ID,
secretAccessKey: secrets.AWS_SECRET,
});
exports.upload = (req, res, next) => {
if (!req.files) {
return res.sendStatus(500);
}
req.files.map((element) => {
var promise = s3
.putObject({
Bucket: "myprivatecook",
ACL: "public-read",
Key: element.filename,
Body: fs.createReadStream(element.path),
ContentType: element.mimetype,
ContentLength: element.size,
})
.promise();
promise
.then(() => {
next();
fs.unlink(element.path, () => {});
})
.catch((err) => {
res.sendStatus(500);
console.log(err);
});
});
};