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

前端 未结 15 1003
情书的邮戳
情书的邮戳 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
    2020-12-02 14:42

    Published knox-copy when I couldn't find a good existing solution. Wraps all the pagination details of the Rest API into a familiar node stream:

    var knoxCopy = require('knox-copy');
    
    var client = knoxCopy.createClient({
      key: '',
      secret: '',
      bucket: 'mrbucket'
    });
    
    client.streamKeys({
      // omit the prefix to list the whole bucket
      prefix: 'buckets/of/fun' 
    }).on('data', function(key) {
      console.log(key);
    });
    

    If you're listing fewer than 1000 files a single page will work:

    client.listPageOfKeys({
      prefix: 'smaller/bucket/o/fun'
    }, function(err, page) {
      console.log(page.Contents); // <- Here's your list of files
    });
    

提交回复
热议问题