Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

S3 Event sends URL encoded key names, causing Lambda handler to fail on Textract API calls #2

Open
matwerber1 opened this issue Aug 31, 2019 · 2 comments

Comments

@matwerber1
Copy link

Hi,

Per S3 docs:

The s3 key provides information about the bucket and object involved in the event. The object key name value is URL encoded. For example, "red flower.jpg" becomes "red+flower.jpg" (Amazon S3 returns "application/x-www-form-urlencoded" as the content type in the response).

The current extract-Enhancer-TextractAsyncJobSubmitFunction Lambda does not URL decode the S3 key received in the event JSON, so I'm receiving errors when trying to parse objects that contain spaces or other URL-encoding relevant characters.

For example, If I upload a document named my test.pdf, the S3 event sent to the extract-Enhancer-TextractAsyncJobSubmitFunction function contains the key Records[0].s3.object.key = my+test.pdf.

The Textract API calls textract.start_document_analysis() and textract.start_document_text_detection() then fail because the DocumentLocation parameter has a value of my+test.pdf when it should instead be my test.pdf.

Can you add URL decoding to the S3 key name in the received events?

@matwerber1 matwerber1 changed the title S3 event replaces spaces in object name with plus (+), causing Textract API to fail S3 Event sends URL encoded key names, causing Lambda handler to fail on Textract API calls Aug 31, 2019
@matwerber1
Copy link
Author

Tested the following addition:

from urllib.parse import unquote_plus
...
document = unquote_plus(record['s3']['object']['key'])

That worked fine but ran into a new issue...

The JobTag and ClientRequestToken parameters of textract.start_document_text_detection() do not allow spaces or plusses (though they do allow dashes).

Maybe the S3 object's MD5 should instead be used as the value of ClientRequestToken?

Not sure about JobTag... haven't looked at rest of code yet, not sure if JobTag matters?

@matwerber1
Copy link
Author

matwerber1 commented Sep 1, 2019

Created this PR to address the issue: #3

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant