如何在API网关映射模板中允许可选密钥

问题描述 投票:0回答:1

我有一个API网关,可以扫描DynamoDB表。我想在请求正文中传递LastEvaluatedKey;如果我通过LastEvaluatedKey,则一切正常,并且得到了包含预期数据的响应-所以我就在那儿了。

但是,当然,我第一次向API LastEvaluatedKey发送请求将不存在,因此映射模板中期望ExclusiveStartKeyLastEvaluatedKey必须是可选的。我尝试了几种不同的方法来使它成为可选的,但是到目前为止没有任何效果。这是我所拥有的:

#set($hasName = $input.json('$.Name'))
{
    "TableName": "MyTable",
    #if($hasName && $hasName.length() != 0)
    "ExclusiveStartKey": {
        "Name": {
            "S": $input.json('$.Name')
        },
        "Date": {
            "S": $input.json('$.Date')
        }
    },#end
    "FilterExpression": "begins_with(#dt, :tdt)",
    "ExpressionAttributeNames": {
        "#dt": "Date"
    },
    "ExpressionAttributeValues": {
        ":tdt": {
            "S": "$input.params('date')"
        }
    }
}

正如我说的,当我do在我的请求正文中传递LastEvaluatedKey时,以上方法起作用,但是当我没有得到错误时:

{
    "__type": "com.amazon.coral.validate#ValidationException",
    "message": "The provided starting key is invalid: One or more parameter values were invalid: An AttributeValue may not contain an empty string"
}

...仍然期望LastEvaluatedKey

[如果Name,我也尝试将Date#if包裹在里面,但是没有运气。我从其他答案中得到了启发,例如:thisthis,但没有运气。

json amazon-web-services amazon-dynamodb aws-api-gateway mime-types
1个回答
0
投票

在我的示例中,“光标”是LastEvaluatedKey:

#set($hasCursor = $input.params('cursor') != "")
{
    "TableName": "my-table"
    #if($hasCursor) "ExclusiveStartKey":$util.base64Decode("$input.params('cursor')")
    #end
}

[它只是检查'cursor'是否作为查询参数传递(例如/ my / api?cursor = myencodedlastevaluatedkey),并且仅在请求中添加ExclusiveStartKey。

© www.soinside.com 2019 - 2024. All rights reserved.