Cloudfront异常“缺少If-Match版本或对该资源无效”更新发行版时

问题描述

使用AWS Node SDK更新Cloudfront发行版时,出现以下异常:

  message:
   'The If-Match version is missing or not valid for the resource.',code: 'InvalidIfMatchVersion',time: 2020-08-23T19:37:22.291Z,requestId: '43a7707f-7177-4396-8013-4a704b7b11ea',statusCode: 400,retryable: false,retryDelay: 57.05741843025996 }

我在做什么错了?

解决方法

发生此错误的原因是,您需要首先使用cloudfront.getDistributionConfig获取当前的发行配置,然后将ETagDistributionConfig字段复制到您的cloudfront.updateDistribution调用中。您可以根据需要修改DistributionConfig,以实现您要执行的实际配置更新。

文档在解释这一点上做得很差(说明仅针对REST API,而不是您使用的实际SDK)。

以下是通过拉动最新的分发配置,对其进行修改然后对其执行更新来更新要禁用的Cloudfront分发的示例:

async function disable_cloudfront_distribution(dist_id) {
    // We need to pull the previous distribution config to update it.
    const previous_distribution_config = await cloudfront.getDistributionConfig({
        Id: dist_id
    }).promise();
    const e_tag = previous_distribution_config.ETag;

    // Update config to be disabled
    previous_distribution_config.DistributionConfig.Enabled = false;

    // Create update distribution request with distribution ID
    // and the copied config along with the returned etag for ifmatch.
    const params = {
        Id: dist_id,DistributionConfig: previous_distribution_config.DistributionConfig,IfMatch: e_tag
    };

    return cloudfront.updateDistribution(params).promise();
}

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...