3

I am trying to send a file from client side and receive it through AWS API Gateway to my Lambda function which will then put this file in S3 bucket.

I have used the following as default parameter template in API Gateway

{"image" : $input.params('MediaUrl0')} 

How will I receive it in python which looks like: def read_upload_toS3(event, context): s3 = boto3.resource('s3')

1 Answer 1

5

You could use the lately introduced $input.body variable in your mapping template:

{ "body" : "$input.body" } 

You maybe should also check out this discussion on this problem. To receive the body in your python function just do

def my_handler(event, context): body = event['body'] 

But if the sole purpose of the function is to upload the file to S3, you could also do this directly with API Gateway:

  • Go to the Integration Request settings of your method
  • Under Integration Type klick show advanced
  • Select AWS Service Proxy
  • Select S3 als the AWS Service and fill in the necessary information
Sign up to request clarification or add additional context in comments.

6 Comments

I would like to know how to receive this in the Python.
I got this error. Can you please help me with this.. {"message": "Could not parse request body into json: Unexpected character (\'-\' (code 45)) in numeric value: expected digit (0-9) to follow minus sign, for valid numeric value\n at [Source: [B@98c32b3; line: 1, column: 3]"}
I have the same error. It seems api gateway still can't properly handle multipart-form data
Try { "body" : "$util.base64Encode($input.body)" } if you're getting an error.
"Could not parse request body into json: Unexpected character..." can be solved by adding a "multipart/form-data" body template in the "Integration Request" section of your POST resource. (A spanish/speaking video made by me: youtu.be/79ckseUxvLw)
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.