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 ?
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.
If it helps anyone I was able to stream from the client to s3 successfully (without memory or disk storage):
https://gist.github.com/mattlockyer/532291b6194f6d9ca40cb82564db9d2a
The server endpoint assumes
req
is a stream object, I sent a File object from the client which modern browsers can send as binary data and added file info set in the headers.Yes putting the file info in the headers breaks convention but if you look at the gist it's much cleaner than anything else I found using streaming libraries or multer, busboy etc...
+1 for pragmatism and thanks to @SalehenRahman for his help.
I'm using the s3-upload-stream module in a working project here.
There is also some good examples from @raynos in his http-framework repository.
Alternatively you can look at - https://github.com/minio/minio-js. It has minimal set of abstracted API's implementing most commonly used S3 calls.
Here is an example of streaming upload.
putObject() here is a fully managed single function call for file sizes over 5MB it automatically does multipart internally. You can resume a failed upload as well and it will start from where its left off by verifying previously upload parts.
Additionally this library is also isomorphic, can be used in browsers as well.
You can now use streaming with the official Amazon SDK for nodejs and what's even more awesome, you finally can do so without knowing the file size in advance. Simply pass the stream as the
Body
: