谷歌云存储公共访问无缓存

Jok*_*kus 5 php google-app-engine caching google-cloud-storage

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

\n\n

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

\n\n

如果我在开发人员控制台中上传文件手册,我可以通过单击“编辑元数据”并将缓存控制设置为private, max-age=0,no-transform(参见下图)来更改此行为。然后,如果我尝试访问该文件,我会得到想要的标头private, max-age=0,no-transform\n[编辑元数据手册

\n\n

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

\n\n
$options = [\'gs\' => [\n    \'Content-Type\' => \'text/json\',\n    \'acl\' => \'public-read\',\n    \'enable_cache\' => false,\n    \'read_cache_expiry_seconds\' => 0]\n];\n$ctx = stream_context_create($options);\nfile_put_contents($url,$content,0,$ctx);\n
Run Code Online (Sandbox Code Playgroud)\n\n

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

\n

Den*_*Huo 5

在相关问题中提到,Cache-Control除了enable_cache和之外,您可能还需要显式/单独设置read_cache_expiry_seconds特别是,您设置的选项控制缓存到memcache的 App Engine 端,而该Cache-Control设置特定于 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']
];
Run Code Online (Sandbox Code Playgroud)