无法通过 API 网关将对象解析为事件以获取 Lambda 的响应?

Unable to parse object to the event through API Gateway to get response from Lambda?

我是无服务器开发的新手。我正在尝试使用 aws Lambda 和 API 网关创建一个简单的 rest api。 我在我的 s3 中有以下 json,我想在调用 API 时根据请求 return 对象

{
"customerA":
 {"Age": "29", "Product": "Laptop"},
 "customerB": {
 "Age": "30", 
 "Product": "Mobile" 
}
}

下面是我添加了相同 API 触发器的 lambda 函数

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"
    }
}



service: customer-function

provider:
  name: aws
  runtime: python3.8
  region: eu-west-1
  iamRoleStatements:
  -  Effect: "Allow"
     Action:
       - "s3:ListBucket"
     Resource: "arn:aws:s3:::mys3"

  -  Effect: "Allow"
     Action:
       - "s3:PutObject"
       - "s3:GetObject"
       - "s3:DeleteObject"
     Resource: "arn:aws:s3:::mys3/*"

functions:
  customer_serverless:
    handler: handler.customer
#    The following are a few example events you can configure
#    NOTE: Please make sure to change your handler code to work with those events
#    Check the event documentation for details
    events:
      - http:
          path: customer_resource/cust_content/
          method: get


预期:我正在寻找的是,当我用请求 'customerA' 调用 api 时,api 应该 return [=37 的对象=] : '{"Age": "29", "Product": "Laptop"}' 那么如何通过请求调用 API 以获得上述响应,如下所示 当我调用 api 它 return 所有值如果我只调用

 "body': json.dumps(customer)"

以下api

test-api.eu-west-1.amazonaws.com/customer_1/cust-details/

但是当我用下面的方法调用时 api

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

service: customer-function

provider:
  name: aws
  runtime: python3.8
  region: eu-west-1
  iamRoleStatements:
  -  Effect: "Allow"
     Action:
       - "s3:ListBucket"
     Resource: "arn:aws:s3:::bucketname"

  -  Effect: "Allow"
     Action:
       - "s3:PutObject"
       - "s3:GetObject"
       - "s3:DeleteObject"
     Resource: "arn:aws:s3:::bucketname/*"

functions:
  hello:
    handler: handler.hello
#    The following are a few example events you can configure
#    NOTE: Please make sure to change your handler code to work with those events
#    Check the event documentation for details
    events:
      - http:
          path: customer_1/cust-details/{customerCode}
          method: get

希望这对您有所帮助

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

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