简体   繁体   English

如何使用AWS S3 API非递归地浏览目录的内容?

[英]How can I non-recursively browse the contents of a directory with the AWS S3 API?

Say I have the following directories and files in an Amazon S3 bucket (files are in bold ): 假设我在Amazon S3存储桶中有以下目录和文件(文件以粗体显示 ):

  • bucketname/ bucketname /
  • bucketname/folder1/ bucketname /文件夹1 /
  • bucketname/folder1/ foobar.txt bucketname / folder1 / foob​​ar.txt
  • bucketname/folder1/subfolder1/ bucketname /文件夹1 / subfolder1 /
  • bucketname/folder1/subfolder1/ hello.txt bucketname / folder1 / subfolder1 / hello.txt
  • bucketname/folder1/subfolder2/ bucketname /文件夹1 / subfolder2 /
  • bucketname/folder1/subfolder2/ world.txt bucketname / folder1 / subfolder2 / world.txt
  • bucketname/folder1/subfolder2/subsubfolder1/ bucketname /文件夹1 / subfolder2 / subsubfolder1 /
  • bucketname/folder1/subfolder2/subsubfolder1/ file.txt bucketname / folder1 / subfolder2 / subsubfolder1 / file.txt

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? 如何使用.NET AWS S3 API列出给定目录的所有对象和直接子目录,而不递归获取该目录下的所有内容? In other words, how can I "browse" the contents of a directory at a single level? 换句话说,如何在单个级别“浏览”目录的内容?

For example, imagine I want to browse the contents of bucketname/folder1/ . 例如,想象一下我想要浏览bucketname/folder1/的内容。 What I would like to see is the following: 我想看到的是以下内容:

  • bucketname/folder1/ foobar.txt bucketname / folder1 / foob​​ar.txt
  • bucketname/folder1/subfolder1/ bucketname /文件夹1 / subfolder1 /
  • bucketname/folder1/subfolder2/ bucketname /文件夹1 / subfolder2 /

...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. 我不想列出子目录中的文件和目录,我只想列出folder1级别的文件和子目录。

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? 有没有办法将过滤器应用于单个AWS API调用,以便它不会返回所有内容并强制我手动解析我需要的内容?

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). 我有相反的问题(我知道如何获取指定文件夹中的文件,但不知道子目录)。

The answer is that Amazon lists files differently than it does sub-folders. 答案是,亚马逊列出的文件与子文件夹不同。

Sub-folders are listed, as your example shows, in the ListObjectsResponse.CommonPrefixes collection. 正如您的示例所示,在ListObjectsResponse.CommonPrefixes集合中列出了子文件夹。

Files are listed in the ListObjectsResponse.S3Objects 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 */
    }
}

my google search turned up this post on the burningmonk blog with this in the comment section: 我的谷歌搜索在burnmonk博客上发布了这篇文章,在评论部分:

When you make the ListObjects request, to list the top level folders, don't set the prefix but set the delimiter to '/', then inspect the 'CommonPrefixes' property on the response for the folders that are in the top folder. 当您发出ListObjects请求时,要列出顶级文件夹,请不要设置前缀,而是将分隔符设置为“/”,然后检查响应中的“CommonPrefixes”属性,以查找顶部文件夹中的文件夹。

To list the contents of a 'rootfolder', make the request with prefix set to the name of the folder plus the backslash, eg 'rootfolder/' and set the delimiter to '/'. 要列出'rootfolder'的内容,请将前缀设置为文件夹名称加反斜杠的请求,例如'rootfolder /',并将分隔符设置为'/'。 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. 在响应中,您将始终将文件夹本身作为元素,其具有与您在请求中使用的前缀相同的键,以及“CommonPrefixes”属性中的任何子文件夹。

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

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