-
Notifications
You must be signed in to change notification settings - Fork 14
/
s3-util.js
38 lines (36 loc) · 1.03 KB
/
s3-util.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
/*global module, require, Promise, console */
const aws = require('aws-sdk'),
fs = require('fs'),
s3 = new aws.S3(),
downloadFileFromS3 = function (bucket, fileKey, filePath) {
'use strict';
console.log('downloading', bucket, fileKey, filePath);
return new Promise(function (resolve, reject) {
const file = fs.createWriteStream(filePath),
stream = s3.getObject({
Bucket: bucket,
Key: fileKey
}).createReadStream();
stream.on('error', reject);
file.on('error', reject);
file.on('finish', function () {
console.log('downloaded', bucket, fileKey);
resolve(filePath);
});
stream.pipe(file);
});
}, uploadFileToS3 = function (bucket, fileKey, filePath, contentType) {
'use strict';
console.log('uploading', bucket, fileKey, filePath);
return s3.upload({
Bucket: bucket,
Key: fileKey,
Body: fs.createReadStream(filePath),
ACL: 'private',
ContentType: contentType
}).promise();
};
module.exports = {
downloadFileFromS3: downloadFileFromS3,
uploadFileToS3: uploadFileToS3
};