Amazon S3: How to get a list of folders in a bucket?

All I found is this GET Bucket method But I don’t understand how I can only get the list of folders in the current folder. What prefix and separator do I need to use? Is this even possible?

+4
source share
4 answers

For example, suppose I have a bucket in an area USEast1called MyBucketName, with the following keys:

 temp/
 temp/foobar.txt
 temp/txt/
 temp/txt/test1.txt
 temp/txt/test2.txt
 temp2/

, S3 - , , S3. - , - S3, . , , , "" ( '/', size = 0), "", S3.

: NuGet 3.1 AWSSDK.S3.

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: ,

, , Prefix, Prefix ListObjectsRequest.

ListObjectsRequest request = new ListObjectsRequest
    {
        BucketName = "MyBucketName",
        Prefix = "temp/"
    };

1 :

temp/
temp/txt/

:

+15

https://github.com/minio/minio-dotnet

Minio.Net API Amazon S3 .

, . CommonPrefix API ListObjects().

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

using Minio;
using Minio.Xml;

namespace Minio.Examples
{
    class ListObjects
    {
        static int Main(string[] args)
        {
            var client = new MinioClient("https://s3.amazonaws.com", "ACCESSKEY", "SECRETKEY");

            var items = client.ListObjects("bucket");

            foreach (Item item in items)
            {
                if (item.IsDir)
                {
                    Console.Out.WriteLine("{0}", item.Key);
                }
            }
            return 0;
        }
    }
}
+4

prefix of the/path/to/read/ ( , , ) delimiter /, <CommonPrefixes>.

CommonPrefixes, . , CommonPrefixes ( -) , . , CommonPrefixes , , prefix. , - /, - (/), // - notes/summer/. , , . . MaxKeys.

http://docs.aws.amazon.com/AmazonS3/latest/API/RESTBucketGET.html

+3

, , . S3 , "folder/name.ext", S3 "", , .

- "/". # , , python .

+2

Source: https://habr.com/ru/post/1614612/


All Articles