简体   繁体   English

Amazon S3 上的自定义标头

[英]Custom headers on Amazon S3

Is it possible to have custom headers on Amazon S3 with arbitrary naming?是否可以在 Amazon S3 上使用任意命名的自定义标头?

For example, I am using a CDN pointing to Amazon S3 as the origin server, and in order to enable advanced functionality on the CDN I need to use a custom header x-something-something ...例如,我使用指向 Amazon S3 作为源服务器的 CDN,为了在 CDN 上启用高级功能,我需要使用自定义标头x-something-something ...

I see it's possible to do this with x-amz-meta-(something) but what about something more general like x-(something)-(something) without the amz ?我认为可以用x-amz-meta-(something)来做到这一点,但是如果没有amzx-(something)-(something)这样更一般的x-(something)-(something)呢?

Amazon S3 自定义标头

This beautiful article explains it all https://medium.com/@tom.cook/edge-lambda-cloudfront-custom-headers-3d134a2c18a2这篇漂亮的文章解释了这一切https://medium.com/@tom.cook/edge-lambda-cloudfront-custom-headers-3d134a2c18a2

tldr:域名:

You can't-do only with S3.您不能只使用 S3。 You need to use CloudFront and Lambda via Lambda@Edge .您需要通过Lambda@Edge使用 CloudFront 和 Lambda。 It's an integration between Lambda and CloudFront.它是 Lambda 和 CloudFront 之间的集成。 It allows you to run Lambdas within the CloudFront.它允许您在 CloudFront 中运行 Lambda。 This allows you to change headers among other things.这允许您更改标题等。 So if you are ok accessing your S3 via CloudFront then this could be a viable option.因此,如果您可以通过 CloudFront 访问 S3,那么这可能是一个可行的选择。

I don't think it is possible with their current API .我认为他们当前的 API是不可能的。 They cover the major headers you will need for caching and browser interoperability.它们涵盖了缓存和浏览器互操作性所需的主要标头。

I think they are being safe in only allowing x-amz-meta- prefixed custom headers, possibly to keep from clashing with user selected headers when they update their api in the future.我认为他们只允许以x-amz-meta-为前缀的自定义标头是安全的,可能是为了在将来更新他们的 api 时避免与用户选择的标头发生冲突。

If you need custom attributes attached to your objects, it should be trivial to parse out the x-amz-meta- in your client application.如果您需要将自定义属性附加到您的对象,那么在您的客户端应用程序中解析x-amz-meta-应该是微不足道的。

I was able to achieve this using the s3cmd tool .我能够使用s3cmd 工具实现这一点。 I wrote a sync script that syncs my static site using the --cache-control parameter on the AWS client tool and then manually resets it and adds a couple other headers for a few specific files:我编写了一个同步脚本,使用 AWS 客户端工具上的--cache-control参数同步我的静态站点,然后手动重置它并为一些特定文件添加几个其他标头:

cd /appropriatedirectory cd /适当目录

# Delete current site # 删除当前站点
aws s3 rm s3://yourbucket --recursive --exclude 'logs/*' aws s3 rm s3://yourbucket --recursive --exclude 'logs/*'

# Upload new site, setting cache header to 1 month for all files # 上传新站点,将所有文件的缓存头设置为 1 个月
aws s3 sync . aws s3 同步。 s3://yourbucket --exclude '.idea/*' --exclude '.git/*' --exclude '.gitignore' --cache-control max-age=2592000 s3://yourbucket --exclude '.idea/*' --exclude '.git/*' --exclude '.gitignore' --cache-control max-age=2592000

# Overrides the cache headers for some file # 覆盖某些文件的缓存头
python /pathToS3cmd/s3cmd modify --add-header="Cache-Control:no-cache,no-store,must-revalidate" s3://yourbucket/somefile.html python /pathToS3cmd/s3cmd modify --add-header="Cache-Control:no-cache,no-store,must-revalidate" s3://yourbucket/somefile.html
python /pathToS3cmd/s3cmd/s3cmd modify --add-header="Expires:0" s3://yourbucket/somefile.html python /pathToS3cmd/s3cmd/s3cmd 修改 --add-header="Expires:0" s3://yourbucket/somefile.html
python /pathToS3cmd/s3cmd/s3cmd modify --add-header="Pragma:no-cache" s3://yourbucket/somefile.html python /pathToS3cmd/s3cmd/s3cmd 修改 --add-header="Pragma:no-cache" s3://yourbucket/somefile.html

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

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