Reputation: 11
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?
Upvotes: 1
Views: 979
Reputation: 156
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).
Upvotes: 1
Reputation: 1411
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.
Upvotes: 0