Javascript AWS S3 对象列表

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/30726079/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 05:39:22  来源:igfitidea点击:

AWS S3 object listing

javascriptnode.jsamazon-web-servicesamazon-s3

提问by Rohit

I am using aws-sdk using node.js. I want to list images in specified folder e.g.This is the directory that i want to fetch

我正在使用 node.js 使用 aws-sdk。我想列出指定文件夹中的图像,例如这是我要获取的目录

I want to list all files and folder in this location but not folder (images) content. There is list Object functionin aws-sdk but it is listing all the nested files also.

我想列出此位置中的所有文件和文件夹,但不列出文件夹(图像)内容。aws-sdk 中有list Object 函数,但它也列出了所有嵌套文件。

Here is the code :

这是代码:

var AWS = require('aws-sdk');
AWS.config.update({accessKeyId: 'mykey', secretAccessKey: 'mysecret', region: 'myregion'});
var s3 = new AWS.S3();

var params = { 
  Bucket: 'mystore.in',
  Delimiter: '',
  Prefix: 's/5469b2f5b4292d22522e84e0/ms.files' 
}

s3.listObjects(params, function (err, data) {
  if(err)throw err;
  console.log(data);
});

采纳答案by Michael - sqlbot

Folders are illusory, but S3 doesprovide a mechanism to emulate their existence.

文件夹是虚幻的,但 S3确实提供了一种机制来模拟它们的存在。

If you set Delimiterto /then each tier of responses will also return a CommonPrefixesarray of the next tier of "folders," which you'll append to the prefix from this request, to retrieve the next tier.

如果您设置Delimiter为,/那么每一层响应还将返回CommonPrefixes下一层“文件夹”的数组,您将附加到此请求的前缀中,以检索下一层。

If your Prefixis a "folder," append a trailing slash. Otherwise, you'll make an unnecessary request, because the first request will return one common prefix. E.g., folder "foo" will return one common prefix "foo/".

如果您Prefix是“文件夹”,请在尾部添加斜杠。否则,您将发出不必要的请求,因为第一个请求将返回一个公共前缀。例如,文件夹“foo”将返回一个公共前缀“foo/”。

回答by Rohit

It's working fine now using this code :

现在使用此代码工作正常:

var AWS = require('aws-sdk');
AWS.config.update({accessKeyId: 'mykey', secretAccessKey: 'mysecret', region: 'myregion'});
var s3 = new AWS.S3();

var params = { 
 Bucket: 'mystore.in',
 Delimiter: '/',
 Prefix: 's/5469b2f5b4292d22522e84e0/ms.files/'
}

s3.listObjects(params, function (err, data) {
 if(err)throw err;
 console.log(data);
});

回答by Vasyl Boroviak

I put up a little modulewhich lists contents of a "folder" you give it:

我建立了一个小模块,其中列出了您提供的“文件夹”的内容:

s3ls({bucket: 'my-bucket-name'}).ls('/', console.log);

will print something like this:

将打印如下内容:

{ files: [ 'funny-cat-gifs-001.gif' ],
  folders: [ 'folder/', 'folder2/' ] }

And that

然后

s3ls({bucket: 'my-bucket-name'}).ls('/folder', console.log);

will print

将打印

{ files: [ 'folder/cv.docx' ],
  folders: [ 'folder/sub-folder/' ] }

UPD:The latest version supports async/await Promise interface:

UPD:最新版本支持 async/await Promise 接口:

const { files, folders } = await lister.ls("/my-folder/subfolder/");

And here is the s3ls.js:

这是s3ls.js

var _ = require('lodash');
var S3 = require('aws-sdk').S3;

module.exports = function (options) {
  var bucket = options.bucket;
  var s3 = new S3({apiVersion: '2006-03-01'});

  return {
    ls: function ls(path, callback) {
      var prefix = _.trimStart(_.trimEnd(path, '/') + '/', '/');    
      var result = { files: [], folders: [] };

      function s3ListCallback(error, data) {
        if (error) return callback(error);

        result.files = result.files.concat(_.map(data.Contents, 'Key'));
        result.folders = result.folders.concat(_.map(data.CommonPrefixes, 'Prefix'));

        if (data.IsTruncated) {
          s3.listObjectsV2({
            Bucket: bucket,
            MaxKeys: 2147483647, // Maximum allowed by S3 API
            Delimiter: '/',
            Prefix: prefix,
            ContinuationToken: data.NextContinuationToken
          }, s3ListCallback)
        } else {
          callback(null, result);
        }
      }

      s3.listObjectsV2({
        Bucket: bucket,
        MaxKeys: 2147483647, // Maximum allowed by S3 API
        Delimiter: '/',
        Prefix: prefix,
        StartAfter: prefix // removes the folder name from the file listing
      }, s3ListCallback)
    }
  };
};

回答by Gaurav Sharma

You can use the Prefixin s3 API params. I am adding an example that i used in a project:

您可以使用Prefixin s3 API 参数。我正在添加一个我在项目中使用的示例:

listBucketContent: ({ Bucket, Folder }) => new Promise((resolve, reject) => {
    const params = { Bucket, Prefix: `${Folder}/` };
    s3.listObjects(params, (err, objects) => {
        if (err) {
            reject(ERROR({ message: 'Error finding the bucket content', error: err }));
        } else {
            resolve(SUCCESS_DATA(objects));
        }
    });
})

Here Bucketis the name of the bucket that contains a folder and Folderis the name of the folder that you want to list files in.

Bucket是包含文件夹的存储桶Folder的名称,也是您要在其中列出文件的文件夹的名称。

回答by koolhead17

Alternatively you can use minio-jsclient library, its open source & compatible with AWS S3 api.

或者,您可以使用minio-js客户端库,它是开源的并且与 AWS S3 api 兼容。

You can simply use list-objects.jsexample, additional documentation are available at https://docs.minio.io/docs/javascript-client-api-reference.

您可以简单地使用list-objects.js示例,其他文档可在https://docs.minio.io/docs/javascript-client-api-reference 获得

var Minio = require('minio')

var s3Client = new Minio({
  endPoint: 's3.amazonaws.com',
  accessKey: 'YOUR-ACCESSKEYID',
  secretKey: 'YOUR-SECRETACCESSKEY'
})
// List all object paths in bucket my-bucketname.
var objectsStream = s3Client.listObjects('my-bucketname', '', true)
objectsStream.on('data', function(obj) {
  console.log(obj)
})
objectsStream.on('error', function(e) {
  console.log(e)
})

Hope it helps.

希望能帮助到你。

Disclaimer: I work for Minio

免责声明:我为Minio工作

回答by Alex

As mentioned in the comments, S3 doesn't "know" about folders, only keys. You can imitate a folder structure with / in the keys. See here for more information - http://docs.aws.amazon.com/AmazonS3/latest/UG/FolderOperations.html

正如评论中提到的,S3 不“知道”文件夹,只知道键。您可以模仿带有 / 键的文件夹结构。请参阅此处了解更多信息 - http://docs.aws.amazon.com/AmazonS3/latest/UG/FolderOperations.html

That said, you can modify your code to something like this:

也就是说,您可以将代码修改为如下所示:

s3.listObjects(params, function (err, data) {
  if(err) throw 

  //data.contents is an array of objects according to the s3 docs
  //iterate over it and see if the key contains a / - if not, it's a file (not a folder)
  var itemsThatAreNotFolders = data.contents.map(function(content){
    if(content.key.indexOf('/')<0) //if / is not in the key
        return content;
  });

  console.log(itemsThatAreNotFolders);
});

This will check each key to see if it contains a /

这将检查每个键以查看它是否包含 /