3

I'm new with serverside programming with node.js. I'm sticking together a tiny webapp with it right now and having the usual startup learning to do. The following piece of code WORKS. But I would love to know if it's more or less a right way to do a simple file upload from a form and throw it into aws s3:

app.router.post('/form', { stream: true }, function () {

    var req = this.req,
        res = this.res,
        form = new formidable.IncomingForm();

    form
    .parse(req, function(err, fields, files) {
        console.log('Parsed file upload' + err);
        if (err) {
            res.end('error: Upload failed: ' + err);
        } else {
            var img = fs.readFileSync(files.image.path);
            var data = {
                Bucket: 'le-bucket',
                Key:    files.image.name,
                Body:   img
            };
            s3.client.putObject(data, function() {
                console.log("Successfully uploaded data to myBucket/myKey");
            });
            res.end('success: Uploaded file(s)');
        }
    });
});

Note: I had to turn buffer off in union / flatiron.plugins.http.

What I would like to learn is, when to stream load a file and when to syncload it. It will be a really tiny webapp with little traffic.

If it's more or less good then please consider this as a token of working code which I also would throw into a gist. It's not that easy to find documenation and working examples of this kind of stuff. I like flatiron alot. But it's small module approach leads to lots of splattered docs and examples all over the net, speak alone of tutorials.

thgie
  • 2,367
  • 1
  • 18
  • 28

1 Answers1

0

You should use other module than formidable because as far as I know formidable does not have s3 storage option , then you must save the files in your server before uploading it. I would recommend you to use : multiparty

Use this example in order to upload directly to S3 without saving it locally in your server.