Search code examples
pythonamazon-web-servicesaws-lambda

Send image file to AWS Lambda Function


I'm trying to send one image to my Lambda Function using Python just to test for one project, but Postman is giving me one error and I don't know how to solve it.

My code is simply to detect if I have some data in the key "image" and return some message. I'm using Postman to send the POST request, I clicked in the Body tab, selected the form-data option and I wrote image for the key and selected the image file from my computer (the image size is 27 kb). This is the code in my Lambda Function:

def lambda_handler(event, context):
    if event['image']:
        return {
            "Message": 'Everything went ok'
        }

And this is the error message that I'm receiving from Postman:

{ "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: (byte[])"----------------------------137965576541301454606184\r\nContent-Disposition: form-data; name="image"; filename="TestImage.png"\r\nContent-Type: image/png\r\n\r\n�PNG\r\n\n ... }


Solution

  • To solve that problem, I needed to set my Camera to convert the image to base64 and then upload it to the server.

    In the server, I convert it again and then work with it as I want. Base64 is a group of binary-to-text encoding schemes that represent binary data in an ASCII string format by translating it into a radix-64 representation.

    So, you will convert your image to string and then sending it, it was the best way that I found to upload my images.