2

I'm quite new to node.js and would like to do the following:

  • user can upload one file
  • upload should be saved to amazon s3
  • file information should be saved to a database
  • script shouldn't be limited to specific file size

As I've never used S3 or done uploads before I might have some wrong ideas - please correct me, if I'm wrong.

So in my opinion the original file name should be saved into the db and returned for download but the file on S3 should be renamed to my database entry id to prevent overwriting files. Next, should the files be streamed or something? I've never done this but it just seems not to be smart to cache files on the server to then push them to S3, does it?

Thanks for your help!

1 Answer 1

5

At first I recommend to look at knox module for NodeJS. It is from quite reliable source. https://github.com/LearnBoost/knox

I write a code below for Express module, but if you do not use it or use another framework, you should still understand basics. Take a look at CAPS_CAPTIONS in the code, you want to change them according to your needs / configuration. Please also read comments to understand pieces of code.

app.post('/YOUR_REQUEST_PATH', function(req, res, next){
    var fs = require("fs")
    var knox = require("knox")
    var s3 = knox.createClient({
        key: 'YOUR PUBLIC KEY HERE' // take it from AWS S3 configuration
      , secret: 'YOUR SECRET KEY HERE' // take it from AWS S3 configuration
      , bucket: 'YOUR BUCKET' // create a bucket on AWS S3 and put the name here. Configure it to your needs beforehand. Allow to upload (in AWS management console) and possibly view/download. This can be made via bucket policies.
    })
    fs.readFile(req.files.NAME_OF_FILE_FIELD.path, function(err, buf){ // read file submitted from the form on the fly
        var s3req = s3.put("/ABSOLUTE/FOLDER/ON/BUCKET/FILE_NAME.EXTENSION", { // configure putting a file. Write an algorithm to name your file
              'Content-Length': buf.length
            , 'Content-Type': 'FILE_MIME_TYPE'
        })
        s3req.on('response', function(s3res){ // write code for response
            if (200 == s3res.statusCode) {
                // play with database here, use s3req and s3res variables here
            } else {
                // handle errors here
            }
        })
        s3req.end(buf) // execute uploading
    })
})
Sign up to request clarification or add additional context in comments.

1 Comment

Had to specify my region and now everything works fine! Thanks

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.