How to return binary data from lambda function in AWS in Python?

后端 未结 4 1753
不知归路
不知归路 2020-12-14 02:18

I cannot get python lambda to return binary data. The node-template for thumbnail images works fine but I cannot get a python lambda to work. Below is the relevant lines fro

4条回答
  •  轮回少年
    2020-12-14 02:50

    I finally figured this out. Returning binary data from a python lambda is doable.

    Follow the instructions here: https://aws.amazon.com/blogs/compute/binary-support-for-api-integrations-with-amazon-api-gateway/

    Be sure to check the 'Use Lambda Proxy integration' when creating a new method.

    Also be sure your python lambda response looks like:

    return {'isBase64Encoded'   : True,
            'statusCode'        : 200,
            'headers'           : { 'Content-Type': content_type },
            'body'              : base64_encoded_binary_data}
    

    THEN:

    For each of your routes/methods issue:

    apigateway update-integration-response --rest-api-id  --resource-id  --http-method POST --status-code 200 --patch-operations "[{\"op\" : \"replace\", \"path\" : \"/contentHandling\", \"value\" : \"CONVERT_TO_BINARY\"}]"
    

    In the AWS console. The and can be seen in the API Gateway 'breadcrumbs' ex:

     = zdb7jsoey8
     = zy2b5g
    

    THEN: You need to 'Deploy API'. From what I found only it only worked AFTER deploying the API.

    Be sure you setup the 'Binary Media Types' before deploying.

    Hint: Nice AWS shell terminal here: https://github.com/awslabs/aws-shell

    pip install aws-shell
    

提交回复
热议问题