0

我有一个 AWS API 网关,我可以将数据发布到该网关 - 数据被放入 Kinesis Data Stream 以传送到各种数据存储。我想捕获用户代理并将其附加到数据中。由于我已经以特定格式收集数据,因此我不想过多地更改格式 - 而不是将数据嵌套到父对象中,我只想在数据上添加另一个键/值用户代理的对象。

到目前为止,我的映射模板如下所示:

#set($payload = "$input.json('$')")
#set($payload.UserAgent = "$context.identity.userAgent")
{
   "StreamName": "<stream-name>",
   "Data": "$util.base64Encode($payload)",
   "PartitionKey": "$context.identity.sourceIp",
   "UserAgent": "$context.identity.userAgent"

}

当我使用 AWS API Gateway 测试实用程序对其进行测试时,我输入了一个带有类似 的请求,{ "foo": "bar" }结果显示:

Thu Jun 03 20:31:53 UTC 2021 : Endpoint request body after transformations: {
   "StreamName": "<stream-name>",
   "Data": "eyJmb28iOiJiYXIifQ==",
   "PartitionKey": "test-invoke-source-ip",
   "UserAgent": "aws-internal/3 aws-sdk-java/1.11.1014 Linux/5.4.102-52.177.amzn2int.x86_64 OpenJDK_64-Bit_Server_VM/25.292-b10 java/1.8.0_292 vendor/Oracle_Corporation cfg/retry-mode/legacy"

}

"eyJmb28iOiJiYXIifQ=="解码时为{ "foo": "bar" },因此未设置用户代理。但我知道这"$context.identity.userAgent"不是空的,因为它显示为UserAgent键,它只是没有附加到$payload对象上。

Apache Velocity Engine VTL 文档表明您应该能够修改对象的属性:

#set - Establishes the value of a reference
Format:

# [ { ] set [ } ] ( $ref = [ ", ' ] arg [ ", ' ] )

Usage:

$ref - The LHS of the assignment must be a variable reference or a property reference.
arg - The RHS of the assignment, arg is parsed (i.e. interpolated) if enclosed in double quotes, and not parsed if enclosed in single quotes. If the RHS evaluates to null, it is not assigned to the LHS.
...
Property reference: #set( $monkey.Blame = $whitehouse.Leak )

知道这里发生了什么吗?

4

0 回答 0