[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:
Ryan Brown 2018-07-30 11:22:00 -04:00 committed by GitHub
parent fee4c24ad4
commit 1c082e93ef
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -444,8 +444,23 @@ def destroy_bucket(s3_client, module):
try:
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]
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:
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:
module.fail_json_aws(e, msg="Failed while deleting bucket")