简体   繁体   English

Amazon S3 和 Cloudfront 缓存,如何清除缓存或同步它们的缓存

[英]Amazon S3 and Cloudfront cache, how to clear cache or synchronize their cache

I have a cron job that runs every 10 minutes and updates the content-type and x-amz-meta.我有一个每 10 分钟运行一次并更新内容类型和 x-amz-meta 的 cron 作业。 But since yesterday it seems like after the cron job run, Amazon is not picking up the changes made (refreshing his cache).但是从昨天开始,似乎在 cron 作业运行之后,亚马逊并没有接受所做的更改(刷新他的缓存)。

I even went and made the changes manually but no change...我什至去手动进行了更改,但没有更改...

When a video is uploaded it has a application/x-mp4 content-type and the cron job changes it to video/mp4 .上传视频时,它具有application/x-mp4内容类型,cron 作业将其更改为video/mp4

Although S3 has the right content type video/mp4 cloudfront shows application/x-mp4 (old content-type) ....尽管 S3 具有正确的内容类型video/mp4 cloudfront 显示application/x-mp4 (旧内容类型)....

The cron job has been working for the last 6 months without a problem. cron 作业在过去 6 个月内一直正常工作,没有出现任何问题。

What is wrong with amazon caching?亚马逊缓存有什么问题? How can i synchronize the caching?如何同步缓存?

Use Invalidations to clear the cache, you can put the path to the files you want to clear, or simply use wild cards to clear everything.使用 Invalidations 清除缓存,您可以将路径放在要清除的文件中,也可以简单地使用通配符清除所有内容。

http://docs.aws.amazon.com/AmazonCloudFront/latest/DeveloperGuide/Invalidation.html#invalidating-objects-api http://docs.aws.amazon.com/AmazonCloudFront/latest/DeveloperGuide/Invalidation.html#invalidating-objects-api

This can also be done using the API!这也可以使用 API 来完成! http://docs.aws.amazon.com/cloudfront/latest/APIReference/API_CreateInvalidation.html http://docs.aws.amazon.com/cloudfront/latest/APIReference/API_CreateInvalidation.html

The AWS PHP SDK now has the methods but if you want to use something lighter check out this library: http://www.subchild.com/2010/09/17/amazon-cloudfront-php-invalidator/ AWS PHP SDK 现在有这些方法,但如果你想使用更轻松的东西,请查看这个库: http : //www.subchild.com/2010/09/17/amazon-cloudfront-php-invalidator/

user3305600's solution doesn't work as setting it to zero is the equivalent of Using the Origin Cache Headers. user3305600 的解决方案不起作用,因为将其设置为零相当于使用源缓存标头。

As to the actual code至于实际代码

get your CloudFront distribution id获取您的 CloudFront 分配 ID

aws cloudfront list-distributions

Invalidate all files in the distribution, so CloudFront fetches fresh ones使分配中的所有文件无效,因此 CloudFront 获取新文件

aws cloudfront create-invalidation --distribution-id=S11A16G5KZMEQD --paths /

My actual full release script is我实际的完整发布脚本是

#!/usr/bin/env bash

BUCKET=mysite.com
SOURCE_DIR=dist/

export AWS_ACCESS_KEY_ID=xxxxxxxxxxx
export AWS_SECRET_ACCESS_KEY=xxxxxxxxx
export AWS_DEFAULT_REGION=eu-west-1


echo "Building production"
if npm run build:prod ; then
   echo "Build Successful"
else
  echo "exiting.."
  exit 1
fi


echo "Removing all files on bucket"
aws s3 rm s3://${BUCKET} --recursive


echo "Attempting to upload site .."
echo "Command:  aws s3  sync $SOURCE_DIR s3://$BUCKET/"
aws s3  sync ${SOURCE_DIR} s3://${BUCKET}/
echo "S3 Upload complete"

echo "Invalidating cloudfrond distribution to get fresh cache"
aws cloudfront create-invalidation --distribution-id=S11A16G5KZMEQD --paths / --profile=myawsprofile

echo "Deployment complete"  

References参考

http://docs.aws.amazon.com/cli/latest/reference/cloudfront/get-invalidation.html http://docs.aws.amazon.com/cli/latest/reference/cloudfront/get-invalidation.html

http://docs.aws.amazon.com/cli/latest/reference/cloudfront/create-invalidation.html http://docs.aws.amazon.com/cli/latest/reference/cloudfront/create-invalidation.html

Here is a manual way to invalidate the cache for all files on CloudFront via AWS这是通过 AWS 使 CloudFront 上所有文件的缓存无效的手动方法

在此处输入图片说明

在此处输入图片说明

在此处输入图片说明

S3 is not used for real time development but if you really want to test your freshly deployed website use S3 不用于实时开发,但如果您真的想测试新部署的网站,请使用

http://yourdomain.com/index.html?v=2
http://yourdomain.com/init.js?v=2

Adding a version parameter in the end will stop using the cached version of the file and the browser will get a fresh copy of the file from the server bucket最后添加版本参数将停止使用文件的缓存版本,浏览器将从服务器存储桶中获取文件的新副本

Cloudfront will cache a file/object until the cache expiry. Cloudfront 将缓存文件/对象,直到缓存到期。 By default it is 24 hrs.默认为 24​​ 小时。 If you have changed this to a large value, then it takes longer.如果您已将其更改为较大的值,则需要更长的时间。

If you anytime needs to force clear the cache, use the invalidation .如果您需要强制清除缓存,请使用invalidation It is charged separately.它是单独收费的。

Another option is to change the URL (object key), so it fetches the new object always.另一种选择是更改 URL(对象键),因此它始终获取新对象。

If you're looking for a minimal solution that invalidates the cache, this edited version of Dr Manhattan's solution should be sufficient.如果您正在寻找使缓存无效的最小解决方案,那么曼哈顿博士解决方案的这个编辑版本应该足够了。 Note that I'm specifying the root / directory to indicate I want the whole site refreshed.请注意,我指定了根 / 目录以表明我希望刷新整个站点。

export AWS_ACCESS_KEY_ID=<Key>
export AWS_SECRET_ACCESS_KEY=<Secret>
export AWS_DEFAULT_REGION=eu-west-1

echo "Invalidating cloudfrond distribution to get fresh cache"
aws cloudfront create-invalidation --distribution-id=<distributionId> --paths / --profile=<awsprofile>

Region Codes can be found here 区域代码可以在这里找到

You'll also need to create a profile using the aws cli.您还需要使用 aws cli 创建配置文件。 Use the aws configure --profile option.使用aws configure --profile选项。 Below is an example snippet from Amazon.以下是来自亚马逊的示例片段。

$ aws configure --profile user2
AWS Access Key ID [None]: AKIAI44QH8DHBEXAMPLE
AWS Secret Access Key [None]: je7MtGbClwBF/2Zp9Utk/h3yCo8nvbEXAMPLEKEY
Default region name [None]: us-east-1
Default output format [None]: text

(edit: Does not work)As of 2014, You can clear your cache whenever you want, Please go thorough the Documentation or just go to your distribution settings>Behaviors>Edit (编辑:不起作用)截至 2014 年,您可以随时清除缓存,请仔细阅读文档或转到您的分发设置>行为>编辑

Object Caching Use ( Origin Cache Headers ) Customize对象缓存使用( Origin Cache Headers自定义

Minimum TTL = 0最小 TTL = 0

http://docs.aws.amazon.com/AmazonCloudFront/latest/DeveloperGuide/Expiration.html http://docs.aws.amazon.com/AmazonCloudFront/latest/DeveloperGuide/Expiration.html

Don't use invalidations.不要使用失效。 They cannot be reverted and you will be charged.它们无法恢复,您将被收取费用。 They only way it works for me is reducing the TTL and wait.他们对我有用的唯一方法是减少 TTL 并等待。

Regards问候

I believe using * invalidate the entire cache in the distribution.我相信使用 * 会使分发中的整个缓存无效。 I am trying at the moment, I would update it further我目前正在尝试,我会进一步更新

invalidate request screenshot无效请求截图

Update:更新:

It worked as expected.它按预期工作。 Please note that you can invalidate the object you would like by specifying the object path.请注意,您可以通过指定对象路径来使您想要的对象无效。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM