Last active
February 22, 2023 18:30
-
-
Save mattlockyer/532291b6194f6d9ca40cb82564db9d2a to your computer and use it in GitHub Desktop.
s3 upload as stream using req stream (extra info in request header)
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 file = this.input.files[0]; | |
//console.log(file); | |
var xhr = new XMLHttpRequest(); | |
xhr.addEventListener('load', (e) => { | |
console.log(e.target.response); | |
}); | |
xhr.open('POST', host + 'fileuploadstream', true); | |
xhr.setRequestHeader('body', JSON.stringify({ id: 'somebucketfolderid', fn: file.name })); | |
xhr.send(file); |
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 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 = { | |
Key, | |
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 { | |
res.send(Key); | |
} | |
}); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This does not work properly with express. Am I missing something?