API Gateway的正文模板在映射时如何保留数据元素的有效JSON数据结构,同时仍允许base64-encoding?

0

【以下的问题经过翻译处理】 我们目前正在根据该文档来创建面向Amazon Kinesis的(http://docs.aws.amazon.com/apigateway/latest/developerguide/integrating-api-with-aws-services-kinesis.html#api-gateway-get-and-add-records-to-stream) 客户想要在使用PutRecord和PutRecords API时支持以下载荷结构(数据元素须以json格式传递): {     "records": [         {             "data": <json>,             "PartitionKey": <String>         },         {             "data": <json>,             "PartitionKey": <String>         }     ] } 例如: { "records": [ { "data": { "Events": [ {"DSType":"RTDS","DSInstance":"Unit 1","DSPoint":"Tag 1","TimeStamp":"2017-10-10T11:10:00.0000000+02:00","Value":8.0,"Quality":192}, {"DSType":"RTDS","DSInstance":"Unit 2","DSPoint":"Tag 2","TimeStamp":"2017-10-10T11:10:00.0000000+02:00","Value":7.0,"Quality":193} ], "Project":"EventAcquisitionStream", "Plant":"Plant1" }, "partition-key": "some key" }, { "data": { "Events": [ {"DSType":"RTDS","DSInstance":"Unit 3","DSPoint":"Tag 3","TimeStamp":"2017-10-11T11:10:00.0000000+02:00","Value":8.0,"Quality":192}, {"DSType":"RTDS","DSInstance":"Unit 4","DSPoint":"Tag 4","TimeStamp":"2017-10-10T11:10:00.0000000+02:00","Value":7.0,"Quality":193} ], "Project":"EventAcquisitionStream", "Plant":"Plant2" }, "partition-key": "some key" } ] } 根据AWS文档中的说明,我们为PutRecords模型配置了如下载荷模型以及请求模版: 模型: { "$schema": "http://json-schema.org/draft-04/schema#", "title": "PutRecords proxy payload data model", "type": "object", "properties": { "records": { "type": "array", "items": { "type": "object", "properties": { "data": { "type": "object", "properties": { "events": { "type": "array", "items": { "type": "object", "properties": { "dstype": { "type": "string" }, "dsinstance": { "type": "string" }, "dspoint": { "type": "string" }, "timeStamp": { "type": "string" }, "value": { "type": "number" }, "quality": { "type": "integer" } } } }, "project": { "type": "string" }, "plant": { "type": "string" } } }, "partition-key": { "type": "string" } } } } } } 正文模版映射: { "StreamName": "$input.params('stream-name')", "Records": [ #foreach($elem in $input.path('$.records')) { "Data": "$util.base64Encode($elem.data)", "PartitionKey": "$elem.partition-key" }#if($foreach.hasNext),#end #end ] } 问题是,在该结构在kineses接收端被解码后,$elem.data中的内容会被转码成无效的json,比如在Events,Project,Planet元素中将: 替换成=后使得json无效: Decoded payload: {Events=[{"DSType":"RTDS","DSInstance":"Unit 1","DSPoint":"Tag 1","TimeStamp":"2017-10-10T11:10:00.0000000+02:00","Value":8.0,"Quality":192},{"DSType":"RTDS","DSInstance":"Unit 2","DSPoint":"Tag 2","TimeStamp":"2017-10-10T11:10:00.0000000+02:00","Value":7.0,"Quality":193}], Project=EventAcquisitionStream, Plant=Plant1} 请问我该怎么配置我的请求正文映射模版以保证json数据结构有效,与此同时允许base64编码机制?

1 回答
0

【以下的回答经过翻译处理】 请参考如下方案: { "StreamName": "event-stream-qa", "Records": [ #set($datakey = ".data") #set($partitionkey = ".key") #foreach($elem in $input.path('$.records')) #set($pathBegin = "$.records[") #set($pathEnd = "]") #set($currentIndex = $foreach.index) #set($dataPath = "$pathBegin$currentIndex$pathEnd$datakey") #set($partitionPath = "$pathBegin$currentIndex$pathEnd$partitionkey") #set($dataJson = $input.json($dataPath)) #set($partitionJson = $input.json($partitionPath)) { "Data": "$util.base64Encode($dataJson)", "PartitionKey": $partitionJson } #if($foreach.hasNext),#end #end
] }

profile picture
专家
已回答 7 年前

您未登录。 登录 发布回答。

一个好的回答可以清楚地解答问题和提供建设性反馈,并能促进提问者的职业发展。

回答问题的准则