[英]return list of objects in s3 prefix using boto3
I have the boto3 code below.我有下面的boto3代码。 In the code I am trying to get a list of objects in an s3 prefix.
在代码中,我试图获取 s3 前缀中的对象列表。 The complete path to the object in the s3 prefix is:
s3 前缀中 object 的完整路径是:
path to file:文件路径:
s3://our_data/our_folder/part-00000-bf597559-8a11-4e6b-8631-ef7c138b4583-c000.txt
When I run the code below it doesn't return anything, but I could swear a couple months ago it worked.当我运行下面的代码时,它不会返回任何东西,但我可以发誓几个月前它有效。 Did anything change with boto3 related to this recently, or am I missing something in my code?
最近与此相关的 boto3 有什么变化,还是我的代码中遗漏了什么?
code:代码:
import boto3
s3_client = boto3.client('s3',
region_name='us-west-2')
trz_bucket = 'our_data'
trz_prefix = 'our_folder/'
result = s3_client.list_objects(Bucket=trz_bucket, Prefix=trz_prefix, Delimiter='v')
result.get('CommonPrefixes')
The CommonPrefixes
will be returned if you provide a Delimiter
:如果您提供
Delimiter
,则将返回CommonPrefixes
:
list_objects(Bucket=trz_bucket, Prefix=trz_prefix, Delimiter='/')
Here' an example of using Delimiter
and CommonPrefixes
using the AWS CLI (which would work the same as using boto3):这是一个使用 AWS CLI 使用
Delimiter
和CommonPrefixes
的示例(其工作方式与使用 boto3 相同):
$ aws s3 mb s3://test-delimiter
make_bucket: test-delimiter
$ aws s3 cp x.py s3://test-delimiter/folder1/
upload: ./x.py to s3://test-delimiter/folder1/x.py
$ aws s3 cp x.py s3://test-delimiter/folder2/
upload: ./x.py to s3://test-delimiter/folder2/x.py
$ aws s3api list-objects --bucket test-delimiter --delimiter '/'
{
"CommonPrefixes": [
{
"Prefix": "folder1/"
},
{
"Prefix": "folder2/"
}
]
}
$ aws s3 cp x.py s3://test-delimiter/folder1/folder3/
upload: ./x.py to s3://test-delimiter/folder1/folder3/x.py
$ aws s3api list-objects --bucket test-delimiter --delimiter '/' --prefix folder1/
{
"Contents": [
{
"Key": "folder1/x.py",
"LastModified": "2021-06-05T07:52:35+00:00",
"ETag": "\"1083f8ddcea96dd6b9d8a3a582373c71\"",
"Size": 727,
"StorageClass": "STANDARD"
}
}
],
"CommonPrefixes": [
{
"Prefix": "folder1/folder3/"
}
]
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.