我有一个版本化的存储桶,并希望从存储桶中删除该对象(及其所有版本).但是,当我尝试从控制台删除对象时,S3只是添加删除标记但不执行硬删除.
s3resource = boto3.resource('s3')
bucket = s3resource.Bucket('my_bucket')
obj = bucket.Object('my_object_key')
# I would like to delete all versions for the object like so:
obj.delete_all_versions()
# or delete all versions for all objects like so:
bucket.objects.delete_all_versions()
最佳答案
作为@jarmod答案的补充,这里有一种方法我开发了一种“硬删除”对象的解决方法(包括删除市场对象);
原文链接:https://www.f2er.com/python/438801.htmldef get_all_versions(bucket,filename):
s3 = boto3.client('s3')
keys = ["Versions","DeleteMarkers"]
results = []
for k in keys:
response = s3.list_object_versions(Bucket=bucket)[k]
to_delete = [r["VersionId"] for r in response if r["Key"] == filename]
results.extend(to_delete)
return results
bucket = "YOUR BUCKET NAME"
file = "YOUR FILE"
for version in get_all_versions(bucket,file):
s3.delete_object(Bucket=bucket,Key=file,VersionId=version)