I have been trying to upload an image to s3 using multer and downloading it to a local path successfully using the below code.
routes.post('/updownload', upload.array('file'), function (req, res) {
var params = {
Bucket: "*******",
Key: "image1.jpeg"
};
var file = fs.createWriteStream('./uploads/file4.jpeg');
s3.getObject(params)
.createReadStream()
.on('error', function (err) {
console.log('Error reading file: ' + err.message);
file.end();
})
.pipe(file)
.on('error', function (err) {
err = true;
console.log('Error writing file:' + error.message);
file.end();
})
.on('finish', function (err) {
file.end();
if (!err) {
console.log('Successfully downloaded file from S3');
}
});
});
However, i am not sure, how do i send the response back to HTML with parameters like Headers, Body, Content-Type, status code and status message based on success/failure of image getting downloaded.
Kindly help and let me know if i am going in a correct path.