1

I'm creating a portal where users can select and upload single files from their PC to S3 on AWS.

Below is my server.js code:

app.post('/submit_doc', function(req, res){

var FileName = req.body.fileName,
Filedescription = req.body.filediscrip,
InputFileName = req.body.inputfile;
AWS.config.region = 'eu-west-1';
var fileStream = fs.createReadStream(FileName);
fileStream.on('error', function (err) {
    if (err){
        console.log("Error reading file: ", err);
        res.send(500);
    }
    else{
        fileStream.on('open', function () {
            var s3 = new AWS.S3();
            s3.putObject({
                Bucket: 'exampleassetcare.com',
                Key: 'reports/'+FileName,
                Body: fileStream
             }, function (err) {
             if (err) { 
                 console.log("Error uploading data: ", err);
                 res.send(500);
             }
         });
     });

I get the error: No such file or directory.

Can someone please help?

NLV
  • 11
  • 4

2 Answers2

1

If I'm understanding you correctly, this code you've posted is running on the server. But the inputs are provided by the client, yes? If so, your server would be trying to find a file locally, based on a file path that the client gave you... So the file won't exist...

If I was a malicious user and I told your server to upload a file path /etc/passwd, your server would go and expose the hashed passwords (assuming it was a Linux system, and assuming there were proper permissions, etc... But you get the idea).

pyepye
  • 156
  • 1
  • 6
  • Yes, the code is in the server while the inputs are from the client. How to handle this upload then? – NLV Mar 10 '16 at 05:22
  • Are you using Express? If you are, take a look at http://stackoverflow.com/questions/23114374/file-uploading-with-express-4-0-req-files-undefined. In short, you need an HTML form on the client side that will POST data to the server. In that data is the file contents which your server can either buffer locally then upload to S3, or alternatively, just stream directly to S3. Make sense? – pyepye Mar 10 '16 at 20:17
  • Yes, that's helpful.I need to install some middlewares to handle the file upload from client (HTML with POST method) to S3 (Nodejs on Express). – NLV Mar 11 '16 at 03:32
  • Yes that sounds about right. I'd approach it in steps: (1) get your HTML form to POST request to your Node.js server; (2) spit out the bytes received during your `on("data")` event; (3) upload a small file in its entirety to S3; (4) change to a streaming approach to handle large files.... Though if you are only uploading files the are small, like up to a MB text files, on a low request server, you'll probably get away with just buffering the data on your server and uploading to S3 in one go. – pyepye Mar 11 '16 at 20:44
  • Thanks for further explanation. In the meantime, I found out there is upload function in AWS SDK for browser. That is exactly what I wanted, but on the server side. Thanks pyepe for the clarifications. – NLV Mar 13 '16 at 20:55
0

change it to

var FileName = req.body.fileName,
Filedescription = req.body.filediscrip,
InputFileName = req.body.inputfile;
AWS.config.region = 'eu-west-1';
console.log(FileName)
var fileStream = fs.createReadStream(FileName);

and check that your file exists, looks like something wrong with path to file.

alexey
  • 1,381
  • 9
  • 19
  • FileName is being displayed correctly. As pyepye mentioned, I'm trying to upload a local file to S3. So the server should access the local file path. – NLV Mar 10 '16 at 05:20