-
Notifications
You must be signed in to change notification settings - Fork 0
/
s3.js
39 lines (33 loc) · 919 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
const knox = require("knox");
const fs = require("fs");
let secrets;
if (process.env.NODE_ENV == "production") {
secrets = process.env;
} else {
secrets = require("./secrets.json");
}
const client = knox.createClient({
key: secrets.AWS_KEY,
secret: secrets.AWS_SECRET,
bucket: "spicedling"
});
exports.upload = (req, res, next) => {
if (!req.file) {
return res.sendStatus(500);
}
const s3Request = client.put(req.file.filename, {
"Content-Type": req.file.mimetype,
"Content-Length": req.file.size,
"x-amz-acl": "public-read"
});
const readStream = fs.createReadStream(req.file.path);
readStream.pipe(s3Request);
s3Request.on("response", s3Response => {
const wasSuccessful = s3Response.statusCode == 200;
if (wasSuccessful) {
next();
} else {
res.sendStatus(500);
}
});
};