简体   繁体   English

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

[英]AWS S3 Listing API - How to list everything inside S3 Bucket with specific prefix

I am trying to list all items with specific prefix in S3 bucket. 我正在尝试在S3存储桶中列出具有特定prefix所有项目。 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: 当我将prefex设置为Item1/Item2 ,得到以下结果:

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? 无论如何,在golang中可以实现这一目标吗?

Folders do not actually exist in Amazon S3. 文件夹实际上在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命令行界面(CLI),我可以将命令复制到Amazon S3存储桶:

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

This work just fine, even though folder1 and folder2 do not exist. 即使folder1folder2不存在,该工作也很好。 This is because objects are stored with a Key (filename) that includes the full path of the object . 这是因为对象使用包含对象完整路径的 Key (文件名)进行存储。 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. 但是,为了使人们更容易使用,Amazon S3管理控制台使它看起来好像有文件夹。 In S3, these are called Common Prefixes rather than folders. 在S3中,这些称为通用前缀而不是文件夹。

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". 因此,当您在指定Prefix同时进行API调用以列出存储桶的内容时,它只会说“列出其键以此字符串开头的所有对象”。

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 . 实际上,这将创建一个长度为0的对象,其名称与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. 这可能就是为什么Item1/Item2/出现在您的列表中,而Item1/Item2/Item3却没有出现的原因。 Somebody, at some stage, must have "created a folder" called Item1/Item2/ , which actually created a zero-length object with that Key. 在某个阶段,某人必须“创建了一个文件夹”,称为Item1/Item2/ ,实际上使用该Key创建了一个零长度的对象。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM