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

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

    In fact aws2js supports listing of objects in a bucket on a low level via s3.get() method call. To do it one has to pass prefix parameter which is documented on Amazon S3 REST API page:

    var s3 = require('aws2js').load('s3', awsAccessKeyId, awsSecretAccessKey);    
    s3.setBucket(bucketName);
    
    var folder = encodeURI('some/path/to/S3/folder');
    var url = '?prefix=' + folder;
    
    s3.get(url, 'xml', function (error, data) {
        console.log(error);
        console.log(data);
    });
    

    The data variable in the above snippet contains a list of all objects in the bucketName bucket.

提交回复
热议问题