Delete all versions of an object in S3 using python?

后端 未结 9 2033
既然无缘
既然无缘 2021-02-05 17:28

I have a versioned bucket and would like to delete the object (and all of its versions) from the bucket. However, when I try to delete the object from the console, S3 simply add

9条回答
  •  名媛妹妹
    2021-02-05 18:00

    You can delete an object with all of its versions using following code

    session = boto3.Session(aws_access_key_id, aws_secret_access_key)
    
    bucket_name = 'bucket_name'
    object_name = 'object_name'
    
    s3 = session.client('s3')
    
    versions = s3.list_object_versions (Bucket = bucket_name, Prefix = object_name)
    version_list = versions.get('Versions')
    for version in version_list:
        versionId = version.get('VersionId')
        s3.delete_object(Bucket = bucket_name, Key= object_name, VersionId = versionId)
    

提交回复
热议问题