-
Notifications
You must be signed in to change notification settings - Fork 1
/
aws.js
45 lines (37 loc) · 834 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
34
35
36
37
38
39
40
41
42
43
44
const AWS = require('aws-sdk');
let s3 = false;
try {
s3 = new AWS.S3({
accessKeyId: process.env.AWS_ACCESS_ID,
secretAccessKey: process.env.AWS_SECRET,
});
} catch(e) {
console.log("Couldn't load credentials");
}
module.exports = {
getObject: (fileName, cb) => {
if (!s3) {
return cb(false, false);
}
s3.getObject({
Bucket: process.env.AWS_STORAGE_BUCKET_NAME,
Key: fileName,
}, cb)
},
uploadFile: (fileName, fileContent, cb) => {
if (!s3) return cb();
const params = {
Bucket: process.env.AWS_STORAGE_BUCKET_NAME,
Key: fileName,
ContentType: 'image/png',
Body: fileContent,
};
// Uploading files to the bucket
s3.upload(params, function(err, data) {
if (err) {
throw err;
}
cb(data);
});
},
};