简体   繁体   中英

Set Cache-Control on Google Cloud Storage bucket

I have a bucket with public images that are frequently updated and would like to disable the default cache duration of 3600. (Eg "Cache-Control:private, max-age=0, no-transform")

Can the Cache-Control be set on a file while uploading it with PHP?

Is there a way to set the default Cache-Control for all future files in the bucket (like with ACL that all files are public)?

Right now I can only set it Cache-Control with gsutil and only for files currently on storage.

You can set the Cache-Control while uploading an object - I suggest you use gsutil -D to see an example of the request it generates to do this, and then translate that to PHP:

gsutil -D -h Cache-Control:"Cache-Control:private, max-age=0, no-transform" \
    cp file gs://your-bucket/file

There's no way to set a default Cache-Control for the bucket.

Here is an example I created after finding your question. In the below I want the file I upload to not be cached:

$this->bucket->upload(
  $content,
  [
    'name' => $name,
    'metadata' => [
      'cacheControl' => "public, max-age=0"
    ]
  ]
);

I worked this out by:

  1. Going to the repo: https://github.com/googleapis/google-cloud-php-storage
  2. That lead to the official docs: https://cloud.google.com/storage/docs
  3. I found the PHP reference: https://googleapis.github.io/google-cloud-php/#/docs/google-cloud/v0.122.0/storage/storageclient
  4. Which contained the bucket's upload reference: https://googleapis.github.io/google-cloud-php/#/docs/google-cloud/v0.122.0/storage/bucket?method=upload
  5. Which recommends using the JSON API reference for metadata https://cloud.google.com/storage/docs/json_api/v1/objects/insert#request-body
  6. Which links to a rfc spec doc for cache control https://tools.ietf.org/html/rfc7234#section-5.2

All of the above were useful resources, but hopefully my answer will be more succinct for future "Googlers".

Max: What's the typo you're referring to? When I run the command it works, and I see the specified Cache-Control in the debug output:

% gsutil -D -h Cache-Control:"Cache-Control:private, max-age=0, no-transform" cp file gs://my-bucket/file

...

INFO 0823 10:31:19.517297 base_api.py] Making http POST to https://www.googleapis.com/upload/storage/v1/b/my-bucket/o?fields=generation%2Ccrc32c%2Cmd5Hash%2Csize&alt=json&prettyPrint=True&uploadType=multipart INFO 0823 10:31:19.517704 base_api.py] Headers: {'accept': 'application/json', 'accept-encoding': 'gzip, deflate', 'content-length': '444', 'content-type': "multipart/related; boundary='===============6935917235018049421=='", 'user-agent': 'apitools gsutil/4.5 (darwin)'} INFO 0823 10:31:19.517773 base_api.py] Body: --===============6935917235018049421== Content-Type: application/json MIME-Version: 1.0

{"cacheControl": "Cache-Control:private, max-age=0, no-transform", "bucket": "my-bucket", "contentType": "text/x-c++", "name": "file", "contentLanguage": "en"} --===============6935917235018049421== Content-Type: text/x-c++ MIME-Version: 1.0 Content-Transfer-Encoding: binary

...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM