Antzi
Antzi

Reputation: 13444

Upload image to Lambda using gateway API

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 ?

Upvotes: 1

Views: 2881

Answers (2)

gdvalderrama
gdvalderrama

Reputation: 806

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

Upvotes: 1

jackko
jackko

Reputation: 7344

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

Upvotes: 1

Related Questions