Brosef
Brosef

Reputation: 3095

Confirming delete of S3 object from Heroku

I'm deleting an object from my S3 bucket from Heroku by doing:

S3_BUCKET = os.environ.get('S3_BUCKET')
key = str(form.trackID.data) + '.m4a'
s3 = boto3.client('s3')
response = s3.delete_object(Bucket=S3_BUCKET, Key=key)
print(response)

So it looks like the files are getting deleted but the response I'm getting isn't what I was expecting:

{'ResponseMetadata': {'HostId': 'kxEnqrRWgfIdeqdUaGQB5ZS75MrMO+hfw/pZPcUxwzLnaRYwqQ7ORyf34S/dsWnRvyVnPuBabPM=', 'HTTPStatusCode': 204, 'RetryAttempts': 0, 'HTTPHeaders': {'server': 'AmazonS3', 'date': 'Fri, 21 Oct 2016 01:54:09 GMT', 'x-amz-request-id': 'E2030C4C2CC59C65', 'x-amz-id-2': 'kxEnqrRWgfIdeqdUaGQB5ZS75MrMO+hfw/pZPcUxwzLnaRYwqQ7ORyf34S/dsWnRvyVnPuBabPM='}, 'RequestId': 'E2030C4C2CC59C65'}}

but the documentation says I should expect a dictionary like below:

{
    'DeleteMarker': True|False,
    'VersionId': 'string',
    'RequestCharged': 'requester'
}

http://boto3.readthedocs.io/en/latest/reference/services/s3.html#S3.Client.delete_object

how can I confirm the file has been deleted?

Upvotes: 1

Views: 692

Answers (1)

helloV
helloV

Reputation: 52403

The response you are getting is correct. The Boto3 document is wrong. There is an outstanding issue to fix the documentation even though it is for delete_objects(): Boto3 delete_objects response does not match documented response

If you want to make sure the object has been deleted, try:

'Contents' in s3.list_objects(Bucket=S3_BUCKET, Prefix=key)

should return True if the object exists, False if the object doesn't exist.

Another option is to call get_object_acl() and expect it to thrown an exception if the object doesn't exist.

s3.get_object_acl(Bucket=S3_BUCKET, Key=key)

botocore.exceptions.ClientError: An error occurred (NoSuchKey) when calling the GetObjectAcl operation: The specified key does not exist.

Upvotes: 2

Related Questions