Mehran
Mehran

Reputation: 16831

How to read an Object in a Bucket with pluses and spaces in its name

I've set up an event handler on a Bucket (in AWS). The event handler is supposed to read the file and process it. And I have a problem with the files that have space in their filename.

When I upload a file with name + .txt, on the event handler I receive ++.txt. I understand that spaces are replaced with + but how can I differentiate between spaces and actual pluses? How can I read such a file on the event handler?

Upvotes: 2

Views: 2477

Answers (1)

Michael - sqlbot
Michael - sqlbot

Reputation: 179064

S3 treats + and %20 identically. They are both represented internally as +. There is no differentiation. An unescaped space in a URI is a protocol violation, so your original upload must have had %20 for the space.

foo+bar and foo%20bar refer to exactly the same object, whose key is foo bar.

To get the correct object key from an event, you need this:

const real_key = decodeURIComponent(event.s3.object.key.replace(/\+/g, ' '));

This is the result of a very old bug in S3, and the current incorrect behavior is too entrenched in existing libraries to allow it to be fixed without widespread collateral damage.

Note also that %2B is not considered equivalent to + by S3, but since browsers don't eacape URLs that way, storing objects with %2B in the key does not interoperate well.

Upvotes: 4

Related Questions