aws s3 獲取所有文件,Amazon S3:如何获取存储桶中的文件夹列表?

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 USEast1 region called MyBucketName, with the following keys:

temp/

temp/foobar.txt

temp/txt/

temp/txt/test1.txt

temp/txt/test2.txt

temp2/

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 /, 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 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);

}

Expected output to console:

temp/

temp/txt/

temp2/

Example 2: Folders matching a specified prefix

You could further limit this to only retrieve folders matching a specified Prefix by setting the Prefix property on ListObjectsRequest.

ListObjectsRequest request = new ListObjectsRequest

{

BucketName = "MyBucketName",

Prefix = "temp/"

};

When applied to Example 1, we would expect the following output:

temp/

temp/txt/

Further reading:

获取Amazon S3存储桶中的文件夹列表,可以使用Amazon S3ListObjects API。以下是一种获取文件夹列表的方法: 1. 使用AWS SDK或AWS命令行界面(CLI)与Amazon S3建立连接。 2. 使用ListObjects API,并指定存储桶的名称和文件夹的前缀。 3. 根据需要设置其他参数,例如分页、排序等。 4. 执行API请求并获取返回的结果。 5. 在返回的结果中,筛选出文件夹对象。 请注意,Amazon S3并没有实际的文件夹概念,它使用对象键(Object Key)来模拟文件夹结构。文件夹只是键中的一部分,例如"folder/file.txt"。因此,获取文件夹列表实际上是获取与指定前缀相匹配的对象键列表。 以下是使用AWS SDK for Python(Boto3)的示例代码: ```python import boto3 # 创建S3客户端 s3 = boto3.client('s3') # 指定存储桶名称和文件夹前缀 bucket_name = 'your-bucket-name' prefix = 'your-folder-prefix/' # 执行ListObjects API请求 response = s3.list_objects_v2(Bucket=bucket_name, Prefix=prefix, Delimiter='/') # 获取文件夹列表 folders = [common_prefix['Prefix'] for common_prefix in response.get('CommonPrefixes', [])] # 打印文件夹列表 for folder in folders: print(folder) ``` 替换`your-bucket-name`为你的存储桶名称,`your-folder-prefix/`为你的文件夹前缀。执行上述代码后,将会打印出存储桶中以指定前缀开头的文件夹列表。 希望对你有所帮助!如有其他问题,请随时提问。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值