def cleanup_bucket(s3, delete_bucket=False): for bucket in s3.buckets.all(): if bucket.name == BUCKET_NAME: for key in bucket.objects.all(): key.delete() if delete_bucket: bucket.delete() return False return True return False
def tearDownModule(): '''Called once by unittest when tearing down this module. Empties and removes the test S3 bucket. ''' s3 = boto3.resource('s3') bucket = s3.Bucket(BUCKET_NAME) try: cleanup_bucket() bucket.delete() except s3.meta.client.exceptions.NoSuchBucket: pass try: bucket.wait_until_not_exists() except Exception: # # This is bad, but not fatal, and should not cause the whole test run # to explode. Either the bucket will get deleted by AWS eventually, # or we can clean it up later ourselves. # pass