Reza Shahbazi
Reza Shahbazi

Reputation: 451

Set Google Storage Bucket's default cache control

Is there any way to set Bucket's default cache control (trying to override the public, max-age=3600 in bucket level every time creating a new object)

Similar to defacl but set the cache control

Upvotes: 24

Views: 17584

Answers (6)

prout.james
prout.james

Reputation: 109

I know that this is quite an old question and you're after a default action (which I'm not sure exists), but the below worked for me on a recent PHP project after much frustration:

$object = $bucket->upload($tempFile, [
            'predefinedAcl' => "PUBLICREAD",
            'name' => $destination,
            'metadata' => [
                'cacheControl' => 'Cache-Control: private, max-age=0, no-transform',
            ]
        ]);

Same can be applied in node:

const storage = new Storage();

const bucket = storage.bucket(BUCKET_NAME);

const blob = bucket.file(FILE_NAME);

const uploadProgress = new Promise((resolve, reject) => {
    const blobStream = blob.createWriteStream();

    blobStream.on('error', err => {
      reject(err);

      throw new Error(err);
    });

    blobStream.on('finish', () => {
      resolve();
    });

    blobStream.end(file.buffer);
});

await uploadProgress;

if (isPublic) {
  await blob.makePublic();
}

blob.setMetadata({ cacheControl: 'public, max-age=31536000' });

Upvotes: 4

sandes
sandes

Reputation: 2277

Using gsutil

  • -h: Allows you to specify certain HTTP headers
  • -r: Recursive
  • -m: To performing a sequence of gsutil operations that may run significantly faster.

gsutil -m setmeta -r -h "Cache-control:public, max-age=259200" gs://bucket-name

Upvotes: 16

Ajinkya Tupkar
Ajinkya Tupkar

Reputation: 389

If someone is still looking for an answer, one needs to set the metadata while adding the blob. For those who want to update the metadata for all existing objects in the bucket, you can use setmeta from gsutil - https://cloud.google.com/storage/docs/gsutil/commands/setmeta

You just need to do the following :

gsutil setmeta -r -h "Cache-control:public, max-age=12345" gs://bucket_name

Upvotes: 28

anon6789
anon6789

Reputation: 179

It is possible to write a Google Cloud Storage Trigger.

This function sets the Cache-Control metadata field for every new object in a bucket:

from google.cloud import storage

CACHE_CONTROL = "private"

def set_cache_control_private(data, context):
    """Background Cloud Function to be triggered by Cloud Storage.
       This function changes Cache-Control meta data.

    Args:
        data (dict): The Cloud Functions event payload.
        context (google.cloud.functions.Context): Metadata of triggering event.
    Returns:
        None; the output is written to Stackdriver Logging
    """

    print('Setting Cache-Control to {} for: gs://{}/{}'.format(
            CACHE_CONTROL, data['bucket'], data['name']))
    storage_client = storage.Client()
    bucket = storage_client.get_bucket(data['bucket'])
    blob = bucket.get_blob(data['name'])
    blob.cache_control = CACHE_CONTROL
    blob.patch()

You also need a requirements.txt file for the storage import in the same directory. Inside the requirements there is the google-cloud-storage package:

google-cloud-storage==1.10.0

You have to deploy the function to a specific bucket:

gcloud beta functions deploy set_cache_control_private \
    --runtime python37 \
    --trigger-resource gs://<your_bucket_name> \
    --trigger-event google.storage.object.finalize

For debugging purpose you can retrieve logs with gcloud command as well:

gcloud functions logs read --limit 50

Upvotes: 8

xav
xav

Reputation: 5628

If you're using a python app, you can use the option "default_expiration" in your app.yaml to set a global default value for the Cache-Control header: https://cloud.google.com/appengine/docs/standard/python/config/appref

For example:

runtime: python27   
api_version: 1   
threadsafe: yes

default_expiration: "30s"

Upvotes: 0

jterrace
jterrace

Reputation: 67163

There is no way to specify a default cache control. It must be set when creating the object.

Upvotes: 7

Related Questions