Jayanth Thyagarajan
Jayanth Thyagarajan

Reputation: 86

How do I pass multipart-form data to AWS Lambda

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

Upvotes: 3

Views: 9556

Answers (1)

birnbaum
birnbaum

Reputation: 4946

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

Upvotes: 5

Related Questions