Silenced Temporarily
Silenced Temporarily

Reputation: 1004

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?

Upvotes: 3

Views: 1685

Answers (3)

Ali.Ghodrat
Ali.Ghodrat

Reputation: 3704

Let's say you have this command in your AWS CLI:

aws s3 rm s3://your-bucket-name/folderA/Family Name.txt

So there is a Space between "Family Name" object.

Then you can remove this object like:

aws s3 rm s3://your-bucket-name/folderA/Family" "Name.txt

There should be one space between the double quotation marks.

Upvotes: 0

208_man
208_man

Reputation: 1728

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.

Upvotes: 1

Guy Wald
Guy Wald

Reputation: 599

Try using AWS SDKs (links to boto3 commands):

  1. List the objects - See (boto3) S3.Client.list_objects
  2. Filter the objects (keys) you want to delete from the list
  3. Delete the objects of the filtered list using S3.Bucket.delete_objects

Upvotes: 3

Related Questions