GZIP Compression on Static Amazon S3 Files

I would like to implement GZIP compression on my site. I implemented this on IIS and the HTML page was compressed successfully as expected.

Now the problem is with the CSS and JS files that I get from Amazon S3 . They are not compressed at all. I also wanted to squeeze them.

Please show me how to do this. Links to it help me a lot.

Update: I added a meta header for S3 files as "Content-Encoding: gzip", now it appears in the response header. However, the file size is the same and does not affect the specific CSS on the page . And I can't even open it in the browser. Here is [link] [1] specific CSS.

thank

+6


source to share


3 answers


The files must be compressed before uploading to Amazon S3.

For some examples see:



+9


source


If you use CloudFront in front of your S3 bucket, there is no need to manually compress HTML assets (CloudFront will compress them on the fly). Note that CloudFront only compresses in gzip (no deflate, brotli) and only CSS / JS / HTML (depending on content type). See https://docs.aws.amazon.com/AmazonCloudFront/latest/DeveloperGuide/ServingCompressedFiles.html#compressed-content-cloudfront-file-types . For this to work, you must redirect some http headers from CloudFront to S3 (see doc).

If your S3 bucket has resources not supported by Cloudfront (a generic "binary / octet stream" MIME type such as "hdr" texture or "nds" ROM), you need to compress them yourself before uploading to S3 and then install "content- encoding "http meta per resource. Note that only browsers that support gz encoding will be able to download and unpack the file.

If you don't want to compress the file one by one manually, you can use the Lambda function



  • fires on every PUT object (file) in the trash
  • if the file is not yet compressed and if compression is useful, replace the original downloaded file with the compressed version
  • set content encoding of http headers to gzip

I wrote a GIST for this, it might inspire you to create your own process. See https://gist.github.com/psa-jforestier/1c74330df8e0d1fd6028e75e210e5042.

And don't forget to undo (= clean up) Cloudfront to apply your changes.

0


source


This is how I did it:

aws s3 cp - encoding 'gzip' content file.json.gz s3: //mybucket/file.json

It will now serve compressed data with the correct header.

0


source







All Articles