Python 使用Boto3删除CloudTrail及其附带的S3存储桶
我正在编写一个python脚本来删除CloudTrail以及与之相关的S3 bucket,我没有收到任何错误,但是代码也没有删除S3 bucket。我能够删除CloudTrail。这是我的代码: def lambda_处理程序(事件、上下文):Python 使用Boto3删除CloudTrail及其附带的S3存储桶,python,amazon-web-services,amazon-s3,boto3,amazon-cloudtrail,Python,Amazon Web Services,Amazon S3,Boto3,Amazon Cloudtrail,我正在编写一个python脚本来删除CloudTrail以及与之相关的S3 bucket,我没有收到任何错误,但是代码也没有删除S3 bucket。我能够删除CloudTrail。这是我的代码: def lambda_处理程序(事件、上下文): 我觉得你需要在删除S3 bucket之前清除所有对象,如何删除bucket中的所有对象,请参见此链接: import boto3 import pprint client=boto3.client('ec2') s3_resour
我觉得你需要在删除S3 bucket之前清除所有对象,如何删除bucket中的所有对象,请参见此链接:
import boto3
import pprint
client=boto3.client('ec2')
s3_resource=boto3.client('s3')
all_regions=client.describe_regions()
#pprint.pprint(all_regions)
list_of_regions=[]
del_list = []
Bucket_names=[]
for each_reg in all_regions['Regions']:
list_of_regions.append(each_reg['RegionName'])
#print(each_reg['RegionName'])
#print(list_of_regions)
for each_reg in list_of_regions:
client = boto3.client('cloudtrail', region_name=each_reg)
trailnames=[trail['TrailARN'] for trail in client.list_trails()['Trails']]
#print(trailnames,each_reg)#List out the Trails Name
for data in trailnames:
#print(data)
response = client.describe_trails(trailNameList=[data])
#print(response)
Bucket_Name =response['trailList'][0].get('S3BucketName')
#print(Bucket_Name)
Bucket_names.append(Bucket_Name)
#print(Bucket_Name)
#home_region = response['trailList'][0].get('HomeRegion')
#print(home_region)
#home_client = boto3.client('cloudtrail', region_name=home_region)
#print(home_client)
#del_response =home_client.delete_trail(Name=data)
#print(data)
for buckets in Bucket_names:
s3_resource = boto3.client('s3', region_name=each_reg)
#print(buckets)
objects = s3_resource.list_objects(Bucket=buckets)['Contents']
#print(objects)
#a=objects
#print(a[5].get('Key'))
file_key_name=objects[0].get('Key')
#print(file_key_name)
copy_source_bucket = {'Bucket': buckets, 'Key': file_key_name}
#s3_resource.copy(copy_source_bucket, buckets, file_key_name, ExtraArgs={'ACL': 'bucket-owner-full-control'})
copy_objectss=s3_resource.copy_object(Bucket = buckets, Key = file_key_name, CopySource = copy_source_bucket,ACL='bucket-owner-full-control',MetadataDirective='REPLACE')
print(copy_objectss)
s3_del = client.delete_bucket(
Bucket='buckets'
)