我在 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"
}
您的请求模型仅适用于事件
的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/