Python 3.x 无法通过API网关将对象解析为事件以从Lambda获取响应?

Python 3.x 无法通过API网关将对象解析为事件以从Lambda获取响应?,python-3.x,amazon-web-services,aws-lambda,aws-api-gateway,aws-serverless,Python 3.x,Amazon Web Services,Aws Lambda,Aws Api Gateway,Aws Serverless,我不熟悉无服务器开发。我正在尝试使用aws Lambda和api网关创建一个简单的rest api。 我的s3中有以下json,我希望在调用API时根据请求返回对象 { "customerA": {"Age": "29", "Product": "Laptop"}, "customerB": { "Age": "30", "Product": "Mobile" } } 下面是lambda函数,我为它添加了相同的API触发器 import json import boto3 de

我不熟悉无服务器开发。我正在尝试使用aws Lambda和api网关创建一个简单的rest api。 我的s3中有以下json,我希望在调用API时根据请求返回对象

{
"customerA":
 {"Age": "29", "Product": "Laptop"},
 "customerB": {
 "Age": "30", 
 "Product": "Mobile" 
}
}
下面是lambda函数,我为它添加了相同的API触发器

import json
import boto3


def customer(event, context):
    # TODO implement
    resource='s3'
    s3=boto3.resource(resource)
    s3Bucket='mys3'
    bucketKey='customerDetails.json'
    obj=s3.Object(s3Bucket,bucketKey)
    body=obj.get()['Body'].read()
    customer={}
    customer=json.loads(body)
    value={}
    # customerCode is the parameter defined in your serverless.yml
    customerCode = event['custCode']

    return {
        'statusCode': 200,
        'body': json.dumps(customer[customerCode]),
        'headers': {
        "content-type": "application/json"
    }
}


预期:iam所寻找的是,当我使用请求'customerA'调用api时,api应该返回'customerA'的对象:'{“年龄”:“29”,“产品”:“笔记本电脑”}' 那么,如何通过请求调用API以获得上述响应,如下所示 当我调用api时 如果iam仅调用,则返回所有值

 "body': json.dumps(customer)"
使用下面的api

test-api.eu-west-1.amazonaws.com/customer_1/cust-details/
test-api.eu-west-1.amazonaws.com/customer_1/cust-details/customerA
但是当我用下面的api调用

test-api.eu-west-1.amazonaws.com/customer_1/cust-details/
test-api.eu-west-1.amazonaws.com/customer_1/cust-details/customerA
它抛出了关键错误

我的预期结果是

{"Age": "29", "Product": "Laptop"}
有人能帮忙吗?

这应该行得通

import json
import boto3


def hello(event, context):
    # TODO implement
    resource='s3'
    s3=boto3.resource(resource)
    s3Bucket='bucketname'
    bucketKey='customer.json'
    obj=s3.Object(s3Bucket,bucketKey)
    body=obj.get()['Body'].read()
    customer={}
    customer=json.loads(body)
    value={}
    # customerCode is the parameter defined in your serverless.yml
    customerCode = event['pathParameters']['customerCode']

    return {
        'statusCode': 200,
        'body': json.dumps(customer[customerCode]),
        'headers': {
            "content-type": "application/json"
        }
    }
serverless.yml 希望这有帮助,这应该行得通

import json
import boto3


def hello(event, context):
    # TODO implement
    resource='s3'
    s3=boto3.resource(resource)
    s3Bucket='bucketname'
    bucketKey='customer.json'
    obj=s3.Object(s3Bucket,bucketKey)
    body=obj.get()['Body'].read()
    customer={}
    customer=json.loads(body)
    value={}
    # customerCode is the parameter defined in your serverless.yml
    customerCode = event['pathParameters']['customerCode']

    return {
        'statusCode': 200,
        'body': json.dumps(customer[customerCode]),
        'headers': {
            "content-type": "application/json"
        }
    }
serverless.yml
希望这对您将API网关配置为Lambda代理有所帮助。进行此配置时,会有一个约定,强制Lambda函数返回JSON对象,如下所示:

{
    statusCode: 200,
    body: "{\"Age\": \"29\", \"Product\": \"Laptop\"}",
    headers: {
        "content-type": "application/json"
    }
}

您已将API网关配置为Lambda代理。进行此配置时,会有一个约定,强制Lambda函数返回JSON对象,如下所示:

{
    statusCode: 200,
    body: "{\"Age\": \"29\", \"Product\": \"Laptop\"}",
    headers: {
        "content-type": "application/json"
    }
}

你好,阿伦,谢谢你的重播。我所寻找的是整合事件请求并返回如下所示的对象,然后它应该在主体“{”年龄“:“29”,“产品“:“膝上型电脑”}中返回响应。我已经修改了这个问题。我现在明白这是一个错误,因此我已经再次更新了答案。我仍然没有在“customerCode=event”中输入什么['pathParameters']['customerCode']'部分。你能给我一个提示吗?值是
customerA
。请看URLHi@Arun的结尾部分,谢谢你的重播。iam正在寻找的是整合事件请求并返回如下所示的对象,然后它应该返回正文“{”Age”中的响应:“29”,“产品”:“笔记本电脑”}我已经修改了这个问题。我现在明白了,这是一个错误,我已经在@Arun更新了答案。我仍然不知道在“customerCode=event['pathParameters']['customerCode']]部分输入什么。你能给我一个提示吗?这个值是
customerA
。看看URL的最后部分