1

I have an AWS Lambda function set up as an event source for an S3 bucket to process newly uploaded PDF files. However, I've noticed that when a file with spaces in its name is uploaded, the event data I receive in the Lambda function has pluses (+) instead of spaces in the key property of the S3 event object.

For example, if I upload a file named "World War.pdf" to my S3 bucket, the Lambda event object shows the key as "public/World+War.pdf":

{
    "s3SchemaVersion": "1.0",
    "configurationId": "arn:aws:cloudformation:eu-west-1:12345673333312:stack/my-lambda-stack/...",
    "bucket": {
        "name": "mybucket",
        "ownerIdentity": { "principalId": "EXAMPLE" },
        "arn": "arn:aws:s3:::mybucket"
    },
    "object": {
        "key": "public/World+War.pdf",
        "size": 40656,
        "eTag": "123456789abcdef",
        "sequencer": "1234567890ABCDEF"
    }
}

This causes issues when I try to use this key to access the file in my S3 bucket, as the actual file name contains spaces and not pluses.

What I've tried:

  1. I have considered using decodeURIComponent to convert the + back to spaces, but I'm unsure if this is a reliable solution, or if I'm supposed to handle these keys differently.

    const key = decodeURIComponent(event.Records[0].s3.object.key);

  2. I have also looked into the S3 event notification configurations to see if there's a way to receive the key without URL encoding, but found nothing in the AWS documentation.

Questions:

  1. Why does AWS encode the S3 object key in the event, and is decodeURIComponent the recommended approach to decode it?
  2. Is there a configuration in AWS S3 or Lambda that I can set to receive the object key with spaces instead of pluses?

CloudWatch Error Message:

Error processing and ingesting PDF: public/World+War.pdf. Error: NoSuchKey: The specified key does not exist.

Any help or guidance on this would be greatly appreciated!

4
  • Yes it's being encoded. Why do you need to convert back to spaces? If your Lambda function just needs to read the object in S3, then the object key with + will work. Commented Nov 9, 2023 at 13:53
  • Hi Mark, it's because I sent the file name to an SQS queue, and when the queue consumer tries to obtain the file name, it cannot due to the +. Commented Nov 9, 2023 at 14:17
  • "when the queue consumer tries to obtain the file name, it cannot due to the +" That doesn't make much sense. What is the actual error? Commented Nov 9, 2023 at 15:13
  • Error processing and ingesting PDF: public/World+War.pdf. Error: NoSuchKey: The specified key does not exist. Commented Nov 9, 2023 at 16:32

2 Answers 2

2

I hope you can find a solution at this link. I had a similar issue and found a solution through this link. AWS: how to fix S3 event replacing space with '+' sign in object key names in json

Sign up to request clarification or add additional context in comments.

Comments

1

The solution for this question was two fold:

  1. Define the variables before using it
  2. Decoding it.

Final solution:

const file = decodeURIComponent(event.Records[0].s3.object.key.replace(/\+/g, " "));

1 Comment

This is the result of a very early design decision in S3 that can't be fixed because so much code accommodates the broken behavior. Object keys have spaces replaced with + like you would find in a query string. It's very much incorrect to encode this way in the path, but that's now it was done in S3.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.