Last active
March 11, 2024 10:20
-
-
Save schempy/87567e11633f8ef11c8e to your computer and use it in GitHub Desktop.
Streaming File Uploads To Amazon S3 With Node.js
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
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, something like this will do it...
curl --form "picture[uploaded_data][email protected];type=image/png" localhost:5000/images
Better to use s3.upload() nowadays.
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
can you let me know sample curl command to test this code please?