Created
October 14, 2019 10:32
-
-
Save shrimp2t/06daee11820cc37ed916ed4a2ee5b6ac to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const streamingS3 = require('streaming-s3'); | |
const fs = require('fs'); | |
const path = require('path'); | |
const _ = require('lodash'); | |
const AWS = require('aws-sdk'); | |
require('dotenv').config(); | |
const mime = require('mime-types'); | |
const S3StreamUploader = (args) => { | |
let options = _.defaults(args, { | |
file: false, | |
done: (resp, stats) => {}, | |
error: (e) => {}, | |
name: false | |
}); | |
let fileInfo = path.parse(options.file); | |
var fStream = fs.createReadStream(options.file); | |
let contentType = mime.lookup(fileInfo.ext); | |
let saveFileName = ''; | |
if (options.name) { | |
saveFileName = name + fileInfo.ext; | |
} else { | |
saveFileName = fileInfo.base; | |
} | |
if (process.env.AWS_SUBFOLDER) { | |
saveFileName = process.env.AWS_SUBFOLDER + '/' + saveFileName; | |
} | |
var uploader = new streamingS3( | |
fStream, | |
{ | |
endpoint: process.env.AWS_END_POINT, | |
accessKeyId: process.env.AWS_KEY, | |
secretAccessKey: process.env.AWS_SECRET_KEY | |
}, | |
{ | |
Bucket: process.env.AWS_BUCKET, | |
Key: saveFileName, | |
ACL: process.env.AWS_ALC, | |
ContentType: contentType | |
} | |
); | |
uploader.on('data', function(bytesRead) { | |
// console.log(bytesRead, ' bytes read.'); | |
}); | |
uploader.on('part', function(number) { | |
// console.log('Part ', number, ' uploaded.'); | |
}); | |
// All parts uploaded, but upload not yet acknowledged. | |
uploader.on('uploaded', function(stats) {}); | |
uploader.on('finished', function(resp, stats) { | |
try { | |
options.done(resp, stats); | |
} catch (e) {} | |
}); | |
uploader.on('error', function(e) { | |
try { | |
options.error(e); | |
} catch (e) {} | |
}); | |
uploader.begin(); // important if callback not provided. | |
}; | |
module.exports = S3StreamUploader; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment