Google 云存储 Public 无需缓存即可访问

Google Cloud Storage Public Access Without Caching

我在 Google-Appengine 上创建了一个网站。为了降低 Google-Cloud-SQL 的成本,我的 API 从 [=] 生成 public json-files 42=] 在我的 Google-Cloud-Storage 中应该被我的 Android-App 和我的网站(通过 ajax)阅读。

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

如果我在 Developers Console 中上传文件手册,我可以通过单击 "Edit Metadata" 并将 Cache-Control 设置为 private, max-age=0,no-transform 来更改此行为(见下图)。然后,如果我尝试访问此文件,我会得到想要的 header private, max-age=0,no-transform

但在我的应用程序中,json-files 是由 PHP 生成的,我总是得到 header 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);

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

related question, you may need to set Cache-Control explicitly/separately in addition to enable_cache and read_cache_expiry_seconds; in particular, the options you are setting control the App Engine side of caching into memcache, whereas the Cache-Control setting is 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']
];