Tip: flat, formidable and aws s3
I'm new to server-side programming with node.js. Now I am going with it a tiny webapp and starting to learn. The following code snippet WORKS. But I would like to know if this is more or less the correct way to do a simple file upload from a form and throw it in 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 disable the buffer in union / flatiron.plugins.http.
What I would like to know is when to download the file and when to sync it. It will be a really tiny webapp with little traffic.
If this is more or less good, please consider it as a sign of working code, which I will also throw in the gist. It's not easy to find documentation and working examples of this sort of thing. I like flatiron alot. But the small modular approach results in a lot of splattering documents and examples all over the web, speaking only of tutorials.
source to share
You should use a different module than formidable, since formidable has no s3 storage option as far as I know, you must save the files to your server before loading it. I would recommend that you use: multiparty
Use this example to upload it directly to S3 without storing it locally on your server.
source to share