强制 CloudFront 分发/文件更新

发布于 2024-08-01 21:30:22 字数 191 浏览 6 评论 0原文

我正在使用 Amazon 的 CloudFront 来提供 Web 应用程序的静态文件。

是否没有办法告诉云前端发行版它需要刷新其文件或指出应该刷新的单个文件?

亚马逊建议您对 logo_1.gif、logo_2.gif 等文件进行版本控制,作为解决此问题的方法,但这似乎是一个非常愚蠢的解决方案。 难道就没有别的办法了吗?

I'm using Amazon's CloudFront to serve static files of my web apps.

Is there no way to tell a cloudfront distribution that it needs to refresh it's file or point out a single file that should be refreshed?

Amazon recommend that you version your files like logo_1.gif, logo_2.gif and so on as a workaround for this problem but that seems like a pretty stupid solution. Is there absolutely no other way?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(13

人事已非 2024-08-08 21:30:22

5 分钟内完成自动更新设置

好的,伙计们。 目前执行自动 CloudFront 更新(失效)的最佳方法是创建 Lambda 函数,每次将任何文件上传到 S3 存储桶(新的或重写的)时都会触发该函数。

即使您以前从未使用过 lambda 函数,这也非常简单 - 只需按照我的分步说明进行操作,只需 5 分钟:

第 1 步

转到 https://console.aws.amazon.com/lambda/home 并单击创建 lambda 函数

第 2 步

单击空白功能(自定义)

第 3 步

单击空白(描边)框并选择S3 从组合中

第 4 步

选择您的存储桶(与 CloudFront 分配相同)

第 5 步

事件类型设置为“已创建对象(全部)”

第 6 步

设置前缀和后缀,如果您不知道它是什么,请将其留空。

第 7 步

选中启用触发器复选框,然后单击下一步

第 8 步

为您的函数命名(类似于:YourBucketNameS3ToCloudFrontOnCreateAll )

第 9 步

选择 Python 2.7(或更高版本)作为运行时

第 10 步

粘贴以下代码默认 python 代码:

from __future__ import print_function

import boto3
import time

def lambda_handler(event, context):
    for items in event["Records"]:
        path = "/" + items["s3"]["object"]["key"]
        print(path)
        client = boto3.client('cloudfront')
        invalidation = client.create_invalidation(DistributionId='_YOUR_DISTRIBUTION_ID_',
            InvalidationBatch={
            'Paths': {
            'Quantity': 1,
            'Items': [path]
            },
            'CallerReference': str(time.time())
            })

第 11 步

打开 https://console.aws。在新的浏览器选项卡中访问 amazon.com/cloudfront/home 并复制您的 CloudFront 分配 ID 以供下一步使用。

第 12 步

返回 lambda 选项卡并粘贴您的分发 ID,而不是 Python 代码中的 _YOUR_DISTRIBUTION_ID_。 保留周围的引号。

第 13 步

设置处理程序:lambda_function.lambda_handler

第 14 步

单击角色组合框并选择创建自定义角色。 将打开浏览器中的新选项卡。

第15步

点击查看策略文档,点击编辑,点击确定并将角色定义替换为以下内容(按原样) :

{
  "Version": "2012-10-17",
  "Statement": [
    {
      "Effect": "Allow",
      "Action": [
        "logs:CreateLogGroup",
        "logs:CreateLogStream",
        "logs:PutLogEvents"
      ],
      "Resource": "arn:aws:logs:*:*:*"
    },
    {
      "Effect": "Allow",
      "Action": [
          "cloudfront:CreateInvalidation"
      ],
      "Resource": [
          "*"
      ]
    }
  ]
}

第 16 步

点击允许。 这将使您返回到 lambda。 仔细检查您刚刚创建的角色名称是否已在现有角色组合框中选择。

第 17 步

内存 (MB) 设置为 128,将超时设置为 5 秒。

第 18 步

单击下一步,然后单击创建函数

第 19 步

一切顺利! 现在,每次您将任何文件上传/重新上传到 S3 时,都会在所有 CloudFront 边缘位置对其进行评估。

PS - 当您进行测试时,请确保您的浏览器正在从 CloudFront 加载图像,而不是从本地缓存加载图像。

PSS - 请注意,每月仅前 1000 个文件失效是免费的,每次超过限制的失效费用为 0.005 美元。 Lambda 函数也可能会收取额外费用,但非常便宜。

Automated update setup in 5 mins

OK, guys. The best possible way for now to perform automatic CloudFront update (invalidation) is to create Lambda function that will be triggered every time when any file is uploaded to S3 bucket (a new one or rewritten).

Even if you never used lambda functions before, it is really easy -- just follow my step-by-step instructions and it will take just 5 mins:

Step 1

Go to https://console.aws.amazon.com/lambda/home and click Create a lambda function

Step 2

Click on Blank Function (custom)

Step 3

Click on empty (stroked) box and select S3 from combo

Step 4

Select your Bucket (same as for CloudFront distribution)

Step 5

Set an Event Type to "Object Created (All)"

Step 6

Set Prefix and Suffix or leave it empty if you don't know what it is.

Step 7

Check Enable trigger checkbox and click Next

Step 8

Name your function (something like: YourBucketNameS3ToCloudFrontOnCreateAll)

Step 9

Select Python 2.7 (or later) as Runtime

Step 10

Paste following code instead of default python code:

from __future__ import print_function

import boto3
import time

def lambda_handler(event, context):
    for items in event["Records"]:
        path = "/" + items["s3"]["object"]["key"]
        print(path)
        client = boto3.client('cloudfront')
        invalidation = client.create_invalidation(DistributionId='_YOUR_DISTRIBUTION_ID_',
            InvalidationBatch={
            'Paths': {
            'Quantity': 1,
            'Items': [path]
            },
            'CallerReference': str(time.time())
            })

Step 11

Open https://console.aws.amazon.com/cloudfront/home in a new browser tab and copy your CloudFront distribution ID for use in next step.

Step 12

Return to lambda tab and paste your distribution id instead of _YOUR_DISTRIBUTION_ID_ in the Python code. Keep surrounding quotes.

Step 13

Set handler: lambda_function.lambda_handler

Step 14

Click on the role combobox and select Create a custom role. New tab in browser will be opened.

Step 15

Click view policy document, click edit, click OK and replace role definition with following (as is):

{
  "Version": "2012-10-17",
  "Statement": [
    {
      "Effect": "Allow",
      "Action": [
        "logs:CreateLogGroup",
        "logs:CreateLogStream",
        "logs:PutLogEvents"
      ],
      "Resource": "arn:aws:logs:*:*:*"
    },
    {
      "Effect": "Allow",
      "Action": [
          "cloudfront:CreateInvalidation"
      ],
      "Resource": [
          "*"
      ]
    }
  ]
}

Step 16

Click allow. This will return you to a lambda. Double check that role name that you just created is selected in the Existing role combobox.

Step 17

Set Memory (MB) to 128 and Timeout to 5 sec.

Step 18

Click Next, then click Create function

Step 19

You are good to go! Now on, each time you will upload/reupload any file to S3, it will be evaluated in all CloudFront Edge locations.

PS - When you are testing, make sure that your browser is loading images from CloudFront, not from local cache.

PSS - Please note, that only first 1000 files invalidation per month are for free, each invalidation over limit cost $0.005 USD. Also additional charges for Lambda function may apply, but it is extremely cheap.

陌上芳菲 2024-08-08 21:30:22

截至 3 月 19 日,Amazon 现在允许 Cloudfront 的缓存 TTL 为 0 秒,因此您(理论上)永远不会看到过时的对象。 因此,如果您的资产位于 S3 中,您只需转到 AWS Web Panel => S3=> 编辑属性=> 元数据,然后将“Cache-Control”值设置为“max-age=0”。

这直接来自 API 文档

要控制 CloudFront 是否缓存对象以及缓存多长时间,我们建议您将 Cache-Control 标头与 max-age= 指令结合使用。 CloudFront 将对象缓存指定的秒数。 (最小值为 0 秒。)

As of March 19, Amazon now allows Cloudfront's cache TTL to be 0 seconds, thus you (theoretically) should never see stale objects. So if you have your assets in S3, you could simply go to AWS Web Panel => S3 => Edit Properties => Metadata, then set your "Cache-Control" value to "max-age=0".

This is straight from the API documentation:

To control whether CloudFront caches an object and for how long, we recommend that you use the Cache-Control header with the max-age= directive. CloudFront caches the object for the specified number of seconds. (The minimum value is 0 seconds.)

俏︾媚 2024-08-08 21:30:22

好消息。 亚马逊终于添加了失效功能。 请参阅 API 参考

这是 API 参考中的示例请求:

POST /2010-08-01/distribution/[distribution ID]/invalidation HTTP/1.0
Host: cloudfront.amazonaws.com
Authorization: [AWS authentication string]
Content-Type: text/xml

<InvalidationBatch>
   <Path>/image1.jpg</Path>
   <Path>/image2.jpg</Path>
   <Path>/videos/movie.flv</Path>
   <CallerReference>my-batch</CallerReference>
</InvalidationBatch>

Good news. Amazon finally added an Invalidation Feature. See the API Reference.

This is a sample request from the API Reference:

POST /2010-08-01/distribution/[distribution ID]/invalidation HTTP/1.0
Host: cloudfront.amazonaws.com
Authorization: [AWS authentication string]
Content-Type: text/xml

<InvalidationBatch>
   <Path>/image1.jpg</Path>
   <Path>/image2.jpg</Path>
   <Path>/videos/movie.flv</Path>
   <CallerReference>my-batch</CallerReference>
</InvalidationBatch>
永言不败 2024-08-08 21:30:22

前往云锋。

单击您的 ID/分发。

单击失效。

单击创建失效。

在巨大的示例框中输入 * 并单击 invalidate

Done

在此处输入图像描述

Go to CloudFront.

Click on your ID/Distributions.

Click on Invalidations.

Click create Invalidation.

In the giant example box type * and click invalidate

Done

enter image description here

星星的軌跡 2024-08-08 21:30:22

当前的 AWS CLI 支持预览模式下的失效。 在控制台中运行以下命令一次:

aws configure set preview.cloudfront true

我使用 npm 部署我的 Web 项目。 我的 package.json 中有以下脚本:

{
    "build.prod": "ng build --prod --aot",
    "aws.deploy": "aws s3 sync dist/ s3://www.mywebsite.com --delete --region us-east-1",
    "aws.invalidate": "aws cloudfront create-invalidation --distribution-id [MY_DISTRIBUTION_ID] --paths /*",
    "deploy": "npm run build.prod && npm run aws.deploy && npm run aws.invalidate"
}

有了上面的脚本,您就可以使用以下方式部署您的网站:

npm run deploy

current AWS CLI support invalidation in preview mode. Run the following in your console once:

aws configure set preview.cloudfront true

I deploy my web project using npm. I have the following scripts in my package.json:

{
    "build.prod": "ng build --prod --aot",
    "aws.deploy": "aws s3 sync dist/ s3://www.mywebsite.com --delete --region us-east-1",
    "aws.invalidate": "aws cloudfront create-invalidation --distribution-id [MY_DISTRIBUTION_ID] --paths /*",
    "deploy": "npm run build.prod && npm run aws.deploy && npm run aws.invalidate"
}

Having the scripts above in place you can deploy your site with:

npm run deploy
2024-08-08 21:30:22

在 ruby​​ 中,即使在失效时使用雾宝石

AWS_ACCESS_KEY = ENV['AWS_ACCESS_KEY_ID']
AWS_SECRET_KEY = ENV['AWS_SECRET_ACCESS_KEY']
AWS_DISTRIBUTION_ID = ENV['AWS_DISTRIBUTION_ID']

conn = Fog::CDN.new(
    :provider => 'AWS',
    :aws_access_key_id => AWS_ACCESS_KEY,
    :aws_secret_access_key => AWS_SECRET_KEY
)

images = ['/path/to/image1.jpg', '/path/to/another/image2.jpg']

conn.post_invalidation AWS_DISTRIBUTION_ID, images

,在所有亚马逊边缘服务器上处理和刷新失效仍然需要 5-10 分钟

In ruby, using the fog gem

AWS_ACCESS_KEY = ENV['AWS_ACCESS_KEY_ID']
AWS_SECRET_KEY = ENV['AWS_SECRET_ACCESS_KEY']
AWS_DISTRIBUTION_ID = ENV['AWS_DISTRIBUTION_ID']

conn = Fog::CDN.new(
    :provider => 'AWS',
    :aws_access_key_id => AWS_ACCESS_KEY,
    :aws_secret_access_key => AWS_SECRET_KEY
)

images = ['/path/to/image1.jpg', '/path/to/another/image2.jpg']

conn.post_invalidation AWS_DISTRIBUTION_ID, images

even on invalidation, it still takes 5-10 minutes for the invalidation to process and refresh on all amazon edge servers

作死小能手 2024-08-08 21:30:22

一种非常简单的方法是文件夹版本控制。

因此,如果您的静态文件有数百个,只需将它们全部放入名为“year+versioning”的文件夹中。

例如,我使用一个名为 2014_v1 的文件夹,其中包含所有静态文件...

所以在我的 HTML 中,我总是放置对该文件夹的引用。 (当然,我有一个 PHP 包含,我在其中设置了文件夹的名称。)因此,通过更改 1 个文件,它实际上会更改我的所有 PHP 文件。

如果我想要完全刷新,我只需将文件夹重命名为 2014_v2 即可我的源代码和php内部的更改包含到2014_v2,

所有HTML自动更改并询问新路径,cloudfront MISS缓存并向源代码请求。

例子:
SOURCE.mydomain.com 是我的来源,
cloudfront.mydomain.com 是 cloudfront 分配的 CNAME。

所以PHP调用这个文件
cloudfront.mydomain.com/2014_v1/javascript.js
当我想要完全刷新时,只需将源文件夹重命名为“2014_v2”,然后通过将文件夹设置为“2014_v2”来更改 PHP 包含内容。

这样,失效就不会延迟,而且无需任何成本!

这是我在 stackoverflow 上的第一篇文章,希望我做得很好!

one very easy way to do it is FOLDER versioning.

So if your static files are hundreds for example, simply put all of them into a folder called by year+versioning.

for example i use a folder called 2014_v1 where inside i have all my static files...

So inside my HTML i always put the reference to the folder. ( of course i have a PHP include where i have set the name of the folder. ) So by changing in 1 file it actually change in all my PHP files..

If i want a complete refresh, i simply rename the folder to 2014_v2 into my source and change inside the php include to 2014_v2

all HTML automatically change and ask the new path, cloudfront MISS cache and request it to the source.

Example:
SOURCE.mydomain.com is my source,
cloudfront.mydomain.com is CNAME to cloudfront distribution.

So the PHP called this file
cloudfront.mydomain.com/2014_v1/javascript.js
and when i want a full refresh, simply i rename folder into the source to "2014_v2" and i change the PHP include by setting the folder to "2014_v2".

Like this there is no delay for invalidation and NO COST !

This is my first post in stackoverflow, hope i did it well !

不甘平庸 2024-08-08 21:30:22

如果您安装了 boto (这不仅适用于 python,还安装了一堆有用的命令行实用程序),它提供了一个命令行实用程序,专门称为 cfadmin 或“cloud front admin”,它提供以下功能:

Usage: cfadmin [command]
cmd - Print help message, optionally about a specific function
help - Print help message, optionally about a specific function
invalidate - Create a cloudfront invalidation request
ls - List all distributions and streaming distributions

您可以通过运行使事物失效:

$sam# cfadmin invalidate <distribution> <path>

If you have boto installed (which is not just for python, but also installs a bunch of useful command line utilities), it offers a command line util specifically called cfadmin or 'cloud front admin' which offers the following functionality:

Usage: cfadmin [command]
cmd - Print help message, optionally about a specific function
help - Print help message, optionally about a specific function
invalidate - Create a cloudfront invalidation request
ls - List all distributions and streaming distributions

You invaliate things by running:

$sam# cfadmin invalidate <distribution> <path>
猫九 2024-08-08 21:30:22

使用 Invalidation API,它会在几分钟内更新。
请查看 PHP 无效器

With the Invalidation API, it does get updated in a few of minutes.
Check out PHP Invalidator.

放飞的风筝 2024-08-08 21:30:22

Bucket Explorer 的 UI 使这一切现在变得非常简单。 操作方法如下:

右键单击您的存储桶。 选择“管理分发”。
右键单击您的发行版。 选择“获取 Cloudfront 失效列表”
然后选择“创建”以创建新的失效列表。
选择要失效的文件,然后单击“失效”。 等待 5-15 分钟。

Bucket Explorer has a UI that makes this pretty easy now. Here's how:

Right click your bucket. Select "Manage Distributions."
Right click your distribution. Select "Get Cloudfront invalidation list"
Then select "Create" to create a new invalidation list.
Select the files to invalidate, and click "Invalidate." Wait 5-15 minutes.

破晓 2024-08-08 21:30:22

只是发帖通知访问此页面的任何人(“Cloudfront 文件刷新”上的第一个结果)
swook.net 上有一个易于使用和访问的在线失效器

这个新的失效器是:

  • 完全在线(无需安装)
  • 24x7 可用(由 Google 托管)并且不需要任何会员资格。
  • 历史记录支持和路径检查可让您轻松使文件失效。 (通常在第一次失效后只需点击几下即可!)
  • 它也非常安全,您会在阅读其 发布帖子

完全披露:我做了这个。 玩得开心!

Just posting to inform anyone visiting this page (first result on 'Cloudfront File Refresh')
that there is an easy-to-use+access online invalidator available at swook.net

This new invalidator is:

  • Fully online (no installation)
  • Available 24x7 (hosted by Google) and does not require any memberships.
  • There is history support, and path checking to let you invalidate your files with ease. (Often with just a few clicks after invalidating for the first time!)
  • It's also very secure, as you'll find out when reading its release post.

Full disclosure: I made this. Have fun!

╰つ倒转 2024-08-08 21:30:22

如果您使用 AWS,您可能还会使用其官方 CLI 工具(迟早)。 AWS CLI 版本 1.9.12 或更高版本支持使文件名列表失效。

完全披露:我做了这个。 玩得开心!

If you are using AWS, you probably also use its official CLI tool (sooner or later). AWS CLI version 1.9.12 or above supports invalidating a list of file names.

Full disclosure: I made this. Have fun!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文