这个函数打印s3桶的列表以及它们的对象键。如何使这个函数只打印空桶的列表?
import boto3
s3 = boto3.resource('s3')
def empty_s3():
#This will print list of all buckets
print("nList of S3 buckets:")
for bucket in s3.buckets.all():
print(bucket.name)
#This will print s3 bucket object keys
for object in bucket.objects.all():
print(object)
您可以简单地检查对象的数量:
import boto3
s3_resource = boto3.resource('s3')
for bucket in s3_resource.buckets.all():
objects = list(bucket.objects.all())
# Empty bucket?
if len(objects) == 0:
print(bucket.name)