'Unable to delete S3 objects with spaces in name
Through a typo I ended up creating a number of S3 files with spaces in their name. I realize based on the key naming guidelines that this is not an ideal situation, but the objects now exist. I have tried to delete them both from the AWS CLI and from the S3 console. Neither method produces an error, but the objects are not deleted. I tried renaming the files to remove the offending space, but this also fails on both CLI and console. How can I delete these objects?
Solution 1:[1]
Try using AWS SDKs (links to boto3 commands):
- List the objects - See (boto3) S3.Client.list_objects
- Filter the objects (keys) you want to delete from the list
- Delete the objects of the filtered list using S3.Bucket.delete_objects
Solution 2:[2]
This answer applies to cases when you are using boto3 instead of aws cli but run into the same problem of the OP.
The problem:
When boto3 retrieves object names spaces in the key are encoded as "+" character. I don't know why the spaces are not url-encoded as %20
(although this post has answers that might explain why) Other special characters in the key name are url-encoded. Ironically "+" in an object name is encoded as %2B
by boto3.
The solution:
Before passing a key name to boto3 delete_objects method, I cleaned up the key this way:
remove_plus = x-www-form-urlencoded_key.replace("+", " ")
uncoded_key = urllib.parse.unquote(remove_plus)
response = client.delete_object(
Bucket=bucket_name,
Key=uncoded_key
)
I suppose there's a more correct way of handling application/x-www-form-urlencoded type strings, but this is working for me right now.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | Guy Wald |
Solution 2 | 208_man |