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:
-
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);
-
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:
- Why does AWS encode the S3 object key in the event, and is decodeURIComponent the recommended approach to decode it?
- 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!
2
Answers
The solution for this question was two fold:
Final solution:
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