通过 AWS SDK 在 API 网关方法请求参数上启用缓存
Enable caching on API Gateway method request parameter via AWS SDK
我在 API 网关中有一个 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();
失败:
Invalid method setting path:
requestParameters/method.request.path.pawId/caching/enabled. Must be
one of: [.../metrics/enabled, .../logging/dataTrace,
.../logging/loglevel,.../throttling/burstLimit,
.../throttling/rateLimit, .../caching/ttlInSeconds,
.../caching/enabled, .../caching/dataEncrypted,
.../caching/requireAuthorizationForCacheControl,
.../caching/unauthorizedCacheControlHeaderStrategy]
这很奇怪,因为 .../caching/enabled
是它的选项之一 "must be"!
如何通过 SDK 对我的请求参数启用缓存?
我的理解是您不能直接在暂存 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.pawId
或
integration.request.header.pawId
等,
找出确切路径和需要使用哪些方法的最简单方法之一是首先调用它们各自的 getStage
、getIntegration
、getMethod
并研究响应。
我在 API 网关中有一个 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();
失败:
Invalid method setting path: requestParameters/method.request.path.pawId/caching/enabled. Must be one of: [.../metrics/enabled, .../logging/dataTrace, .../logging/loglevel,.../throttling/burstLimit, .../throttling/rateLimit, .../caching/ttlInSeconds, .../caching/enabled, .../caching/dataEncrypted, .../caching/requireAuthorizationForCacheControl, .../caching/unauthorizedCacheControlHeaderStrategy]
这很奇怪,因为 .../caching/enabled
是它的选项之一 "must be"!
如何通过 SDK 对我的请求参数启用缓存?
我的理解是您不能直接在暂存 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.pawId
或
integration.request.header.pawId
等,
找出确切路径和需要使用哪些方法的最简单方法之一是首先调用它们各自的 getStage
、getIntegration
、getMethod
并研究响应。