AWS S3 object listing

前端 未结 6 2158
南方客
南方客 2021-02-06 20:52

I am using aws-sdk using node.js. I want to list images in specified folder e.g.\"This

6条回答
  •  -上瘾入骨i
    2021-02-06 21:23

    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

    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 /

提交回复
热议问题