简体   繁体   English

如何从 Amazon S3 提供压缩资产

[英]How to serve gzipped assets from Amazon S3

I am currently serving all of my static assets from Amazon S3.我目前正在从 Amazon S3 提供我所有的静态资产。 I would like to begin using gzipped components.我想开始使用 gzip 组件。 I have gzipped and confirmed that Amazon is setting the correct headers.我已经压缩并确认亚马逊正在设置正确的标题。 However, the styles are not loading.但是,样式未加载。

I am new to gzipping components, so possibly I am missing something?我是 gzipping 组件的新手,所以我可能遗漏了什么? I can't find too much information about this with Amazon S3.我在 Amazon S3 上找不到太多关于此的信息。

For future reference to anyone else with this problem:以供将来参考有此问题的其他人:

Gzip your components. Gzip 组件。 Then remove the .gz extension leaving only the .css or .js extension.然后删除 .gz 扩展名,只留下 .css 或 .js 扩展名。 Upload the files to your bucket.将文件上传到您的存储桶。

From your S3 dashboard, pull up the properties for the file that you just uploaded.从您的 S3 仪表板中,提取您刚刚上传的文件的属性。 Under the 'Metadata' header enter this information:在“元数据”标题下输入以下信息:

'content-type'      :  'text/css' or 'text/javascript'
'content-encoding'  :  'gzip'

These value options are not available by default (wtf) so you must manually type them.这些值选项默认不可用 (wtf),因此您必须手动键入它们。

I also found a solution how to do it using CLI, very useful when working with multiple files:我还找到了一个如何使用 CLI 执行此操作的解决方案,在处理多个文件时非常有用:

aws s3api put-object \ 
  --bucket YOUR_BUCKET \ 
  --key REMOTE_FILE.json \ 
  --content-encoding gzip \
  --content-type application/json \ 
  --body LOCAL_FILE.json.gz 

Notes:笔记:

  • Set content-type approppriately to what you're uploadingcontent-type适当地设置为您要上传的内容
  • The file name on the server doesn't need to have the .gz extension服务器上的文件名不需要具有.gz扩展名

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

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