dynamodb 地图的 AWS API 网关主体映射模板 [英] AWS API gateway body mapping template for dynamodb map
问题描述
我试图弄清楚如何通过 AWS 中的 API 网关将动态数据插入到 dynamodb 表中.目前我有一个 dynamodb 表和一个 API 端点设置,可以接受这样的 POST.
I am trying to figure out how to insert dynamic data into a dynamodb table via an API Gateway in AWS. Currently I have a dynamodb table and an API endpoint setup that accepts a POST like so.
POST https://{unique-id}.execute-api.us.east-1.amazonaws.com/notification/events
POST https://{unique-id}.execute-api.us.east-1.amazonaws.com/notification/events
{
"reference_number": 99,
"purchase_date": "1/1/2017"
}
我在 API 网关中设置了一个身体映射模板,以将数据按摩到 dynamodb 中.
I've setup a body mapping template in the API gateway to massage the data into the dynamodb.
{
"TableName": "Events",
"Item": {
"reference_number": {
"N": "$input.path('$.reference_number')"
},
"purchase_date": {
"S": "$input.path('$.purchase_date')"
}
}
}
上述工作并保存到表中.
The above works and saves to the table.
假设我将事件哈希添加到我的 json 中(可以根据事件进行更改).
Suppose I add the event hash to my json (which can change based on events).
{
"reference_number": 99,
"purchase_date": "1/1/2017",
"event": {
"name": "purchase",
"items": [1,3,6],
"info": {
"currencyID": "USD",
"countryID": "US"
}
}
}
如何使用 API Gateway Body 映射模板语法将事件属性保存到 dynamodb 中的 Map?
How do I save the event attribute to a Map in dynamodb using the API Gateway Body mapping template syntax?
{
"TableName": "Events",
"Item": {
"reference_number": {
"N": "$input.path('$.reference_number')"
},
"purchase_date": {
"S": "$input.path('$.purchase_date')"
},
"event":{
"M": "$input.path('$.event')"
}
}
}
上面的模板给了我以下错误."预期地图或为空"
The above template gives me the following error. "Expected map or null"
推荐答案
看起来 DynamoDB API 实际上要求 'M' 属性的值是 String 的 Map ->属性值
.不幸的是,您无法传递原始 JSON.您必须手动映射整个 event
对象才能使 DDB API 满意.
It looks like DynamoDB API actually requires the value of an 'M' attribute to be a Map of String -> AttributeValue
. Unfortunately you can't pass the raw JSON. You'll have to manually map the whole event
object to make the DDB API happy.
一种可能的解决方法是将 event
对象字符串化并将其写入类型 S
但这当然需要读者期待这种行为.
One possible workaround would be to stringify the event
object and write it as type S
but that would of course require the reader to expect that behavior.
{
"TableName": "Events",
"Item": {
"reference_number": {
"N": "$input.path('$.reference_number')"
},
"purchase_date": {
"S": "$input.path('$.purchase_date')"
},
"event":{
"S": "$util.escapeJavaScript($input.json('$.event'))"
}
}
}
这篇关于dynamodb 地图的 AWS API 网关主体映射模板的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!