-
Notifications
You must be signed in to change notification settings - Fork 0
/
s3.js
41 lines (36 loc) · 1.03 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
//CLIENT
console.log('LISTENING S3');
const fs = require('fs')
const knox = require('knox');
let secrets;
if (process.env.NODE_ENV == 'production') {
secrets = process.env;
} else {
secrets = require('./secrets.json');
}
const client = knox.createClient({
key: secrets.awsKey,
secret: secrets.awsSecret,
bucket: 'socialnetworknash'
});
module.exports.uploadToS3 = function(file) {
return new Promise(function(resolve, reject) {
const s3Request = client.put(file.filename, {
'Content-Type': file.mimetype,
'Content-Length': file.size,
'x-amz-acl': 'public-read'
});
const readStream = fs.createReadStream(file.path);
readStream.pipe(s3Request);
s3Request.on('response', s3Response => {
const wasSuccessful = s3Response.statusCode == 200;
if (wasSuccessful) {
resolve()
}
else {
console.log(s3Response.statusCode);
reject()
}
})
})
}