Create a lambda that accepts an image file via POST request

I’m attempting to crate a lambda function in python that accepts an image file via a POST request.
I understand aws api gatway supports binary files now. But I failed to find any example of how to use it.
How should i configure the serverless.yml?
How can i access the binary data in the python lambda?
Is it Base64Encoded?

1 Like

Hi @amitm02 ,
You’ll need to configure binary media types (e.g. image/png, image/gif, etc.) AND you need to pass a corresponding Content-Type header.
Setting up binary media types cannot be configured natively in serverless, yet, but there’s a plugin for this.

The following links might help:

Once you setup binary media types and POST an image with a “matching” Content-Type header, the binary data will be base64 encoded. This means the body will be base64 encoded and the property “isBase64Encoded” will be set to true.

Note: Binary media types are applied to requests and responses and require appropriate Content-Type and Accept headers.

Also keep in mind that there’s a limit of 10MB. So using S3 instead might be beneficial in some situations.


for future reference, i’ve used to following in the lambda

body = event[‘body’]‘API isBase64Encoded={}’.format(event[‘isBase64Encoded’]))‘body type={}’.format(type(body)))‘body len: {}’.format(len(body)))‘content-type: {}’.format(event[‘headers’][‘Content-Type’]))‘b64decoding…’)
img = b64decode(body)

1 Like

Nice on getting it working. I was trying to get it working but decided I would upload to s3 from the client and then send the path the my lambda function.