Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

S3 file upload stream using node js

I am trying to find some solution to stream file on amazon S3 using node js server with requirements:

  • Don't store temp file on server or in memory. But up-to some limit not complete file, buffering can be used for uploading.
  • No restriction on uploaded file size.
  • Don't freeze server till complete file upload because in case of heavy file upload other request's waiting time will unexpectedly increase.

I don't want to use direct file upload from browser because S3 credentials needs to share in that case. One more reason to upload file from node js server is that some authentication may also needs to apply before uploading file.

I tried to achieve this using node-multiparty. But it was not working as expecting. You can see my solution and issue at https://github.com/andrewrk/node-multiparty/issues/49. It works fine for small files but fails for file of size 15MB.

Any solution or alternative ?

like image 785
Janak Kansal Avatar asked Feb 09 '14 10:02

Janak Kansal


3 Answers

You can now use streaming with the official Amazon SDK for nodejs in the section "Uploading a File to an Amazon S3 Bucket" or see their example on GitHub.

What's even more awesome, you finally can do so without knowing the file size in advance. Simply pass the stream as the Body:

var fs = require('fs');
var zlib = require('zlib');

var body = fs.createReadStream('bigfile').pipe(zlib.createGzip());
var s3obj = new AWS.S3({params: {Bucket: 'myBucket', Key: 'myKey'}});
s3obj.upload({Body: body})
  .on('httpUploadProgress', function(evt) { console.log(evt); })
  .send(function(err, data) { console.log(err, data) });
like image 135
Johann Philipp Strathausen Avatar answered Oct 14 '22 11:10

Johann Philipp Strathausen


For your information, the v3 SDK were published with a dedicated module to handle that use case : https://www.npmjs.com/package/@aws-sdk/lib-storage

Took me a while to find it.

like image 6
nfroidure Avatar answered Oct 14 '22 12:10

nfroidure


Give https://www.npmjs.org/package/streaming-s3 a try.

I used it for uploading several big files in parallel (>500Mb), and it worked very well. It very configurable and also allows you to track uploading statistics. You not need to know total size of the object, and nothing is written on disk.

like image 2
Yaroslav Pogrebnyak Avatar answered Oct 14 '22 11:10

Yaroslav Pogrebnyak