Skip to content

Instantly share code, notes, and snippets.

@andrepadez
Forked from NeCkEr/gist:9132397
Last active July 11, 2023 20:12
Show Gist options
  • Save andrepadez/9132454 to your computer and use it in GitHub Desktop.
Save andrepadez/9132454 to your computer and use it in GitHub Desktop.
Streamed Upload to S3 using busboy
var Busboy = require('busboy'); //A streaming parser for HTML form data: https://github.com/mscdex/busboy
var generateId = require('time-uuid');
//** Handler to recive file uploads via stream
module.exports.boUpload = {
method: 'POST',
path: '/upload/',
config:{
payload: 'stream'
},
handler: function (request) {
busboy = new Busboy({ headers: request.raw.req.headers });
var infiles, outfiles, done;
busboy.on('file', function(fieldname, file, filename, encoding) {
++infiles;
filename = generateId();
filename = filename.replace(/\s+/g, '');
//onFileSaveToDisk(fieldname, file, filename, encoding, function() {
onFileSaveToS3(fieldname, file, filename, encoding, function() {
++outfiles;
if (done){
// ACTUAL EXIT CONDITION
console.log('All parts written to disk');
request.reply({ msg: filename}).type('text/html');
}
});
});
busboy.once('end', function() {
console.log('Done parsing form!');
done = true;
});
request.raw.req.pipe(busboy);
}
};
var pkgcloud = require('pkgcloud'); //a cloud API standard library: https://github.com/nodejitsu/pkgcloud
var s3client = pkgcloud.storage.createClient({
provider: 'amazon',
accessKey: '***',
accessKeyId: '***'
});
//** Function to save files to S3 cloud storage
function onFileSaveToS3(fieldname, file, filename, enconding, next){
file.pipe(
s3client.upload({
container: 'bla', //http://bla.s3.amazonaws.com/
remote: path.basename(filename)
}, function (err){
next();
console.log(err);
}
));
}
var fs = require('fs');
var path = require('path');
//** Function to save files to disk
function onFileSaveToDisk(fieldname, file, filename, encoding, next) {
// or save at some other location
var fstream = fs.createWriteStream(path.join('./upload', path.basename(filename)));
file.once('end', function() {
console.log(fieldname + '(' + filename + ') EOF');
});
fstream.once('close', function() {
console.log(fieldname + '(' + filename + ') written to disk');
next();
});
console.log(fieldname + '(' + filename + ') start saving');
file.pipe(fstream);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment