Amazon S3:如何获取存储桶中的文件夹列表? [英] Amazon S3: How to get a list of folders in the bucket?
问题描述
所有我发现的东西,就是这种方法获取存储桶 但是我不明白如何才能仅获得当前文件夹中的文件夹列表.我需要使用哪个前缀和定界符?可能吗?
为示例起见,假设我在USEast1
区域中有一个名为MyBucketName
的存储桶,并带有以下键:
temp/
temp/foobar.txt
temp/txt/
temp/txt/test1.txt
temp/txt/test2.txt
temp2/
使用文件夹可能会造成混乱,因为S3本身不支持层次结构-而是,它们像其他任何S3对象一样只是键.文件夹只是S3 Web控制台中可用的抽象,它使导航存储桶变得更加容易.因此,当我们以编程方式工作时,我们希望找到与文件夹"的尺寸匹配的键(定界符"/",大小= 0),因为它们很可能是S3控制台向我们展示的文件夹".>
两个示例的注意事项:我正在使用AWSSDK.S3版本3.1 NuGet软件包.
示例1:存储桶中的所有文件夹
此代码是从此基本示例中修改的S3文档列出了存储桶中的所有键.下面的示例将标识所有以定界符/
结尾且为空的键.
IAmazonS3 client;
using (client = new AmazonS3Client(Amazon.RegionEndpoint.USEast1))
{
// Build your request to list objects in the bucket
ListObjectsRequest request = new ListObjectsRequest
{
BucketName = "MyBucketName"
};
do
{
// Build your call out to S3 and store the response
ListObjectsResponse response = client.ListObjects(request);
// Filter through the response to find keys that:
// - end with the delimiter character '/'
// - are empty.
IEnumerable<S3Object> folders = response.S3Objects.Where(x =>
x.Key.EndsWith(@"/") && x.Size == 0);
// Do something with your output keys. For this example, we write to the console.
folders.ToList().ForEach(x => System.Console.WriteLine(x.Key));
// If the response is truncated, we'll make another request
// and pull the next batch of keys
if (response.IsTruncated)
{
request.Marker = response.NextMarker;
}
else
{
request = null;
}
} while (request != null);
}
预期输出到控制台:
temp/
temp/txt/
temp2/
示例2:与指定前缀匹配的文件夹
您可以通过将 应用于示例1时,我们希望获得以下输出: 进一步阅读: All that I found, it's this method GET Bucket
But I can't understand how can I get only a list of folders in the current folder. Which prefix and delimiter I need to use? Is that possible at all? For the sake of example, assume I have a bucket in the Working with folders can be confusing because S3 does not natively support a hierarchy structure -- rather, these are simply keys like any other S3 object. Folders are simply an abstraction available in the S3 web console to make it easier to navigate a bucket. So when we're working programatically, we want to find keys matching the dimensions of a 'folder' (delimiter '/', size = 0) because they will likely be 'folders' as presented to us by the S3 console. Note for both examples: I'm using the AWSSDK.S3 version 3.1 NuGet package. Example 1: All folders in a bucket This code is modified from this basic example in the S3 documentation to list all keys in a bucket. The example below will identify all keys that end with the delimiter character Expected output to console: Example 2: Folders matching a specified prefix You could further limit this to only retrieve folders matching a specified When applied to Example 1, we would expect the following output: Further reading: 这篇关于Amazon S3:如何获取存储桶中的文件夹列表?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!temp/
temp/txt/
USEast1
region called MyBucketName
, with the following keys: temp/
temp/foobar.txt
temp/txt/
temp/txt/test1.txt
temp/txt/test2.txt
temp2/
/
, and are also empty. IAmazonS3 client;
using (client = new AmazonS3Client(Amazon.RegionEndpoint.USEast1))
{
// Build your request to list objects in the bucket
ListObjectsRequest request = new ListObjectsRequest
{
BucketName = "MyBucketName"
};
do
{
// Build your call out to S3 and store the response
ListObjectsResponse response = client.ListObjects(request);
// Filter through the response to find keys that:
// - end with the delimiter character '/'
// - are empty.
IEnumerable<S3Object> folders = response.S3Objects.Where(x =>
x.Key.EndsWith(@"/") && x.Size == 0);
// Do something with your output keys. For this example, we write to the console.
folders.ToList().ForEach(x => System.Console.WriteLine(x.Key));
// If the response is truncated, we'll make another request
// and pull the next batch of keys
if (response.IsTruncated)
{
request.Marker = response.NextMarker;
}
else
{
request = null;
}
} while (request != null);
}
temp/
temp/txt/
temp2/
Prefix
by setting the Prefix
property on ListObjectsRequest.ListObjectsRequest request = new ListObjectsRequest
{
BucketName = "MyBucketName",
Prefix = "temp/"
};
temp/
temp/txt/