I'm in trouble with uploading file to Amazon S3 using node.js (hapi). Here's the code of my route:
{
method: 'POST',
path: '/upload',
config: {
auth: {
mode: 'optional',
},
payload:{
maxBytes: 209715200,
output:'stream',
parse: true
},
handler: function(request, reply) {
var filePath = path.join(__dirname, request.payload.file.hapi.filename);
fs.readFile(filePath, function(err, data) {
console.log(data);
const s3 = new AWS.S3();
s3.putObject({
Bucket: 'mybucket',
Key: request.payload.file.hapi.filename,
Body: data,
ACL: 'public-read'
}, function (err) {
if (err) { throw err; }
});
});
reply(request.payload.file);
}
}
}
What's wrong? My files uploads to the S3 cluster, but with 0 bytes size. What did I do wrong? Console.log for data returns <Buffer >
I don't know why. Can someone tell me how to fix it?