如何在Amazon S3中使用浏览器缓存?

4th*_*ace 16 amazon-s3 amazon-web-services amazon-cloudfront

我在gtmetrix.com上获得以下内容:

Leverage browser caching    
E (55)
52%
Server  High
What does this mean?
The following cacheable resources have a short freshness lifetime. Specify an expiration at least one week in the future for the following resources:

http://mysite.s3.amazonaws.com/profile.png (expiration not specified)
http://mysite.s3.amazonaws.com/circle.player.css (expiration not specified)
http://mysite.s3.amazonaws.com/main.js (expiration not specified)
http://mysite.s3.amazonaws.com/not.the.skin.css (expiration not specified)
http://cdn-images.mailchimp.com/embedcode/slim-321711.css (expiration not specified)
Run Code Online (Sandbox Code Playgroud)

我猜测mailchimp文件无法修改.但是,如何为Amazon S3 Cloudfront文件指定浏览器缓存?

Fla*_*che 26

通过Amazon Management Console,选择您的文件(或文件夹).

在此输入图像描述


转到"属性"选项卡,然后单击"元数据".

在此输入图像描述


最后,将Cache-Control添加到其标头并保存.

在此输入图像描述

  • 手动?逐一?如果您有数千个文件怎么办?如果您想更新这些值怎么办? (4认同)
  • 您可以将其添加到存储桶中的整个文件夹,它将更新所有包含的对象 (4认同)

Ryu*_*usa 10

您可以使用s3cmd指定header参数!在将文件上传到s3时.

例如:

s3cmd sync LOCAL_DIRECTORY s3://BUCKET[/PREFIX] --add-header=Cache-Control:max-age=3153600
Run Code Online (Sandbox Code Playgroud)

  • 值得一提的是,使用[AWS CLI工具](http://aws.amazon.com/cli/) [S3 Command](http://docs.aws.amazon.com/cli/latest)也可以做到这一点./reference/s3 /):`aws s3 sync $ LOCAL s3:// $ REMOTE --cache-control"max-age = 3153600"` (12认同)

dat*_*age 6

将文件存储到s3时.您可以设置将发送到客户端的自定义标头.在这种情况下,您可以为文件设置Expires标头.

Cloudfront的工作方式略有不同,因为它取决于您的来源.