-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaws.js
33 lines (28 loc) · 786 Bytes
/
aws.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
const aws = require("aws-sdk");
const fs = require("fs");
const conf = require("./config");
const secrets =
process.env.NODE_ENV == "production"
? JSON.parse(process.env.SECRETS)
: require("./secrets");
const s3 = new aws.S3({
accessKeyId: secrets.AWS_KEY,
secretAccessKey: secrets.AWS_SECRET,
});
exports.upload = (req, res, next) => {
const { filename, mimetype, size, path } = req.file;
s3.putObject({
Bucket: conf.s3Bucket,
ACL: "public-read",
Key: filename,
Body: fs.createReadStream(path),
ContentType: mimetype,
ContentLength: size,
})
.promise()
.then(() => {
fs.unlink(path, () => {});
next();
})
.catch((err) => next(err));
};