自动为整个 S3 存储桶设置缓存控制(使用存储桶策略?) [英] Set cache-control for entire S3 bucket automatically (using bucket policies?)

查看:48
本文介绍了自动为整个 S3 存储桶设置缓存控制(使用存储桶策略?)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要为整个 s3 存储桶(包括现有文件和未来文件)设置缓存控制标头,并希望在存储桶策略中执行此操作.我知道我可以编辑现有的,并且如果我自己上传它们,我知道如何在放置时指定它们,但不幸的是,上传它们的应用程序无法设置标头,因为它使用 s3fs 将文件复制到那里.

I need to set cache-control headers for an entire s3 bucket, both existing and future files and was hoping to do it in a bucket policy. I know I can edit the existing ones and I know how to specify them on put if I upload them myself but unfortunately the app that uploads them cannot set the headers as it uses s3fs to copy the files there.

推荐答案

现在有 3 种方法可以完成此任务:通过 AWS 控制台通过命令行,或通过 s3cmd 命令行工具.

There are now 3 ways to get this done: via the AWS Console, via the command line, or via the s3cmd command line tool.

这是现在推荐的解决方案.这很简单,但可能需要一些时间.

This is now the recommended solution. It is straight forward, but it can take some time.

  • 登录 AWS 管理控制台
  • 进入 S3 存储桶
  • 按路径选择所有文件
  • 选择更多"从菜单
  • 选择更改元数据"
  • 在钥匙"中字段,选择缓存控制"从下拉菜单max-age=604800 输入(7 天)作为值
  • 按保存"按钮

(感谢@biplob - 请在下面给他一些爱)

最初,当我创建这个存储桶策略时是不可行的,所以我想出了如何使用 aws-cli 来做到这一点,而且它非常巧妙.在研究时,我在野外找不到任何例子,所以我想我会发布一些我的解决方案来帮助那些有需要的人.

Originally, when I created this bucket policies were a no go, so I figured how to do it using aws-cli, and it is pretty slick. When researching I couldn't find any examples in the wild, so I thought I would post some of my solutions to help those in need.

注意:默认情况下,aws-cli 仅复制文件的当前元数据,即使您指定了新元数据.

要使用在命令行中指定的元数据,您需要添加--metadata-directive REPLACE"标志.以下是一些示例.

To use the metadata that is specified on the command line, you need to add the '--metadata-directive REPLACE' flag. Here are a some examples.

对于单个文件

aws s3 cp s3://mybucket/file.txt s3://mybucket/file.txt --metadata-directive REPLACE 
--expires 2034-01-01T00:00:00Z --acl public-read --cache-control max-age=2592000,public

对于整个存储桶(注意 --recursive 标志):

aws s3 cp s3://mybucket/ s3://mybucket/ --recursive --metadata-directive REPLACE 
--expires 2034-01-01T00:00:00Z --acl public-read --cache-control max-age=2592000,public

我发现了一个小问题,如果您只想将其应用于特定文件类型,则需要排除所有文件,然后包含您想要的文件.

A little gotcha I found, if you only want to apply it to a specific file type, you need to exclude all the files, then include the ones you want.

仅 jpg 和 png:

aws s3 cp s3://mybucket/ s3://mybucket/ --exclude "*" --include "*.jpg" --include "*.png" 
--recursive --metadata-directive REPLACE --expires 2034-01-01T00:00:00Z --acl public-read 
--cache-control max-age=2592000,public

如果您需要更多信息,这里有一些手册链接:

Here are some links to the manual if you need more info:

已知问题:

"Unknown options: --metadata-directive, REPLACE"

这可能是由过时的 awscli 引起的 - 请参阅 @eliotRosewater 下面的回答

this can be caused by an out of date awscli - see @eliotRosewater's answer below

S3cmd 是用于管理 Amazon S3 和 CloudFront 服务的命令行工具".虽然此解决方案需要 git pull,但它可能是一个更简单、更全面的解决方案.

S3cmd is a "Command line tool for managing Amazon S3 and CloudFront services". While this solution requires a git pull it might be a simpler and more comprehensive solution.

有关完整说明,请参阅下方@ashishyadaveee11 的帖子

希望能帮到你!

这篇关于自动为整个 S3 存储桶设置缓存控制(使用存储桶策略?)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆