简体   繁体   中英

AWS API Gateway - Lambda - Internal Server Error

I'm uploading an image to s3, through a lambda, and everything works well, with no errors, but the response from API Gateway is 500 Internal Server Error.

I configured my api-gateway following this tutorial: Binary Support for API Integrations with Amazon API Gateway .

My lambda receives the base64Image, decode it and successfully upload to s3.

This is my lambda code:

def upload_image(event, context):
    s3 = boto3.client('s3')
    b64_image = event['base64Image']
    image = base64.b64decode(b64_image)

    try:
        with io.BytesIO(image) as buffer_image:
            buffer_image.seek(0)
            s3.upload_fileobj(buffer_image, 'MY-BUCKET', 'image')

        return {'status': True}

    except ClientError as e:
        return {'status': False, 'error': repr(e)}

This is what i'm receiving: { "message": "Internal server error" }, with a 500 status code.

Obs: I'm not using lambda proxy integration.

You need to return a header in the response, eg in Python:

    return {
        "statusCode": 200,
        'headers': { 'Content-Type': 'application/json' },
        "body": json.dumps(body)
    }

That example looks like it falls short on mapping the responses section in favor of a pass through. In which case changing your return to: return {'status': True, 'statusCode': 200} might work.

Generally speaking there are two paths when building a response with ApiGateway-Lambda. One is the lambda-proxy (where your lambda function defines the response), the other is where ApiGateway transforms your responses and generates the appropriate headers/status based on a mapping.

The path from the example is for the latter.

Personally I would change: return {'status': True} to return {'status': "Success"} And create a regex that looks for the word "Success" and "Error" respectively.

I have used this blog post successfully with this technique (it also describes at length the differences between the two approaches). Once you get one mapping working you could adjust it as is more appropriate for your implementation.

EDIT: hot tip these decorators are awesome and make python & lambda even cleaner/easier but mostly for the proxy setup

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM