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

birnbaum
  • 4,718
  • 28
  • 37

1 Answers1

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
birnbaum
  • 4,718
  • 28
  • 37
  • I would like to know how to receive this in the Python. – Jayanth Thyagarajan Apr 29 '16 at 08:43
  • 1
    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]"} – Jayanth Thyagarajan Apr 30 '16 at 07:43
  • 1
    I have the same error. It seems api gateway still can't properly handle multipart-form data – ALex_hha Sep 29 '16 at 20:48
  • 1
    Try `{ "body" : "$util.base64Encode($input.body)" }` if you're getting an error. – Rush Nov 07 '16 at 20:20
  • 1
    "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: https://youtu.be/79ckseUxvLw) – christian Mar 30 '17 at 18:11
  • Its been years for this question and still I am facing the issue. Why AWS not working on this...?? Anyone with the trick...?? I am getting this in `$input.json($)` as `----------------------------688578637414600670323347\r\nContent-Disposition: form-data; name=\"filename\"\r\n\r\nfilename.json\r\n----------------------------688578637414600670323347--` – Shivendra Pratap Kushwaha Apr 14 '21 at 07:28