doucheyi1347 2019-06-12 21:41
浏览 711
已采纳

AWS S3 Listing API-如何列出具有特定前缀的S3存储桶中的所有内容

I am trying to list all items with specific prefix in S3 bucket. Here is directory structure that I have:

Item1/
     Item2/
          Item3/
               Item4/
                     image_1.jpg
               Item5/
                     image_1.jpg
                     image_2.jpg

When I set prefex to be Item1/Item2, I get as a result following keys:

Item1/Item2/
Item1/Item2/Item3/Item4/image_1.jpg
Item1/Item2/Item3/Item5/image_1.jpg
Item1/Item2/Item3/Item5/image_2.jpg

What I would like to get is:

Item1/Item2/
Item1/Item2/Item3
Item1/Item2/Item3/Item4
Item1/Item2/Item3/Item5
Item1/Item2/Item3/Item4/image_1.jpg
Item1/Item2/Item3/Item5/image_1.jpg
Item1/Item2/Item3/Item5/image_2.jpg

Is there anyway to achieve this in golang?

  • 写回答

1条回答 默认 最新

  • dragoninasia2014 2019-06-13 00:54
    关注

    Folders do not actually exist in Amazon S3. It is a flat object storage system.

    For example, using the AWS Command-Line Interface (CLI) I could copy a command to an Amazon S3 bucket:

    aws s3 cp foo.txt s3://my-bucket/folder1/folder2/foo.txt
    

    This work just fine, even though folder1 and folder2 do not exist. This is because objects are stored with a Key (filename) that includes the full path of the object. So, the above object actually has a Key (filename) of:

    folder1/folder2/foo.txt
    

    However, to make things easier for humans, the Amazon S3 management console makes it appear as though there are folders. In S3, these are called Common Prefixes rather than folders.

    So, when you make an API call to list the contents of the bucket while specifying a Prefix, it simply says "List all objects whose Key starts with this string".

    Your listing doesn't show any folders because they don't actually exist.

    Now, just to contradict myself, it actually is possible to create a folder (eg by clicking Create folder in the management console). This actually creates a zero-length object with the same name as the folder. The folder will then appear in listings because it is actually listing the zero-length object rather than the folder.

    This is probably why Item1/Item2/ appears in your listing, but Item1/Item2/Item3 does not. Somebody, at some stage, must have "created a folder" called Item1/Item2/, which actually created a zero-length object with that Key.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 蓝牙耳机怎么查看日志
  • ¥15 R语言 拟时序分析降维图如何减少分支
  • ¥15 Fluent齿轮搅油
  • ¥15 八爪鱼爬数据为什么自己停了
  • ¥15 交替优化波束形成和ris反射角使保密速率最大化
  • ¥15 树莓派与pix飞控通信
  • ¥15 自动转发微信群信息到另外一个微信群
  • ¥15 outlook无法配置成功
  • ¥30 这是哪个作者做的宝宝起名网站
  • ¥60 版本过低apk如何修改可以兼容新的安卓系统