Google CDN Connection to CDN create nosuch Key Errors

Viewed 1157

i uploaded images to google storage bucket and i am no trying to set the CDN using the load balancer to work.

Storage Status : Bucket Permissions : Storage Object Viewer - Reader assign to allUsers , Storage Legacy Bucket Reader assign To allUsers

File Status : Share Public is set and there is a public link

Load Balancer: Set to path /creatives/* on the host name

but i always get this msg:

 <Error>
 <Code>NoSuchKey</Code>
 <Message>The specified key does not exist.</Message>
 </Error>

what i notice is as soon as i build the path of /creatives/* there is another path build /* direct to the backend service of the auto scale group

am i missing here any settings?

2 Answers

So as i discovered Google CDN + Google http Load balancer works different from other CDN.

with a regular CDN you can direct the origin to you bucket HTTP address and work on the / structure. for example : Google CDN Bucket URL: googleapi.storage.com/my-bucket Folder structure: /1/1.jpg

Normal CDN origin will be pointing to googleapi.storage.com/my-bucket and you will get a new service endpoint for the CDN like: my-bucket.fastly.cdonservice.com and this call will work: my-bucket.fastly.cdonservice.com/1/1.jpg

but on google cloud what you are setting up is a path that is connected to the CDN service that you created on the backend part. So this is the big difference, lets assume that you created this path rule. host: www.googlecdnnonexplainedfeautres.com path: /images/* service: yourbackendservice (connected to the bucket you want to cache)

so you might assume this should work: www.googlecdnnonexplainedfeautres.com/images/1/1.jpg.

but NO .. after digging the logs you will find a 404 on the bucket because google will go and search this path on the bucket:

googleapi.storage.com/my-bucket/images/1/1.jpg.

wait , where did the images came ? i thought its a hook. no google take this as a static website root ( the thing you can check on and off on S3) so here its mandatory.

so how this should work ?

modify the folder structure to be like this :

Google CDN Bucket URL: googleapi.storage.com/my-bucket

Folder structure: images/1/1.jpg

and now you are good.

This link should work now :

www.googlecdnnonexplainedfeautres.com/images/1/1.jpg.

so before you commit a bucket to be used as CDN source for google just add another top folder that match with the path you set on the LB.

and of course .. permissions , allUsers , read and etc..

EnjoY!

You can use URL Rewrites to solve this problem.

This may not have existed back in 2017 but there's an option at least as of 2021.


Explanation:

The lb's default behavior is to pass the entire path after the host to Cloud Storage. This may seem incorrect, but it's a sane default (How is the load balancer supposed to know what part of the path you want to include or exclude?).

I was facing the same issue.

I connected mydomain.com/static/* to my cloud storage bucket static-assets.

Upon visiting mydomain.com/static/static-asset.jpg the load balancer would request cloud storage for an object with key static-assets/**static**/static-asset.jpg.

Since the object's actual key is static-assets/static-asset.jpg, this would return a NoSuchKey response.

The fix was the rewrite the path prefix of /static to /.

One way to configure this is through the Cloud Console Load Balancer UI--we can add an advanced path rule to rewrite the prefix.

Cloud Console GUI Showing Path Rule Details

Please note:

Important: The rewrite is prepended to the path as is. Full path rewrites are not supported. HTTP(S) Load Balancing only implements path prefix rewrites. For example, you can rewrite: host.name/path1/resource1 to host.name/path2/resource1. You cannot rewrite host.name/path1/resource1 to host.name/path1/resource2.

Read about URL Rewrites here.

Related