Skip to content

Upload a image file to a Python lambda.

0

How do I do this? We have a React FE and want it to send a file to a lambda (which we will then send to an API and S3).

Current thinking is that we send the file/fields to the lambda using form-multipart, then we decode it in the lambda somehow?

Thanks,

Mick

asked 2 years ago5.2K views
2 Answers
1

Hi,
For uploading image to a Lambda function, you can use API Gateway to expose the Lambda at an endpoint, and then make a POST request with the Content-Type header as multipart/form-data . Then from the Lambda function, you can store the image into S3 and invoke another API call.

You can follow along this blog [2] for reference.


** UPDATE **
To handle the multi-part upload in the Lambda, make sure to configure API Gateway Mapping Template [3]:

In the Integration Request section of your API Gateway POST method, select Mapping Templates. Add a new mapping template for multipart/form-data content type. This template should transform the incoming request body into a format that your Lambda function understands.

Then, in Lambda, use the below code:

imageBody = base64.b64decode(event['body-json'])

A similar blog post [4] explains the way to handle form-multipart. (Japanese content, use translator if needed)


References:
[1] https://docs.aws.amazon.com/lambda/latest/dg/services-apigateway.html
[2] https://aws.amazon.com/blogs/compute/uploading-to-amazon-s3-directly-from-a-web-or-mobile-application/
[3] https://docs.aws.amazon.com/apigateway/latest/developerguide/api-gateway-mapping-template-reference.html
[4] https://qiita.com/tetsu0831/items/220ba032116dc94063b3

Please let me know you have any other doubts regarding this.

Thanks,
Atul

answered 2 years ago
0

Thanks,

Yes, we have made it that far. Just not sure how to handle the form data in the Python lambda.

answered 2 years ago
  • Thanks for the reply. I have updated the previous answer to assist you with your issue on handling the data in Lambda.

You are not logged in. Log in to post an answer.

A good answer clearly answers the question and provides constructive feedback and encourages professional growth in the question asker.