如何避免Github Action上传S3文件时自动修改Content-Encoding?
解决S3上传JS文件时Content-Encoding被自动设为Gzip的问题
问题根源
你使用的lbertenasco/s3-deploy@v1 Github Action默认会自动对上传文件做Gzip压缩,还会给文件加上Content-Encoding: gzip的响应头,这就是上传后编码被修改的原因。而用Cyberduck手动上传时不会触发这个自动压缩逻辑,所以能保持原始头信息。
解决方案
在Action配置中添加参数,要么关闭自动压缩,要么精确指定文件的头信息:
方法1:直接关闭自动压缩
在with字段里添加gzip: false,禁用Action的自动Gzip压缩功能:
- name: Deploy to production CDN and invalidate if: github.ref == 'refs/heads/master' uses: lbertenasco/s3-deploy@v1 with: folder: test bucket: ${{ secrets.S3_BUCKET }} dist-id: ${{ secrets.CLOUDFRONT_DISTRIBUTION_ID }} invalidation: /example/example.js gzip: false
方法2:针对特定文件自定义头信息
如果只需要控制目标JS文件的头,可以用headers参数明确指定它的Content-Encoding为空,同时确保Content-Type正确:
- name: Deploy to production CDN and invalidate if: github.ref == 'refs/heads/master' uses: lbertenasco/s3-deploy@v1 with: folder: test bucket: ${{ secrets.S3_BUCKET }} dist-id: ${{ secrets.CLOUDFRONT_DISTRIBUTION_ID }} invalidation: /example/example.js headers: | [ { "key": "Content-Encoding", "value": "", "files": "example/example.js" }, { "key": "Content-Type", "value": "application/javascript", "files": "example/example.js" } ]
验证步骤
上传完成后,进入S3控制台找到目标JS文件,查看它的元数据,确认Content-Encoding未被设置、Content-Type为application/javascript即可。之后Cloudfront会根据浏览器的Accept-Encoding头,正确返回Brotli或其他支持的压缩版本。
内容的提问来源于stack exchange,提问作者dealwap
相关产品推荐
相关产品推荐

