Node.js & Amazon S3: How to iterate through all files in a bucket?

前端 未结 15 995
情书的邮戳
情书的邮戳 2020-12-02 14:17

Is there any Amazon S3 client library for Node.js that allows listing of all files in S3 bucket?

The most known aws2js and knox don\'t seem to have this functionalit

15条回答
  •  悲哀的现实
    2020-12-02 14:58

    Meekohi provided a very good answer, but the (new) documentation states that NextMarker can be undefined. When this is the case, you should use the last key as the marker.

    So his codesample can be changed into:

    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 || data.Contents[data.Contents.length-1].Key, cb);
        else
          cb();
      });
    }
    

    Couldn't comment on the original answer since I don't have the required reputation. Apologies for the bad mark-up btw.

提交回复
热议问题