Invalidate Google Cloud CDN cache from the backend

こ雲淡風輕ζ 提交于 2020-06-27 19:48:07

问题


After few days of research and reading documentation, I'm almost sure It's nearly impossible, but still I would like to ask:

The goal is to invalidate all cached content in GCloud CDN on demand (due to headers changes) on the 3rd party backend, which does not use that CDN. Using gsuite it can be achieved by using the following command:

gcloud compute url-maps invalidate-cdn-cache web --path '/*' --async

But the problem is that this command requires us to login to google account via browser with client's credentials, which makes it absolutely worthless.

The sad story is that it seems Google has pretty rich API for its other services, but for CDN there is no API :(

The idea is to accept user's credentials and invalidate that cache using them. Is it even possible?


回答1:


So, I was wrong about impossibility. I found the respective REST API method (https://cloud.google.com/compute/docs/reference/rest/v1/urlMaps/invalidateCache):

POST https://www.googleapis.com/compute/v1/projects/{project}/global/urlMaps/{resourceId}/invalidateCache

And here is a sample Ruby code to work with it using official gem:

require 'google/apis/compute_v1'

service = Google::Apis::ComputeV1::ComputeService.new
service.authorization =
  Google::Auth::ServiceAccountCredentials
    .make_creds(
      json_key_io: File.open('/path/to/credentials.json'),
      scope:       [Google::Apis::ComputeV1::AUTH_COMPUTE]
    )
service.invalidate_url_map_cache(
   'some-gcloud-project-id',
   'some-url-map',
   nil,
   request_id: SecureRandom.uuid
)


来源:https://stackoverflow.com/questions/49715710/invalidate-google-cloud-cdn-cache-from-the-backend

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!