1

I'm trying to let user upload pictures to a lambda function for processing; using the gateway API interface.

I tried to specify a model for my POST method, but so far I keep getting the error

Invalid model specified: Validation Result: warnings : [], errors : [Invalid model schema specified]

... Not so helpful.

I understand that I cannot directly send raw data to lambda and must using some kind of formatting in-between.

What I understood is that I could make the gateway interface base64 encode the data for me.

I tried to do so by using the following model schema with the content type image/jpeg

{
    "body" : $util.base64Encode($input.body)
}

How to send the image ?

3
  • 1
    A better solution would be to upload the pictures in S3 and then process it with Lambda. stackoverflow.com/questions/31645205/… Commented May 25, 2016 at 14:06
  • 2
    It seems either using s3 or performing base64 before upload is necessary. Base64 consume more bandwidth and s3 force us to perform additional requests... Commented May 25, 2016 at 14:12
  • 1
    Wrapping $util.base64Encode($input.body) in quotes will solve the issue: "body": "$util.base64Encode($input.body)". However, the encoded data is corrupt and useless. Commented Oct 17, 2016 at 22:06

2 Answers 2

1

There is no native support in API Gateway for binary data as you have seen. We are working on this but I don't have an ETA for you. Some customers have had success base64 encoding the data like you have in your question, only it should be in a mapping template in the Integration Request not the Method Request.

If you set the content type to image/jpeg, then the encoding will apply only when the Content-Type header on the incoming request is also image/jpeg, so make sure to set that.

You can also reject incoming requests to the method that don't send the right Content Type by setting the 'Request body passthrough' (passthroughBehavior in the API) to the recommended value ("when there are no templates defined" or 'WHEN_NO_TEMPLATES' in the API)

Docs for the passthrough behavior -> https://docs.aws.amazon.com/apigateway/api-reference/resource/integration/#passthroughBehavior

Sign up to request clarification or add additional context in comments.

1 Comment

I was able to base64-encode png image and send it to lambda. Lambda then decodes it and stores the image somewhere in s3. But that image is corrupt. In other words, base64Encode doesn't work correctly for binary data. Not sure how some of your customers have had success base64 encoding the data...
1

Since it seems like working with binary data and API Gateway is complicated, I think you should:

  1. Upload the image using API Gateway as an S3 proxy
  2. Set a trigger for your lambda function on PUT for the bucket where you've uploaded the image

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.