Skip to content

Instantly share code, notes, and snippets.

@schempy
Last active March 11, 2024 10:20
Show Gist options
  • Star 26 You must be signed in to star a gist
  • Fork 5 You must be signed in to fork a gist
  • Save schempy/87567e11633f8ef11c8e to your computer and use it in GitHub Desktop.
Save schempy/87567e11633f8ef11c8e to your computer and use it in GitHub Desktop.
Streaming File Uploads To Amazon S3 With Node.js
var http = require('http');
var router = require('routes')();
var Busboy = require('busboy');
var AWS = require('aws-sdk');
var inspect = require('util').inspect;
var port = 5000;
// Define s3-upload-stream with S3 credentials.
var s3Stream = require('s3-upload-stream')(new AWS.S3({
accessKeyId: '',
secretAccessKey: ''
}));
// Handle uploading file to Amazon S3.
// Uses the multipart file upload API.
function uploadS3 (readStream, key, callback) {
var upload = s3Stream.upload({
'Bucket': '',
'Key': key
});
// Handle errors.
upload.on('error', function (err) {
callback(err);
});
// Handle progress.
upload.on('part', function (details) {
console.log(inspect(details));
});
// Handle upload completion.
upload.on('uploaded', function (details) {
callback();
});
// Pipe the Readable stream to the s3-upload-stream module.
readStream.pipe(upload);
}
// Define our route for uploading files
router.addRoute('/images', function (req, res, params) {
if (req.method === 'POST') {
// Create an Busyboy instance passing the HTTP Request headers.
var busboy = new Busboy({ headers: req.headers });
// Listen for event when Busboy finds a file to stream.
busboy.on('file', function (fieldname, file, filename, encoding, mimetype) {
// Handle uploading file to Amazon S3.
// We are passing 'file' which is a ReadableStream,
// 'filename' which is the name of the file
// and a callback function to handle success/error.
uploadS3(file, filename, function (err) {
if (err) {
res.statusCode = 500;
res.end(err);
} else {
res.statusCode = 200;
res.end('ok');
}
});
});
// Pipe the HTTP Request into Busboy.
req.pipe(busboy);
}
});
var server = http.createServer(function (req, res) {
// Check if the HTTP Request URL matches on of our routes.
var match = router.match(req.url);
// We have a match!
if (match) match.fn(req, res, match.params);
});
server.listen(port, function () {
console.log('Listening on port ' + port);
});
@elvinaze
Copy link

can you let me know sample curl command to test this code please?

@skubot
Copy link

skubot commented Apr 26, 2017

@elvinaze, something like this will do it...
curl --form "picture[uploaded_data]=@image.png;type=image/png" localhost:5000/images

@ChrisGrigg
Copy link

Better to use s3.upload() nowadays.

@brpapa
Copy link

brpapa commented Oct 10, 2021

If two files are loaded, the request will end when the first one was uploaded

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment