设置Google Storage Bucket的默认缓存控件

时间:2015-08-21 18:57:15

标签: google-cloud-storage

有没有办法设置Bucket的默认缓存控制(每次创建新对象时尝试覆盖桶级别中的<?php $cats = get_categories(); // Get categories ?> <?php if ($cats) : ?> <ul> <?php // Loop through categories to print name and count excluding CPTs ?> <?php foreach ($cats as $cat) { // Create a query for the category to determine the number of posts $category_id= $cat->term_id; $cat_query = new WP_Query( array( 'post_type' => 'post', 'posts_per_page' => -1, 'cat' => $category_id ) ); $count = $cat_query->found_posts; // Print each category with it's count as a list item echo "<li>" . $cat->name . " (" . $count . ")</li>"; } ?> <?php wp_reset_query(); // Restore global post data ?> </ul> <?php endif; ?>

与defacl类似但设置了缓存控制

6 个答案:

答案 0 :(得分:4)

可以写一个Google Cloud Storage Trigger

此函数为存储桶中的每个新对象设置Cache-Control元数据字段:

ValueError: operands could not be broadcast together with shapes (137141,) (62859,) 

对于同一目录中的存储导入,您还需要一个requirements.txt文件。需求内有google-cloud-storage软件包:

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()

您必须将功能部署到特定存储桶:

google-cloud-storage==1.10.0

出于调试目的,您还可以使用gcloud命令检索日志:

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

答案 1 :(得分:2)

如果某人仍在寻找答案,则需要在添加Blob时设置元数据。 对于那些想要更新存储桶中所有现有对象的元数据的用户,可以使用gitUrl = "https://whateverIsyourscmurl" //checkout in order to be able to execute git commands checkout([ $class: 'GitSCM', branches: [[name: '*/master']], credentialsId: 'bitbucket.service.user', userRemoteConfigs: [ [url: gitUrl, noTags: false] ] ]) def selectedTag = "1.0.0" //get all git tags sorted command = "git tag --sort=-v:refname" gitlog = sh(returnStdout: true, script: command).trim() //or bat() in windows def gitTags = gitlog.tokenize("\n"); //as there is no default value for "ChoiceParameter" we jus put the found element on top gitTags = gitTags.minus([selectedTag]); gitTags.remove(0) // gitlog also shows the command itself so remove the first element gitTags.add(0, selectedTag) ChoiceParameterDefinition currentTag = new ChoiceParameterDefinition( 'currentTag', gitTags as String[], "Git tag") returnValue = input message: 'Create workorder for ' + currentStage + ' ?', parameters: [currentTag], submitterParameter: 'approver' description: "whatever" -https://cloud.google.com/storage/docs/gsutil/commands/setmeta

中的setmeta

您只需要执行以下操作:

gsutil

答案 2 :(得分:1)

无法指定默认缓存控件。必须在创建对象时设置它。

答案 3 :(得分:1)

使用gsutil

  • -h :允许您指定某些HTTP标头
  • -r :递归
  • -m :执行一系列可能会明显加快运行速度的gsutil操作。

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

答案 4 :(得分:0)

如果您使用的是python应用程序,则可以使用app.yaml中的“default_expiration”选项为Cache-Control标题设置全局默认值:https://cloud.google.com/appengine/docs/standard/python/config/appref

例如:

runtime: python27   
api_version: 1   
threadsafe: yes

default_expiration: "30s"

答案 5 :(得分:0)

我知道这是一个很老的问题,您正在执行默认操作(我不确定是否存在),但是在无奈之后,以下内容对我最近的PHP项目有效:

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

可以在节点中应用相同的内容(尽管未经测试):

await storage.bucket(bucketName).upload(filename, {
      gzip: true,
      destination: destination,
      public: true,
      metadata: {
        cacheControl: "Cache-Control: private, max-age=0, no-transform"
      }
    });