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?

2 Answers 2

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).

Sign up to request clarification or add additional context in comments.

5 Comments

Yes, the code is in the server while the inputs are from the client. How to handle this upload then?
Are you using Express? If you are, take a look at stackoverflow.com/questions/23114374/…. 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?
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).
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.
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.
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.

1 Comment

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.

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.