python-3.x - API 网关 - Lambda 代理 - Python 内部服务器错误

标签 python-3.x aws-lambda

我在 Python 3.7 中解析来自事件的输入数据时遇到问题。

def lambda_handler(event, context):
 image = event['image']
 siteid = int(event['siteid'])
 camid = int(event['camid'])

错误:

Lambda execution failed with status 200 due to customer function error: 'image'.

方法请求模型:

{
  "$schema": "http://json-schema.org/draft-04/schema#",
  "title": "UploadModel",
  "type": "object",
  "properties": {
    "image": { "type": "string" },
    "siteid": { "type": "string" },
    "camid": { "type": "string" }
    }
}

使用 Lambda 代理集成:开启

它直接从 lambda 控制台使用一个简单的输入数组就可以正常工作:

{
    "image": "xxxx"
    "siteid": 2,
    "camid": 1
}

响应函数:

def response(message, status_code):
    return {
        "statusCode": str(status_code),
        "body": json.dumps(message),
        "headers": {
            "Content-Type": "application/json",
            "Access-Control-Allow-Origin": '*'
            },
        }

最佳答案

您为 event 对象假设了错误的形状。

当您使用 Lambda 代理集成时,事件 具有以下形状...

{
    "resource": "Resource path",
    "path": "Path parameter",
    "httpMethod": "Incoming request's method name"
    "headers": {String containing incoming request headers}
    "multiValueHeaders": {List of strings containing incoming request headers}
    "queryStringParameters": {query string parameters }
    "multiValueQueryStringParameters": {List of query string parameters}
    "pathParameters":  {path parameters}
    "stageVariables": {Applicable stage variables}
    "requestContext": {Request context, including authorizer-returned key-value pairs}
    "body": "A JSON string of the request payload."
    "isBase64Encoded": "A boolean flag to indicate if the applicable request payload is Base64-encode"
}

引用:https://docs.aws.amazon.com/apigateway/latest/developerguide/set-up-lambda-proxy-integrations.html#api-gateway-simple-proxy-for-lambda-input-format

您的请求模型仅适用于事件body

为了说明这一点,尝试使用返回 event 作为响应的处理程序:

def lambda_handler(event, context):
    return {
        "statusCode": str(status_code),
        "body": json.dumps(message),
        "headers": {
            "Content-Type": "application/json",
            "Access-Control-Allow-Origin": '*'
            },
        }

关于python-3.x - API 网关 - Lambda 代理 - Python 内部服务器错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53686252/

相关文章:

python - 属性错误: module 'pymysql.constants.ER' has no attribute 'CONSTRAINT_FAILED' in pymysql Python

python - 使用子进程 popen 运行列表命令字符串并获取输出

python - API网关+Lambda+Python : Handling Exceptions

node.js - 将生成的 PNG 从 lambda 上的 Node 上传到亚马逊 S3

amazon-web-services - CloudFormation - 无法创建 Lambda 函数

python - python 中描述符概念的行为(令人困惑)

python - 如何修复 Anaconda linter 在 Sublime Text 3 中显示 f 字符串错误?

python - 如何使用 Python 2.7 在 AWS Lambda 中编写 hello world 示例?

aws-lambda - 为什么我的 Lambda 函数连接到 SES VPC Endpoint 会超时?

mysql - python3 mysql 连接保持事件状态多长时间?