问题描述
说我在Amazon S3存储桶中有以下目录和文件(文件以粗体表示):
Say I have the following directories and files in an Amazon S3 bucket (files are in bold):
- 存储区名称/
- 存储区名称/文件夹1/
- 存储桶名称/文件夹1/ foobar.txt
- 存储区名称/文件夹1/子文件夹1/
- 存储区名称/文件夹1/子文件夹1/ hello.txt
- 存储区名称/文件夹1/子文件夹2/
- 存储桶名称/文件夹1/子文件夹2/ world.txt
- 存储区名称/文件夹1/子文件夹2/子子文件夹1/
- 存储区名称/文件夹1/子文件夹2/子子文件夹1/ file.txt
- bucketname/
- bucketname/folder1/
- bucketname/folder1/foobar.txt
- bucketname/folder1/subfolder1/
- bucketname/folder1/subfolder1/hello.txt
- bucketname/folder1/subfolder2/
- bucketname/folder1/subfolder2/world.txt
- bucketname/folder1/subfolder2/subsubfolder1/
- bucketname/folder1/subfolder2/subsubfolder1/file.txt
如何使用.NET AWS S3 API列出给定目录的所有对象和立即子目录,而不必递归地将所有内容和目录下的所有内容和立即子目录列出?换句话说,如何在单个级别上浏览"目录的内容?
How can I list all objects and immediate subdirectories of a given directory with the .NET AWS S3 API, without recursively getting everything below that directory? In other words, how can I "browse" the contents of a directory at a single level?
例如,假设我要浏览bucketname/folder1/
的内容.我希望看到以下内容:
For example, imagine I want to browse the contents of bucketname/folder1/
. What I would like to see is the following:
- 存储桶名称/文件夹1/ foobar.txt
- 存储区名称/文件夹1/子文件夹1/
- 存储区名称/文件夹1/子文件夹2/
...别无其他.我不想列出子目录中的文件和目录,我只想列出folder1
级别的文件和子目录.
...and nothing else. I don't want to list the files and directories in subdirectories, I just want to list the files and subdirectories at the folder1
level.
是否可以将过滤器应用于单个AWS API调用,以使它不会返回一切并迫使我仅手动解析我需要的内容?
Is there a way to apply filters to a single AWS API call so that it doesn't return everything and force me to manually parse only what I need?
我发现该代码让我仅获得直接的子目录(按预期方式),但是我也无法弄清楚如何包括直接的文件:
I've found that this code let's me get just the immediate subdirectories (as intended), but I can't figure out how to include the immediate files too:
var request = new ListObjectsRequest()
.WithBucketName("bucketname")
.WithPrefix(@"folder1/")
.WithDelimiter(@"/");
using (var client = Amazon.AWSClientFactory.CreateAmazonS3Client(accessKey, secretKey))
using (var response = client.ListObjects(request))
{
foreach (var item in response.CommonPrefixes)
{
/* ... */
}
}
推荐答案
我遇到了相反的问题(我知道如何获取指定文件夹中的文件,而不是子目录中的文件).
I had the opposite problem (I knew how to get the files in the specified folder, but not the subdirectories).
答案是Amazon列出的文件与子文件夹不同.
The answer is that Amazon lists files differently than it does sub-folders.
子文件夹在 ListObjectsResponse.CommonPrefixes 集合中列出.
Sub-folders are listed, as your example shows, in the ListObjectsResponse.CommonPrefixes collection.
文件在 ListObjectsResponse.S3Objects 集合中列出.
所以您的代码应如下所示:
So your code should look like this:
var request = new ListObjectsRequest()
.WithBucketName("bucketname")
.WithPrefix(@"folder1/")
.WithDelimiter(@"/");
using (var client = Amazon.AWSClientFactory.CreateAmazonS3Client(accessKey, secretKey))
using (var response = client.ListObjects(request))
{
foreach (var subFolder in response.CommonPrefixes)
{
/* list the sub-folders */
}
foreach (var file in response.S3Objects) {
/* list the files */
}
}
我的Google搜索在燃烧的僧侣博客,在评论部分中带有以下内容:
my google search turned up this post on the burningmonk blog with this in the comment section:
要列出"rootfolder"的内容,请在请求前将前缀设置为文件夹名称加上反斜杠,例如"rootfolder/",并将定界符设置为"/".在响应中,您将始终将文件夹本身作为元素,其键名与请求中使用的前缀相同,并在"CommonPrefixes"属性中添加所有子文件夹.
To list the contents of a ‘rootfolder’, make the request with prefix set to the name of the folder plus the backslash, e.g. ‘rootfolder/’ and set the delimiter to ‘/’. In the response you’ll always have the folder itself as an element with the same key as the prefix you used in the request, plus any subfolders in the ‘CommonPrefixes’ property.
这篇关于如何使用AWS S3 API非递归浏览目录的内容?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!