强制 CloudFront 分发/文件更新
我正在使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(13)
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 代码:
第 11 步
打开 https://console.aws。在新的浏览器选项卡中访问 amazon.com/cloudfront/home 并复制您的 CloudFront 分配 ID 以供下一步使用。
第 12 步
返回 lambda 选项卡并粘贴您的分发 ID,而不是 Python 代码中的 _YOUR_DISTRIBUTION_ID_。 保留周围的引号。
第 13 步
设置处理程序:lambda_function.lambda_handler
第 14 步
单击角色组合框并选择创建自定义角色。 将打开浏览器中的新选项卡。
第15步
点击查看策略文档,点击编辑,点击确定并将角色定义替换为以下内容(按原样) :
第 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:
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):
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.
截至 3 月 19 日,Amazon 现在允许 Cloudfront 的缓存 TTL 为 0 秒,因此您(理论上)永远不会看到过时的对象。 因此,如果您的资产位于 S3 中,您只需转到 AWS Web Panel => S3=> 编辑属性=> 元数据,然后将“Cache-Control”值设置为“max-age=0”。
这直接来自 API 文档:
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:
好消息。 亚马逊终于添加了失效功能。 请参阅 API 参考。
这是 API 参考中的示例请求:
Good news. Amazon finally added an Invalidation Feature. See the API Reference.
This is a sample request from the API Reference:
前往云锋。
单击您的 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
当前的 AWS CLI 支持预览模式下的失效。 在控制台中运行以下命令一次:
我使用 npm 部署我的 Web 项目。 我的
package.json
中有以下脚本:有了上面的脚本,您就可以使用以下方式部署您的网站:
current AWS CLI support invalidation in preview mode. Run the following in your console once:
I deploy my web project using npm. I have the following scripts in my
package.json
:Having the scripts above in place you can deploy your site with:
在 ruby 中,即使在失效时使用雾宝石
,在所有亚马逊边缘服务器上处理和刷新失效仍然需要 5-10 分钟
In ruby, using the fog gem
even on invalidation, it still takes 5-10 minutes for the invalidation to process and refresh on all amazon edge servers
一种非常简单的方法是文件夹版本控制。
因此,如果您的静态文件有数百个,只需将它们全部放入名为“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 !
如果您安装了 boto (这不仅适用于 python,还安装了一堆有用的命令行实用程序),它提供了一个命令行实用程序,专门称为 cfadmin 或“cloud front admin”,它提供以下功能:
您可以通过运行使事物失效:
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:You invaliate things by running:
使用 Invalidation API,它会在几分钟内更新。
请查看 PHP 无效器。
With the Invalidation API, it does get updated in a few of minutes.
Check out PHP Invalidator.
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.
只是发帖通知访问此页面的任何人(“Cloudfront 文件刷新”上的第一个结果)
swook.net 上有一个易于使用和访问的在线失效器
这个新的失效器是:
完全披露:我做了这个。 玩得开心!
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:
Full disclosure: I made this. Have fun!
设置 TTL=1 小时并替换
http://developer.amazonwebservices.com/connect /ann.jspa?annID=655
Set TTL=1 hour and replace
http://developer.amazonwebservices.com/connect/ann.jspa?annID=655
如果您使用 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!