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

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

    Here's what I came up with based on the other answers.
    You can await listAllKeys() without having to use callbacks.

    const listAllKeys = () =>
      new Promise((resolve, reject) => {
        let allKeys = [];
        const list = marker => {
          s3.listObjects({ Marker: marker }, (err, data) => {
            if (err) {
              reject(err);
            } else if (data.IsTruncated) {
              allKeys.push(data.Contents);
              list(data.NextMarker || data.Contents[data.Contents.length - 1].Key);
            } else {
              allKeys.push(data.Contents);
              resolve(allKeys);
            }
          });
        };
        list();
      });
    

    This assumes you've initialized the s3 variable like so

    const s3 = new aws.S3({
      apiVersion: API_VERSION,
      params: { Bucket: BUCKET_NAME }
    });
    

提交回复
热议问题