Skip to content

Instantly share code, notes, and snippets.

Last active Sep 18, 2020
What would you like to do?
s3 upload as stream using req stream (extra info in request header)
const file = this.input.files[0];
var xhr = new XMLHttpRequest();
xhr.addEventListener('load', (e) => {
});'POST', host + 'fileuploadstream', true);
xhr.setRequestHeader('body', JSON.stringify({ id: 'somebucketfolderid', fn: }));
const fileUploadStream = (req, res) => {
//get "body" args from header
const { id, fn } = JSON.parse(req.get('body'));
const Key = id + '/' + fn; //upload to s3 folder "id" with filename === fn
const params = {
Bucket: bucketName, //set somewhere
Body: req, //req is a stream
s3.upload(params, (err, data) => {
if (err) {
res.send('Error Uploading Data: ' + JSON.stringify(err) + '\n' + JSON.stringify(err.stack));
} else {
Copy link

spsneo commented Jun 27, 2017

This does not work properly with express. Am I missing something?

Copy link

parthdesai93 commented Mar 16, 2018

@spsneo are you using express bodyParser? using that might remove req.files

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