3
votes

I want to be able to support Brotli and Gzip encoding for static assets hosted with Google Cloud Storage. To do this I want to encode files before they are uploaded as <filename>, <filename>.gz and <filename>.br. The issue is, I can't find a way to redirect a request with Accept-Encoding to the correct file.

I have looked into using:

  • Cloud Functions to somehow redirect incoming requests (similar to AWS Cloudfront Lambda), but it does not seem to be supported
  • Load Balancer to redirect requests to different buckets, but as far as I could see it can only redirect based on hostname/path to different buckets
  • Cloud CDN, but it does not seem to have any functionality that helps with this

Requests/Response examples

Assume bucket example-bucket contains the following files:

library.js
library.js.gz
library.js.br
Example 1
GET http://storage.googleapis.com/example-bucket/library.js

Accept-Encoding: gzip, deflate, br
Content-Encoding: br

<Contents of http://storage.googleapis.com/example-bucket/library.js.br>
Example 2
GET http://storage.googleapis.com/example-bucket/library.js

Accept-Encoding: gzip, deflate
Content-Encoding: gzip

<Contents of http://storage.googleapis.com/example-bucket/library.js.gz>

Is there a way to accomplish the above in a manner that is simple, performant and cost effective? I realize it's possible to just host my own server via App Engine, and let that take care of routing requests to the bucket, but is this the only way?

1
Cloud Storage won't do this for you. You will have to write your own code on your own endpoint to make these sorts of decisions. If you're having a trouble with a specific solution, that would be something else to ask a question about. - Doug Stevenson
@DougStevenson I was asking since this is possible to do on AWS via the Lambda@edge. Was hoping to avoid hosting an application for this, as they can easily become the bottleneck (compared to CDN and static hosted files). - Csvn

1 Answers

0
votes

Lambda@edge is a way to do it but the simple way is to Whitelist the Accept-encoding header on CloudFront so CloudFront can pass it to Origin and you can have the Configuration on Origin to serve file based on the Accept-encoding header.

If you planning to use Lambda@edge, I would suggest using the Origin request function. 1. Whitelist the Accept-encoding header 2.Origin request function to read the header value and change the request URI path.