[aws] Catch errors raised when deleting objects remaining in an s3_bucket (#43358)
* Catch errors raised when deleting objects remaining in a bucket * Also remove VersionId when it is not needed
This commit is contained in:
parent
fee4c24ad4
commit
1c082e93ef
1 changed files with 16 additions and 1 deletions
|
@ -444,8 +444,23 @@ def destroy_bucket(s3_client, module):
|
||||||
try:
|
try:
|
||||||
for key_version_pairs in paginated_versions_list(s3_client, Bucket=name):
|
for key_version_pairs in paginated_versions_list(s3_client, Bucket=name):
|
||||||
formatted_keys = [{'Key': key, 'VersionId': version} for key, version in key_version_pairs]
|
formatted_keys = [{'Key': key, 'VersionId': version} for key, version in key_version_pairs]
|
||||||
|
for fk in formatted_keys:
|
||||||
|
# remove VersionId from cases where they are `None` so that
|
||||||
|
# unversioned objects are deleted using `DeleteObject`
|
||||||
|
# rather than `DeleteObjectVersion`, improving backwards
|
||||||
|
# compatibility with older IAM policies.
|
||||||
|
if not fk.get('VersionId'):
|
||||||
|
fk.pop('VersionId')
|
||||||
|
|
||||||
if formatted_keys:
|
if formatted_keys:
|
||||||
s3_client.delete_objects(Bucket=name, Delete={'Objects': formatted_keys})
|
resp = s3_client.delete_objects(Bucket=name, Delete={'Objects': formatted_keys})
|
||||||
|
if resp.get('Errors'):
|
||||||
|
module.fail_json(
|
||||||
|
msg='Could not empty bucket before deleting. Could not delete objects: {0}'.format(
|
||||||
|
', '.join([k['Key'] for k in resp['Errors']])
|
||||||
|
),
|
||||||
|
errors=resp['Errors'], response=resp
|
||||||
|
)
|
||||||
except (BotoCoreError, ClientError) as e:
|
except (BotoCoreError, ClientError) as e:
|
||||||
module.fail_json_aws(e, msg="Failed while deleting bucket")
|
module.fail_json_aws(e, msg="Failed while deleting bucket")
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue