I have an API method that when called and passed a string of file keys, converts that to an array, and downloads them from S3. However when I call this API, I get the error Error: ENOENT: no such file or directory, open <filename here>
on the server.
This is my api:
reports.get('/xxx/:fileName', async (req, res) => {
var AWS = require('aws-sdk');
var s3 = new AWS.S3();
var str_array = req.params.fileName.split(',');
for (var i = 0; i < str_array.length; i++) {
var filename = str_array[i].trim();
localFileName = './' + filename;
let file = fs.createWriteStream(localFileName);
s3.getObject({
Bucket: config.reportBucket,
Key: filename
})
.on('error', function (err) {
res.end("File download failed with error " + err.message);
})
.on('httpData', function (chunk) {
file.write(chunk);
})
.on('httpDone', function () {
file.end();
})
.send();
}
res.end("Files have been downloaded successfully")
});
How can I successfully download multiple files from S3 by passing my array of keys?