python 列出所有S3存储桶和对象

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了python 列出所有S3存储桶和对象相关的知识,希望对你有一定的参考价值。

#
# Print all S3 buckets and objects
# Warning: If you have a lot of S3 objects, this could potentially incur a large cost.
#

import boto3

client = boto3.client('s3')

buckets = client.list_buckets()
for bucket in buckets["Buckets"]:
	print(bucket['Name'])
	keylist = client.list_objects_v2(Bucket=bucket["Name"], Delimiter='/')
	if "Contents" in keylist:
		for object in keylist.get('CommonPrefixes'):
			print(object.get('Prefix'))

以上是关于python 列出所有S3存储桶和对象的主要内容,如果未能解决你的问题,请参考以下文章