dynamodb映射的AWS API网关主体映射模板 [英] AWS API gateway body mapping template for dynamodb map

查看:74
本文介绍了dynamodb映射的AWS API网关主体映射模板的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图弄清楚如何通过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.

http://docs.aws.amazon.com/ amazondynamodb / latest / APIReference / API_AttributeValue.html#DDB-Type-AttributeValue-M

一种可能的解决方法是对进行字符串化事件对象并将其写为类型 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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆