AWS API 网关 - 如何在 body 映射模板中获取 date/timestamp/epoch?

AWS API Gateway - How do I get the date/timestamp/epoch in a body mapping template?

我需要在 body 映射模板中为 API 网关方法包含请求时间。是否有 date/time 变量或函数?我在 template reference.

中找不到任何内容

示例 body 映射模板:

Action=SendMessage&MessageBody=$util.urlEncode("{""timestamp"":""TIMESTAMP_HERE"",""body-json"":$input.json('$'),""params"":""$input.params()""}")

更新: API Gateway 刚刚添加了两个新的上下文变量 http://docs.aws.amazon.com/apigateway/latest/developerguide/api-gateway-mapping-template-reference.html

$context.requestTime    The CLF-formatted request time (dd/MMM/yyyy:HH:mm:ss +-hhmm).
$context.requestTimeEpoch   The Epoch-formatted request time.

API 网关目前不支持此功能。它之前在论坛上被请求过 - https://forums.aws.amazon.com/thread.jspa?messageID=697658&。我们的积压工作中有此功能,但遗憾的是我无法承诺任何时间表。

似乎有一种方法可以从 API 网关请求中获取时间戳,但您需要查看 the X-Ray documentation 才能找到它:

Amazon API Gateway gateways add a trace ID to incoming HTTP requests in a header named X-Amzn-Trace-Id.

(...)

A trace_id consists of three numbers separated by hyphens. For example, 1-58406520-a006649127e371903a2de979. This includes:

  • The version number, that is, 1.
  • The time of the original request, in Unix epoch time, in 8 hexadecimal digits. For example, 10:00AM December 2nd, 2016 PST in epoch time is 1480615200 seconds, or 58406520 in hexadecimal.

(...)

只需在模板顶部设置一个时间值作为您希望它过期的日期。然后将其作为属性添加到您的项目中。例如,如果您希望项目在 60 天后过期,您将添加 5184000 秒。

#set($expireDate = $context.requestTimeEpoch + 5184000) 
{
  "TableName": "your-table-name",
  "Item": {
      ... other attributes
      "expireDate": {"N": "$expireDate"},
  }
}