如何在S3中获取存储桶的*目录?

时间:2022-04-30 10:44:54

How do I list of the common prefixes for the objects in my bucket?

如何列出存储桶中对象的公共前缀?

If I use ListObjects, I am able to retrieve objects. I can see there's a common prefix called 2017:

如果我使用ListObjects,我可以检索对象。我可以看到有一个名为2017的常见前缀:

using (var client = new AmazonS3Client())
{
    var listObjectsResponse = client.ListObjects(new ListObjectsRequest
    {
        BucketName = bucket,
        Prefix = "2",
        Delimiter = "",
    });

    // Prints: 2017/11/08/<omitted>.json
    Console.WriteLine(listObjectsResponse.S3Objects.First().Key);        
}

However if I set Delimiter, nothing is returned:

但是,如果我设置分隔符,则不返回任何内容:

using (var client = new AmazonS3Client())
{
    var listObjectsResponse = client.ListObjects(new ListObjectsRequest
    {
        BucketName = bucket,
        Prefix = "2",
        Delimiter = "/",
    });

    // Prints: 0
    Console.WriteLine(listObjectsResponse.S3Objects.Count);      
}

How do I get the common prefixes like 2017 to be returned?

如何获得2017年的常用前缀?

I've tried looking at the documentation and it just says to use prefix and delimiter, but that's not working.

我已经尝试查看文档,它只是说使用前缀和分隔符,但这不起作用。

1 个解决方案

#1


1  

I see there's a CommonPrefixes property on ListObjectsResponse.

我看到ListObjectsResponse上有一个CommonPrefixes属性。

using (var client = new AmazonS3Client())
{
    var listObjectsResponse = client.ListObjects(new ListObjectsRequest
    {
        BucketName = bucket,
        Prefix = "2",
        Delimiter = "/",
    });

    // Prints: 2017
    Console.WriteLine(listObjectsResponse.CommonPrefixes[0]);     
}

#1


1  

I see there's a CommonPrefixes property on ListObjectsResponse.

我看到ListObjectsResponse上有一个CommonPrefixes属性。

using (var client = new AmazonS3Client())
{
    var listObjectsResponse = client.ListObjects(new ListObjectsRequest
    {
        BucketName = bucket,
        Prefix = "2",
        Delimiter = "/",
    });

    // Prints: 2017
    Console.WriteLine(listObjectsResponse.CommonPrefixes[0]);     
}