-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy paths3.js
73 lines (65 loc) · 1.87 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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
const aws = require("aws-sdk");
const fs = require("fs");
let secrets;
if (process.env.NODE_ENV == "production") {
secrets = process.env; // in prod the secrets are environment variables
} else {
secrets = require("./secrets"); // in dev they are in secrets.json which is listed in .gitignore
}
const s3 = new aws.S3({
accessKeyId: secrets.AWS_KEY,
secretAccessKey: secrets.AWS_SECRET
});
exports.upload = (req, res, next) => {
if (!req.file) {
console.log("no req.file");
return res.sendStatus(500);
}
const { filename, mimetype, size, path } = req.file;
const promise = s3
.putObject({
Bucket: "lucy-msg-socialnet",
ACL: "public-read",
Key: filename,
Body: fs.createReadStream(path),
ContentType: mimetype,
ContentLength: size
})
.promise(); //sets whole putObject block to be a promise
promise
.then(() => {
console.log("s3.js putObject worked");
next();
fs.unlink(path, () => {});
})
.catch(err => {
console.log("s3.js: error in upload putObject", err);
res.sendStatus(500);
});
};
exports.delete = (picArray, next) => {
if (!picArray) {
console.log("no pics");
return res.sendStatus(500);
}
//removed from here
const promise = s3
.deleteObjects({
Bucket: "lucy-msg-socialnet",
Delete: {
Objects: picArray,
Quiet: false
}
})
.promise();
promise
.then(data => {
console.log("data in s3.js", data);
console.log("s3.js deleteObject fired");
next();
})
.catch(err => {
console.log("s3.js: error in delete object", err);
res.sendStatus(500);
});
};