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

前端 未结 15 961
情书的邮戳
情书的邮戳 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 15:05

    Here's Node code I wrote to assemble the S3 objects from truncated lists.

    var params = {
        Bucket: ,
        Prefix: ,
    };
    
    var s3DataContents = [];    // Single array of all combined S3 data.Contents
    
    function s3Print() {
        if (program.al) {
            // --al: Print all objects
            console.log(JSON.stringify(s3DataContents, null, "    "));
        } else {
            // --b: Print key only, otherwise also print index 
            var i;
            for (i = 0; i < s3DataContents.length; i++) {
                var head = !program.b ? (i+1) + ': ' : '';
                console.log(head + s3DataContents[i].Key);
            }
        }
    }
    
    function s3ListObjects(params, cb) {
        s3.listObjects(params, function(err, data) {
            if (err) {
                console.log("listS3Objects Error:", err);
            } else {
                var contents = data.Contents;
                s3DataContents = s3DataContents.concat(contents);
                if (data.IsTruncated) {
                    // Set Marker to last returned key
                    params.Marker = contents[contents.length-1].Key;
                    s3ListObjects(params, cb);
                } else {
                    cb();
                }
            }
        });
    }
    
    s3ListObjects(params, s3Print);
    

    Pay attention to listObject's documentation of NextMarker, which is NOT always present in the returned data object, so I don't use it at all in the above code ...

    NextMarker — (String) When response is truncated (the IsTruncated element value in the response is true), you can use the key name in this field as marker in the subsequent request to get next set of objects. Amazon S3 lists objects in alphabetical order Note: This element is returned only if you have delimiter request parameter specified. If response does not include the NextMarker and it is truncated, you can use the value of the last Key in the response as the marker in the subsequent request to get the next set of object keys.

    The entire program has now been pushed to https://github.com/kenklin/s3list.

提交回复
热议问题