【问题标题】:Google Cloud Storage Public Access Without Caching没有缓存的谷歌云存储公共访问
【发布时间】:2015-03-28 12:15:24
【问题描述】:

我在 Google-Appengine 上创建了一个网站。为了降低 Google-Cloud-SQL 的成本,我的 API 从我的 Google-Cloud-Storage 中的 Cloud-SQL 生成 public json-files应该由我的 Android 应用程序和我的网站(通过 ajax)读取。

问题是,Google-Cloud-Storage 的文件会自动缓存一个小时,但我想禁用特定文件的缓存。

如果我在 Developers Console 中上传文件手册,我可以通过单击“编辑元数据”并将 Cache-Control 设置为 private, max-age=0,no-transform 来更改此行为(参见下图)。然后如果我尝试访问这个文件,我会得到想要的标题private, max-age=0,no-transform

但在我的应用程序中,json 文件是由 PHP 生成的,我总是得到标题 cache-control → public, max-age=3600。我使用以下代码创建文件(这里是官方文档:Docs):

$options = ['gs' => [
    'Content-Type' => 'text/json',
    'acl' => 'public-read',
    'enable_cache' => false,
    'read_cache_expiry_seconds' => 0]
];
$ctx = stream_context_create($options);
file_put_contents($url,$content,0,$ctx);

有人知道为什么没有关闭缓存吗?

【问题讨论】:

    标签: php google-app-engine caching google-cloud-storage


    【解决方案1】:

    related question 中提到,除了enable_cacheread_cache_expiry_seconds 之外,您可能还需要显式/单独设置Cache-Control;特别是,您设置的选项控制 App Engine 端缓存到 memcache,而 Cache-Control 设置是 specific to Google Cloud Storage 本身。所以你想要:

    $options = ['gs' => [
        'Content-Type' => 'text/json',
        'acl' => 'public-read',
        'enable_cache' => false,
        'read_cache_expiry_seconds' => 0,
        'Cache-Control' => 'private, max-age=0,no-transform']
    ];
    

    【讨论】:

    • 如果使用该流上下文,我仍然会得到标题“public,max-age=3600”。看看这个 PHP 生成的文件:link 如相关问题中所述,如果您将 Get-values 添加到链接中,您将获得实际的(未缓存的)文件。有什么想法吗?
    • 对不起,我忘了给你的答案投票!您的回答是正确的,但 Cache-Control 标头不适用于 appengine 版本 1.9.17。此错误已在 1.9.19 中修复,所以现在可以使用了! (code.google.com/p/googleappengine/issues/detail?id=11651)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-09-06
    • 2019-08-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-12-12
    相关资源
    最近更新 更多