如何使用 boto3 遍历 Wasabi / S3 bucket 中的所有对象
此代码片段展示如何遍历 bucket 中的所有对象:
iterate_s3_objects.py
import boto3
# 创建到 Wasabi / S3 的连接
s3 = boto3.resource('s3',
endpoint_url = 'https://s3.eu-central-1.wasabisys.com',
aws_access_key_id = 'MY_ACCESS_KEY',
aws_secret_access_key = 'MY_SECRET_KEY'
)
# 获取 bucket 对象
my_bucket = s3.Bucket('boto-test')
# 遍历 bucket 中的对象
for obj in my_bucket.objects.all():
print(obj)Don’t forget to fill in MY_ACCESS_KEY and MY_SECRET_KEY. Depending on what region and what S3-compatible service you use, you might need to use another endpoint URL instead of https://s3.eu-central-1.wasabisys.com.
示例输出:
s3_objects_output.txt
s3.ObjectSummary(bucket_name='boto-test', key='10k-Test-Objects/1.txt')
s3.ObjectSummary(bucket_name='boto-test', key='10k-Test-Objects/10.txt')
s3.ObjectSummary(bucket_name='boto-test', key='10k-Test-Objects/100.txt')
[...]If this post helped you, please consider buying me a coffee or donating via PayPal to support research & publishing of new posts on TechOverflow