gpt4 book ai didi

amazon-dynamodb - Api网关获取item dynamodb配置

转载 作者:行者123 更新时间:2023-12-03 08:45:49 24 4
gpt4 key购买 nike

您好,我需要通过 api 网关从 dynamodb 获取一个项目,并且我配置如下资源:

resources screenshot

我配置集成方法如下图所示:

enter image description here

映射模板是这样的: enter image description here

但是当我测试 apigateway 启动时出现此错误

Execution failed due to configuration error:
No match for output mapping and no default output mapping configured.
Endpoint Response Status Code: 200
Gateway response type: API_CONFIGURATION_ERROR with status code: 500

最佳答案

我按照这两个教程对我的 dynamoDB 表执行了 get 操作。

我的表有主键名称“pk”。教程:Video & Blog

  1. 如果我像你一样编写发电机请求正文,它不会获取任何记录
{
"TableName": "apiG",
"Key": {
"pk": {
"S": "key1"
}
}
}

但是如果我像这样提出请求

{
"TableName": "apiG",
"PrimaryKey": "pk",
"KeyConditionExpression": "pk = :v1",
"ExpressionAttributeValues": {
":v1": {
"S": "key1"
}
}
}

我从 dynamoDB 得到了所需的响应。

  • 您的错误看起来像是您的集成响应和方法响应存在一些混淆。在最简单的形式中,将您的方法响应保留为默认值,即“HTTP Status: 200 Models: application/json => Empty”,并保留您的集成响应,因为 API Gateway 控制台会为您创建它。
  • 这将确保您的 DynamoDB 输出在不修改的情况下发送到您的浏览器/输出。

  • 我在集成响应中使用以下映射模板转换了发电机输出。
  • #set($inputRoot = $input.path('$'))
    {
    "content": [
    #foreach($elem in $inputRoot.Items) {
    "key": "$elem.pk.S",
    "value": "$elem.pv.S"
    }#if($foreach.hasNext),#end
    #end
    ]
    }

    它产生了以下输出。

    {
    "content": [
    {
    "key": "key1",
    "value": "val1"
    }
    ]
    }

    附注我有一个名为“apiG”的发电机表,其主键为“pk”,并遵循我的实验中导出的 swagger。希望对您有所帮助。

    openapi: "3.0.1"
    info:
    title: "dynamoProxy"
    version: "2020-05-01T06:45:38Z"
    servers:
    - url: "https://aaaaaaaaaa.execute-api.us-east-2.amazonaws.com/{basePath}"
    variables:
    basePath:
    default: "/test"
    paths:
    /db:
    get:
    responses:
    200:
    description: "200 response"
    content:
    application/json:
    schema:
    $ref: "#/components/schemas/Empty"
    x-amazon-apigateway-integration:
    credentials: "arn:aws:iam::111111111111:role/apiGddbRole"
    uri: "arn:aws:apigateway:us-east-2:dynamodb:action/Query"
    responses:
    default:
    statusCode: "200"
    responseTemplates:
    application/json: "#set($inputRoot = $input.path('$'))\n{\n \"content\"\
    : [\n #foreach($elem in $inputRoot.Items) {\n \"\
    key\": \"$elem.pk.S\",\n \"value\": \"$elem.pv.S\"\n \
    \ }#if($foreach.hasNext),#end\n\t#end\n ]\n}"
    passthroughBehavior: "when_no_templates"
    httpMethod: "POST"
    requestTemplates:
    application/json: "{\n \"TableName\": \"apiG\",\n \"PrimaryKey\":\
    \ \"pk\",\n \"KeyConditionExpression\": \"pk = :v1\",\n \"ExpressionAttributeValues\"\
    : {\n \":v1\": {\n \"S\": \"key1\"\n }\n }\n\
    }"
    type: "aws"
    components:
    schemas:
    Empty:
    title: "Empty Schema"
    type: "object"

    干杯!

    关于amazon-dynamodb - Api网关获取item dynamodb配置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61534155/

    24 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com