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

前端 未结 15 1004
情书的邮戳
情书的邮戳 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条回答
  •  慢半拍i
    慢半拍i (楼主)
    2020-12-02 14:44

    I am using this version with async/await.
    This function will return the content in an array.
    I'm also using the NextContinuationToken instead of the Marker.

    async function getFilesRecursivelySub(param) {
    
        // Call the function to get list of items from S3.
        let result = await s3.listObjectsV2(param).promise();
    
        if(!result.IsTruncated) {
            // Recursive terminating condition.
            return result.Contents;
        } else {
            // Recurse it if results are truncated.
            param.ContinuationToken = result.NextContinuationToken;
            return result.Contents.concat(await getFilesRecursivelySub(param));
        }
    }
    
    async function getFilesRecursively() {
    
        let param = {
            Bucket: 'YOUR_BUCKET_NAME'
            // Can add more parameters here.
        };
    
        return await getFilesRecursivelySub(param);
    }
    

提交回复
热议问题