|
|
@ -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")
|
|
|
|
|
|
|
|
|
|
|
|