简体   繁体   English

AWS API 网关 - Lambda - 内部服务器错误

[英]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.我正在通过 lambda 将图像上传到 s3,一切正常,没有错误,但 API 网关的响应是 500 内部服务器错误。

I configured my api-gateway following this tutorial: Binary Support for API Integrations with Amazon API Gateway .我按照本教程配置了我的 api-gateway: Binary Support for API Integrations with Amazon API Gateway

My lambda receives the base64Image, decode it and successfully upload to s3.我的 lambda 接收 base64Image,对其进行解码并成功上传到 s3。

This is my lambda code:这是我的 lambda 代码:

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.这是我收到的信息:{“message”:“内部服务器错误”},带有 500 状态代码。

Obs: I'm not using lambda proxy integration. Obs:我没有使用 lambda 代理集成。

You need to return a header in the response, eg in Python:您需要在响应中返回一个标头,例如在 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.在这种情况下,将返回值更改为: return {'status': True, 'statusCode': 200}可能会起作用。

Generally speaking there are two paths when building a response with ApiGateway-Lambda.一般来说,使用 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.一个是 lambda 代理(您的 lambda 函数在其中定义响应),另一个是 ApiGateway 转换您的响应并根据映射生成适当的标头/状态。

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.我个人会改变: return {'status': True} return {'status': "Success"}并创建一个正则表达式,分别查找单词“Success”和“Error”。

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编辑:热门提示,这些装饰器很棒,使 python 和 lambda 更干净/更容易,但主要用于代理设置

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 AWS 上的 API 网关和 lambda 出现“内部服务器错误” - "internal server error" with API gateway and lambda on AWS API网关中的AWS Lambda集成响应 - {“message”:“内部服务器错误”} - AWS Lambda Integration Response in API Gateway - {“message”: “Internal server error”} AWS Lambda 调用 API 网关得到 500 内部服务器错误 - AWS Lambda calling API gateway getting 500 internal server error AWS Lambda-API 网关“消息”:“内部服务器错误”(502 错误网关) - AWS Lambda-API gateway "message": "Internal server error" (502 Bad Gateway) 在 VS2019 中使用 API Gateway 部署到 AWS Lambda 会给出 {“message”:“Internal Server Error”} - Deploying to AWS Lambda in VS2019 with API Gateway gives {“message”:“Internal Server Error”} 内部服务器错误 - 当通过 aws lambda/api gateway/s3 上传 base64 时 - Internal server error - when upload base64 through aws lambda/api gateway/s3 为什么在通过 API 网关调用时,Java 中的 AWS Lambda 代码返回“内部服务器错误”? - why does this AWS Lambda code in Java return "internal server error" when invoked via an API gateway? 间歇性内部服务器错误 - API 网关上的 StatusCode 500 调用 Lambda - Intermittent Internal Server Error - StatusCode 500 on API Gateway calling Lambda 部署时 API 网关 + Lambda 出现 502“内部服务器错误” - 502 "Internal Server Error" with API Gateway + Lambda when deploying “消息”:Lambda/API 网关和 iOS 的“内部服务器错误”问题 - "message" : "Internal server error" issue with Lambda/API Gateway and iOS
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM