Mat*_*ner 24 web-hosting amazon-s3
我正在使用Amazon S3虚拟主机来生成我的静态html,js,css(等等)文件.替换我的index.html文件后,我仍然通过浏览器使用旧版本.我想设置一个默认的ttl 到桶(而不是它中的特定对象).我找到了这个链接:http: //docs.aws.amazon.com/AmazonCloudFront/latest/DeveloperGuide/distribution-web-values-specify.html#DownloadDistValuesDefaultTTL
但无法在仪表板中找到"对象缓存"设置.谁能指出它在哪里?
D. *_*ods 32
这是一个回答您问题的Amazon S3链接.据亚马逊称,除非您使用第三方工具(该页面链接到少数几个),否则您无法为整个存储桶设置缓存控制标头.我遇到的一个工具描述了如何为使用PUT请求提交的对象设置缓存指令(或使用他们的工具批量设置).有关详细信息,请参阅BucketExplorer.
以下是亚马逊指令的剪贴(因为SO不喜欢依赖可能改变或消失的外部链接):
使用Amazon S3控制台将Cache-Control或Expires标头字段添加到Amazon S3对象
max-age=number of seconds that you want objects to stay in a CloudFront edge cache如果要将标题字段添加到其他对象,请单击下一个对象的名称,然后重复步骤5到9.
Sea*_*n W 12
实现此目的的最简单方法是使用AWS CLI (S3)。这也可以使用GitHub actions等工具完全免费实现自动化。
静态站点不应将文件cache-control设置为长期存在的值HTML,因为在浏览器缓存过期或手动破坏其缓存之前,用户不会看到更新的版本。
由于AWS的CLI限制,您必须执行以下操作才能为整个存储桶设置缓存。
通用示例
上传内容和--delete旧的S3内容,并设置cache-control所有内容。
aws s3 sync [YOUR_LOCAL_SOURCE_CODE_PATH] s3://[BUCKET_NAME] --delete --cache-control max-age=31536000
Run Code Online (Sandbox Code Playgroud)
cache-control递归地从所有文件中删除标头HTML并将文件设置回类型HTML.
aws s3 cp s3://[BUCKET_NAME] s3://[TO_BUCKET_NAME] --recursive --exclude "*" --include "*.html" --metadata-directive REPLACE --cache-control max-age:no-cache --content-type text/html
Run Code Online (Sandbox Code Playgroud)
笔记
HTML在 AWS S3 中修改文件的元数据,则还必须设置content-type,否则它将自动设置为通用类型,导致浏览器下载该文件而不是在浏览器中呈现它。例子
// delete old files and upload files from the local directory to the s3 bucket, and set the cache-control header on every file.
aws s3 sync ./out s3://www.test.com --delete --cache-control max-age=31536000
// copy all files and remove cache control header from only HTML files and set back to html content type
aws s3 cp s3://www.test.com s3://www.test.com --recursive --exclude "*" --include "*.html" --metadata-directive REPLACE --cache-control max-age:no-cache --content-type text/html
// bonus - if using CloudFront - small site can invalidate all cache (/*)
aws cloudfront create-invalidation --distribution-id=123ABCDEFG --paths "/*"
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
16717 次 |
| 最近记录: |