2

I would like to understand if there are any best practices on how to maintain images on S3 and use cloudfront to access them. The reason why am asking this question is because of how S3 and cloudfront work together.

I upload an image, set an expiry using the meta tags on S3 and try to use the cloudfront URL I get the desired image. The problem is when I try to update the same image and it does not immediately reflect until the set expiry on the meta tags in met.

Is there a good way to get the latest image if there is a change on S3?

4

2 回答 2

3

您需要告诉 CloudFront 它已缓存的文件已更改,以便它将从 S3 获取更新的版本。为此,您使CloudFront 上的文件无效。然后下次请求该文件时,CloudFront 将返回 S3 以获取最新版本。

于 2012-05-08T18:39:30.930 回答
2

虽然您可以使用 Cloudfront 的无效功能,但我认为它具有传播延迟,因此如果您对图像/css/js 进行重大更改,可能会出现问题。

我们经历了类似的进展,确保更改总是在进入 Cloudfront 之前立即传播,然后在进入 Cloudfront 之后再次传播。我假设许多其他人也与我编写的脚本的更多升级/更新相吻合,我决定让其他人使用它(这里有更多历史!)

该脚本托管在这里:https ://github.com/alltuition/version-tags

基本上它是一个 Python 或 Django 脚本,它使用 Linux 命令和 git 历史记录来确定对媒体文件进行了哪些文件修改,然后使用 sed 将所有引用更新到修改文件的不同路径。(文件必须从允许您使用通配符别名的服务器提供(自述文件提供了 Nginx、Apache 和 Django 开发服务器的说明。))

对媒体文件的所有更改都会立即在用户的浏览器中更新,因为第一个获得第一个修改过的 html 页面的人请求了一个不在 Cloudfront 缓存中的 url,因此 Cloudfront 去获取文件的新版本。这也意味着您可以将所有媒体文件设置为永不过期。

于 2012-06-18T18:42:25.523 回答