-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy paths3.js
51 lines (45 loc) · 1.48 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
44
45
46
47
48
49
50
51
const aws = require("aws-sdk");
const fs = require("fs");
let secrets;
// only need if-block if you wanna deploy to heroku
if (process.env.NODE_ENV == "production") {
secrets = process.env; // in prod the secrets are environment variables
} else {
secrets = require("./utils/secrets"); // in dev they are in secrets.json which is listed in .gitignore
}
//creating aws client
const s3 = new aws.S3({
accessKeyId: secrets.AWS_KEY,
secretAccessKey: secrets.AWS_SECRET
});
exports.upload = (req, res, next) => {
// error handling (specify what to do if there's no file)
if (!req.file) {
console.log("no file");
return res.sendStatus(500);
}
const { filename, mimetype, size, path } = req.file;
const promise = s3
// putObject = for put requests
.putObject({
// information about the file that's gonna be uploaded
Bucket: "felix-bucket",
ACL: "public-read",
Key: filename,
Body: fs.createReadStream(path),
ContentType: mimetype,
ContentLength: size
})
.promise();
promise
.then(() => {
console.log("file upload successful!");
next();
// if you wanna delete the local copy of the file you've just uploaded
// fs.unlink(path, () => {});
})
.catch(err => {
console.log("err in put-object / file-upload", err);
res.sendStatus(500);
});
};