dynamodb映射的AWS API网关主体映射模板 [英] AWS API gateway body mapping template for dynamodb map
问题描述
我试图弄清楚如何通过AWS中的API网关将动态数据插入dynamodb表中。目前,我有一个dynamodb表和一个接受此类POST的API端点设置。
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网关正文映射模板语法将事件属性保存到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->的AttributeValue
。不幸的是,您无法传递原始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.
一种可能的解决方法是对进行字符串化事件
对象并将其写为类型 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屋!