Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

aws-sdk S3: best way to list all keys with listObjectsV2

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?

like image 880
eljefedelrodeodeljefe Avatar asked Feb 22 '17 14:02

eljefedelrodeodeljefe


2 Answers

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);
like image 119
Giovanni Bruno Avatar answered Sep 22 '22 19:09

Giovanni Bruno


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();
            } 

        }
    });
}
like image 43
notionquest Avatar answered Sep 20 '22 19:09

notionquest