[英]Lambda function to delete an S3 bucket using Boto
I am working on a Boto script to delete the resource when invoked by a lambda function, I am not sure how to invoke the delete function using Lambda, "path"
has the resource to be deleted and below is the lambda function being used to delete資源。 提前致謝。
print(path)
def delete_bucket(path):
while True:
objects = s3.list_objects(Bucket=path)
content = objects.get('Contents', [])
if len(content) == 0:
break
for obj in content:
s3.delete_object(Bucket=path, Key=obj['Key'])
s3.delete_bucket(Bucket=path)
def lambda_handler(event, context):
delete_bucket(path)
以下代碼在 Python 3.8 上驗證;
import boto3
def get_s3_client():
return boto3.client('s3', region_name='eu-west-1') #change region_name as per your setup
def delete_bucket(bucket_name): #here bucket_name can be path as per logic in your code
s3_client = get_s3_client()
while True:
objects = s3_client.list_objects(Bucket=bucket_name)
content = objects.get('Contents', [])
if len(content) == 0:
break
for obj in content:
s3_client.delete_object(Bucket=bucket_name, Key=obj['Key'])
s3_client.delete_bucket(Bucket=bucket_name)
def lambda_handler(event, context):
# put your existing logic here
delete_bucket(path)
注意:如果您為存儲桶啟用了版本控制,那么您將需要額外的邏輯來使用list_object_versions
列出對象,然后遍歷此類版本 object 以使用delete_object
刪除它們
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.