-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy paths3.js
40 lines (36 loc) · 990 Bytes
/
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
const aws = require('aws-sdk');
const fs = require('fs');
const secrets =
process.env.NODE_ENV == 'production' ? process.env : require('./secrets');
const s3 = new aws.S3({
accessKeyId: secrets.AWS_KEY,
secretAccessKey: secrets.AWS_SECRET
});
exports.upload = (req, res, next) => {
if (!req.file) {
console.log('Multer did not work.');
res.sendStatus(500);
return;
}
const { filename, mimetype, size, path } = req.file;
const promise = s3
.putObject({
Bucket: 'ategev',
ACL: 'public-read',
Key: filename,
Body: fs.createReadStream(path),
ContentType: mimetype,
ContentLength: size
})
.promise()
.then(() => {
// it worked!!!
next();
fs.unlink(path, () => {});
})
.catch(err => {
// uh oh
console.log(err);
res.sendStatus(500);
});
};