【问题标题】:Amazon CloudFront Versioning 'index.html'Amazon CloudFront 版本控制“index.html”
【发布时间】:2015-07-23 15:32:06
【问题描述】:

我在 S3 和 CloudFront 中部署了一个 AngularJS 应用程序。我通过 Grunt & Jenkins 的构建过程包括一个 FileRev 步骤,用于唯一命名我的脚本和供应商 JS 文件的每个新版本。最后,FileRev 还更新了我的“index.html”页面标签,以引用我的脚本和供应商文件的最新版本。

一切都很好,除了...

如何让 CloudFront 在我的所有边缘站点中立即使“index.html”无效,而不是以编程方式在每个版本上创建新的无效??

谢谢!

【问题讨论】:

  • 如果您不想失效,您也可以为每个版本设置 index.html 版本。在这种情况下,在每个版本中,您的负载均衡器或代理都需要指向新的 index.html,我只会在 index.html 上调用 invalidate 来更新所有其他 ui 资源的版本,而不是做所有这些马戏团。
  • 您可以将 Jenkins 配置为使用 AWS CLI 进行调用,以使 CloudFront docs.aws.amazon.com/cli/latest/reference/cloudfront/… 中的 index.html 文件无效
  • @EarlD 你是怎么解决的?
  • 我没有解决。在每次成功构建应用程序到生产环境后,我只是使用 AWS CLI 创建一个失效。
  • 看看我的answer here

标签: amazon-s3 amazon-cloudfront


【解决方案1】:

您可以通过以下方式以编程方式执行此操作。这应该是部署脚本的一部分。我们只会使index.html 失效,因为我们已经通过文件名对其他资源进行了版本控制:

const aws = require('aws-sdk')

function invalidateIndex () {
  const client = new aws.CloudFront({
    accessKeyId: process.env.AWS_ACCESS_ID,
    secretAccessKey: process.env.AWS_SECRET_ACCESS_KEY,
  })
  const invalidation = client.createInvalidation({
    DistributionId: process.env.AWS_CLOUDFRONT_DISTRIBUTION_ID, /* required */
    InvalidationBatch: {
      /* required */
      CallerReference: Date.now() + '', /* required - request ID given by you, any string is okay*/
      Paths: {
        /* required */
        Quantity: 1, /* required */
        Items: [
          '/',
          /* more items */
        ]
      }
    }
  }, function (err, data) {
    if (err) console.log(err, err.stack); // an error occurred
    else     console.log('Index was invalidated with invalidation id: ', data.Invalidation.Id);           // successful response
  })
}

invalidateIndex()

您可以在此处阅读更多 API 文档:http://docs.aws.amazon.com/AWSJavaScriptSDK/latest/AWS/CloudFront.html#createInvalidation-property

【讨论】:

  • 项目不需要指定/index.html吗?很棒的帖子,谢谢。
  • @Learner 这是由Items: ['/']处理的
猜你喜欢
  • 1970-01-01
  • 2013-01-10
  • 2012-09-16
  • 2016-05-06
  • 1970-01-01
  • 2017-03-03
  • 2011-11-17
  • 2019-11-23
  • 2015-06-10
相关资源
最近更新 更多