How do I pass multipart-form data to AWS Lambda

Jayanth Thyagarajan picture Jayanth Thyagarajan · Apr 29, 2016 · Viewed 8.1k times · Source

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')

Answer

birnbaum picture birnbaum · Apr 29, 2016

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