Im trying to download a bucket from S3
async function downloadBucketRaw(buckt, prefix) {
return s3.listObjectsV2({ Bucket: buckt, Prefix: prefix },
(err, obj) => {
if (err) return console.log(err);
var promises = []
obj.Contents.forEach(element => {
if (element.Size > 0) {
var p = downloadFileFromS3({ Bucket: buckt, Key: element.Key });
promises.push(p)
}
});
return promises
});
}
}
async function downloadFileFromS3(item) {
var file = s3.getObject(item,
(err, data) => {
if (err) throw err;
});
file.createReadStream()
.pipe(fs.createWriteStream(item));
return file.promise()
}
In order to wait for all the downloads to happen I want to do something like:
Promise.all(downloadBucketRaw(buckt, prefix))
But it is not working, I am not sure about how to do a wait of a promise returning a list of promises
2
Answers
The issue in your code is that you’re not properly handling promises in the downloadBucketRaw function. The s3.listObjectsV2 method does not return a promise, so you can’t directly use async/await with it.
To fix this, you can wrap the callback-based function with a Promise and resolve it once the callback is called. Here’s an updated version of your code:
You can rewrite your code like below
The
s3.listObjectsV2()
method returns anAWS.Request
, which provides a.promise()
method, which turns the Node callback style into a then-able Promise:Note that if you were to return the Array of Promises directly, you would have to use it like: