Jokus
Jokus

Reputation: 473

Google Cloud Storage Public Access Without Caching

I created a website on Google-Appengine. To reduce the costs of Google-Cloud-SQL my API generates public json-files from Cloud-SQL in my Google-Cloud-Storage that should be read by my Android-App and my Website (via ajax).

The Problem is, that the files of Google-Cloud-Storage are automatically cached for an hour, but I want to disable caching for specific files.

If I upload a file manual in the Developers Console I may change this behaviour with a click on "Edit Metadata" and setting Cache-Control to private, max-age=0,no-transform (see the following picture). Then if I try to access this file, I get the wanted header private, max-age=0,no-transform [Edit Metadata manual

But in my application the json-files are generated by PHP and I always get the header cache-control → public, max-age=3600. I create the files with the following code (Here is the official docs: 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);

Has anybody an idea why caching is not turned off?

Upvotes: 5

Views: 3081

Answers (1)

Dennis Huo
Dennis Huo

Reputation: 10677

Mentioned in a 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 itself. So you want:

$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']
];

Upvotes: 5

Related Questions