With the v1 version of the listObjects
API call, you would have done something like from this SO answer.
var allKeys = [];
function listAllKeys(marker, cb) {
s3.listObjects({Bucket: s3bucket, Marker: marker}, function(err, data){
allKeys.push(data.Contents);
if(data.IsTruncated)
listAllKeys(data.NextMarker, cb);
else
cb();
});
}
What would be the equivalent of the listObjectsV2
function?
this is the best way to do that in my opinion:
const AWS = require('aws-sdk');
const s3 = new AWS.S3();
const listAllKeys = (params, out = []) => new Promise((resolve, reject) => {
s3.listObjectsV2(params).promise()
.then(({Contents, IsTruncated, NextContinuationToken}) => {
out.push(...Contents);
!IsTruncated ? resolve(out) : resolve(listAllKeys(Object.assign(params, {ContinuationToken: NextContinuationToken}), out));
})
.catch(reject);
});
listAllKeys({Bucket: 'bucket-name'})
.then(console.log)
.catch(console.log);
Here is the code to get the list of keys from a bucket.
var params = {
Bucket: 'bucket-name'
};
var allKeys = [];
listAllKeys();
function listAllKeys() {
s3.listObjectsV2(params, function (err, data) {
if (err) {
console.log(err, err.stack); // an error occurred
} else {
var contents = data.Contents;
contents.forEach(function (content) {
allKeys.push(content.Key);
});
if (data.IsTruncated) {
params.ContinuationToken = data.NextContinuationToken;
console.log("get further list...");
listAllKeys();
}
}
});
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With