rafidude
rafidude

Reputation: 4626

How to receive an uploaded file using node.js formidable library and save it to Amazon S3 using knox?

I would like to upload a form from a web page and directly save the file to S3 without first saving it to disk. This node.js app will be deployed to Heroku, where there is no local disk to save the file to.

The node-formidable library provides a great way to upload files and save them to disk. I am not sure how to turn off formidable (or connect-form) from saving file first. The Knox library on the other hand provides a way to read a file from the disk and save it on Amazon S3.

1) Is there a way to hook into formidable's events (on Data) to send the stream to Knox's events, so that I can directly save the uploaded file in my Amazon S3 bucket? 2) Are there any libraries or code snippets that can allow me to directly take the uploaded file and save it Amazon S3 using node.js?

There is a similar question here but the answers there do not address NOT saving the file to disk.

Upvotes: 2

Views: 3533

Answers (2)

chilts
chilts

Reputation: 451

this is Andy, creator of AwsSum:

I just released v0.2.0 of this library. It uploads the files that were created by Express' bodyParser() though as you say, this won't work on Heroku:

However, I shall be looking at adding the ability to stream from formidable directly to S3 in the next (v0.3.0) version. For the moment though, take a look and see if it can help. :)

Upvotes: 0

rafidude
rafidude

Reputation: 4626

It looks like there is no good way to do it. One reason might be that the node-formidable library saves the uploaded file to disk. I could not find any options to do otherwise. The knox library takes the saved file on the disk and using your Amazon S3 credentials uploads it to Amazon.

Since on Heroku I cannot save files locally, I ended up using transloadit service. Though their authentication docs have some learning curve, I found the service useful.

For those who want to use transloadit using node.js, the following code sample may help (transloadit page had only Ruby and PHP examples)

var crypto, signature;
crypto = require('crypto');
signature = crypto.createHmac("sha1", 'auth secret').
    update('some string').
    digest("hex")
console.log(signature);

Upvotes: 3

Related Questions