I'm a new developer in node.js and I want to write a little lambda function (in node.js) that upload a pdf file (from the hard drive) to an Amazon S3 bucket.
Here is my code :
// dependencies
var util = require('util');
var fs = require('fs');
exports.handler = function (req, res) {
var file = req.files.file;
fs.readFile("C:\\Users\\zack\\Downloads\\test.pdf", function (err, data) {
if (err) throw err; // Something went wrong!
var s3bucket = new AWS.S3({params: {Bucket: 'myBucket3'}});
s3bucket.createBucket(function () {
var params = {
Key: file.originalFilename, //file.name doesn't exist as a property
Body: data
};
s3bucket.upload(params, function (err, data) {
// Whether there is an error or not, delete the temp file
fs.unlink("C:\\Users\\zack\\Downloads\\test.pdf", function (err) {
if (err) {
console.error(err);
}
console.log('Temp File Delete');
});
console.log("PRINT FILE:", file);
if (err) {
console.log('ERROR MSG: ', err);
res.status(500).send(err);
} else {
console.log('Successfully uploaded data');
res.status(200).end();
}
});
});
});
};
The lambda function shows me this error message :
"errorMessage": "RequestId: 05024c81-f44b-11e6-a45e-57b13036ad96 Process exited before completing request"
And the cloudwatch :
START RequestId: e66a7653-f44b-11e6-8b1a-c511605a533b Version: $LATEST
2017-02-16T13:29:11.133Z e66a7653-f44b-11e6-8b1a-c511605a533b TypeError: Cannot read property 'file' of undefined
at exports.handler (/var/task/invoices3/invoices3.js:17:25)
END RequestId: e66a7653-f44b-11e6-8b1a-c511605a533b
REPORT RequestId: e66a7653-f44b-11e6-8b1a-c511605a533b Duration: 27.82 ms Billed Duration: 100 ms Memory Size: 1024 MB Max Memory Used: 23 MB
RequestId: e66a7653-f44b-11e6-8b1a-c511605a533b Process exited before completing request
Can you tell me where is the problem please?? I'm not good at node.js at all.
Thank you in advance !