【问题标题】:Enable caching on API Gateway method request parameter via AWS SDK通过 AWS SDK 在 API Gateway 方法请求参数上启用缓存
【发布时间】:2018-12-28 06:46:39
【问题描述】:

我在 API Gateway 中有一个 API,我想通过 AWS SDK 启用或禁用对请求参数的缓存。

方法是GET /cats。我正在使用updateStage 方法,并尝试了以下方法:

params = {
  restApiId: 'myRestApiId',
  stageName: 'myStageName',
  patchOperations: [
    {
      op: 'replace',
      path: '/~1cats/GET/requestParameters/method.request.header.pawId/caching/enabled'
    }];
await aws.updateStage(params).promise();

失败:

方法设置路径无效: requestParameters/method.request.path.pawId/caching/enabled。一定是 以下之一:[.../metrics/enabled, .../logging/dataTrace, .../logging/loglevel,.../节流/burstLimit, .../节流/rateLimit, .../缓存/ttlInSeconds, .../缓存/启用,.../缓存/数据加密, .../缓存/requireAuthorizationForCacheControl, .../缓存/unauthorizedCacheControlHeaderStrategy]

这很奇怪,因为.../caching/enabled 是它“必须”的选项之一!

如何通过 SDK 对我的请求参数启用缓存?

【问题讨论】:

    标签: amazon-web-services caching aws-sdk aws-api-gateway


    【解决方案1】:

    我的理解是您不能直接在暂存 API 上启用 requestParameters 缓存。您将需要更新 API 并将其再次部署到阶段。您在阶段级别看到的 cachingEnabled 选项用于启用对完整 API 响应(不是参数)的缓存。 https://docs.aws.amazon.com/apigateway/latest/developerguide/api-gateway-caching.html

    现在更新 API 以启用请求参数的缓存可以使用以下操作完成。

    var params = {
      httpMethod: 'GET',
      resourceId: 'xxxx', /* you will need to pass unique identifier which API gateway creates for /cats or /pets resources */
      restApiId: 'xxxxxxx', /* unique identifer for your API */
      patchOperations: [
        {
          op: 'add', /* add or remove only for enabling/disabling */
          path: '/cacheKeyParameters/method.request.header.pawId',
        },
        /* more items */
      ]
    };
    apigateway.updateIntegration(params, function(err, data) {
      if (err) console.log(err, err.stack); // an error occurred
      else     console.log(data);           // successful response
    });
    

    注意 - 路径会根据您启用参数缓存的位置而变化。 例子 - method.request.header.pawIdintegration.request.header.pawId等,

    找出确切路径和需要使用哪些方法的最简单方法之一是先调用它们各自的getStagegetIntegrationgetMethod 并研究响应。

    【讨论】:

    • 你说得对,谢谢。我正在使用带有代理 lambda 函数的无服务器。我在 API Gateway 控制台中进行了更改,但它们只有在使用无服务器部署后才有效(即使没有其他更改)。
    猜你喜欢
    • 2017-10-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-07-23
    • 2019-05-27
    • 2018-07-30
    • 1970-01-01
    • 2017-04-24
    相关资源
    最近更新 更多