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 ?

Antzi
  • 12,831
  • 7
  • 48
  • 74
  • 1
    A better solution would be to upload the pictures in S3 and then process it with Lambda. http://stackoverflow.com/questions/31645205/how-to-upload-file-to-lambda-function-or-api-gateway – Alexis N-o May 25 '16 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... – Antzi May 25 '16 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. – advncd Oct 17 '16 at 22:06

2 Answers2

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

jackko
  • 6,998
  • 26
  • 38
  • 1
    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... – advncd Oct 17 '16 at 22:08
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
gdvalderrama
  • 713
  • 1
  • 17
  • 26