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

前端 未结 5 1125
野趣味
野趣味 2021-01-02 10:01

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?

5条回答
  •  爱一瞬间的悲伤
    2021-01-02 10:07

    Alternatively another simpler approach is using https://github.com/minio/minio-dotnet

    Minio .Net implements minimal API's to work with Amazon S3 and other compatible storage solutions.

    Following example shows how you can filter out only directories. Here the CommonPrefix is abstracted as a folder through the ListObjects() API.

    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;
            }
        }
    }
    

提交回复
热议问题