Google云存储:如何重置边缘缓存? [英] Google cloud storage: How can I reset edge cache?
问题描述
我更新了一个映像(从PHP),但是仍然下载了该映像的旧版本.
I updated an image (from PHP) but still the old version of the image is downloaded.
如果我在GCS控制台上下载了该映像,则可以下载该映像的新版本.但是,下面的URL返回旧版本.
If I download the image on the GCS console, I can download the new version of the image. However, this url below returns the old version.
https://storage.googleapis.com/[bucket 名称]/sample-image. png
https://storage.googleapis.com/[bucket name]/sample-image.png
看来旧图像在Google的边缘缓存中.
It seems that the old image is on the Google's edge cache.
有些文章说我应该删除图像对象,然后插入新的图像对象,以便清除边缘缓存.
Some articles say that I should delete the image object then insert the new image object so that the edge cache is cleared.
有人知道吗?
更新1
这是我在GCE上的PHP代码.
This is my PHP code which is on GCE.
$obj = new \Google_Service_Storage_StorageObject();
$obj->setName($path . "/" . $name);
$client = new \Google_Client();
$client->useApplicationDefaultCredentials();
$client->addScope(\Google_Service_Storage::DEVSTORAGE_FULL_CONTROL);
$storage = new \Google_Service_Storage($client);
$bucket = 'sample.com';
$binary = file_get_contents($_FILES['files']['tmp_name']);
$fileInfo = new finfo(FILEINFO_MIME_TYPE);
$mimeType = $fileInfo->buffer($binary);
$storage->objects->insert($bucket, $obj, [
'name' => $path . "/" . $name,
'data' => $binary,
'uploadType' => 'media',
'mimeType' => $mimeType,
]);
似乎只有这些参数有效.我认为我无法设置任何缓存设置.
It seems that only these parameters are valid. I don't think I can set any cache settings.
// Valid query parameters that work, but don't appear in discovery.
private $stackParameters = array(
'alt' => array('type' => 'string', 'location' => 'query'),
'fields' => array('type' => 'string', 'location' => 'query'),
'trace' => array('type' => 'string', 'location' => 'query'),
'userIp' => array('type' => 'string', 'location' => 'query'),
'quotaUser' => array('type' => 'string', 'location' => 'query'),
'data' => array('type' => 'string', 'location' => 'body'),
'mimeType' => array('type' => 'string', 'location' => 'header'),
'uploadType' => array('type' => 'string', 'location' => 'query'),
'mediaUpload' => array('type' => 'complex', 'location' => 'query'),
'prettyPrint' => array('type' => 'string', 'location' => 'query'),
);
https://github.com/google/google-api-php-client/blob/master/src/Google/Service/Resource.php
我已经尝试过这种方式,但到目前为止还无法正常工作.这仅适用于GAE ...? (或者可能需要安装)
I tried this way but not work so far. This is for only GAE...? (Or mounting may be necessary)
$image = file_get_contents($gs_name);
$options = [ "gs" => [ "Content-Type" => "image/jpeg"]];
$ctx = stream_context_create($options);
file_put_contents("gs://<bucketname>/".$fileName, $gs_name, 0, $ctx);
我如何从PHP表单上传图像到Google Cloud Storage?
更新2
API文档显示了请求正文的cacheControl属性.我猜想直接使用API(而不是通过SDK)是一种方法.我会尝试的.
API doc shows cacheControl property of Request body. I guess that using API directly (not via SDK) is a way. I will try it.
https://cloud.google.com/storage/docs/json_api/v1/objects/insert
cacheControl string Cache-Control directive for the object data. writable
我想我终于找到了!
I think I found it finally!
$ obj-> setCacheControl('no-cache');
$obj->setCacheControl('no-cache');
更新3
$bucket_name = 'my-bucket';
$file = "xxx.html";
$infotowrite = "999";
$service = new Google_Service_Storage($client);
$obj = new Google_Service_Storage_StorageObject();
$obj->setName($file);
$obj->setCacheControl('public, max-age=6000');
$results = $service->objects->insert(
$bucket_name,
$obj,
['name' => $file, 'mimeType' => 'text/html', 'data' => $infotowrite, 'uploadType' => 'media']
);
在Google Cloud上设置Cache-Control php客户端存储对象
我们可以检查结果
gsutil ls -L gs://...
推荐答案
默认情况下,如果所有匿名用户均可公开访问该对象,并且您未指定cacheControl设置,则GCS将提供3600的Cache-Control标头秒或1小时.如果您正在获取过时的对象数据并且没有弄乱缓存控制设置,那么我认为您正在提供可公开访问的对象.我不确定Google本身是否在缓存您的对象数据,或者您与Google之间是否还有其他缓存.
By default, if an object is publicly accessible to all anonymous users and you do not otherwise specify a cacheControl setting, GCS will serve a Cache-Control header of 3600 seconds, or 1 hour. If you're getting stale object data and haven't been messing with cache control settings, I assume you're serving publicly accessible objects. I'm not sure if Google itself is caching your object data or if there's some other cache between you and Google, though.
将来,您可以通过显式设置较短的Cache-Control标头来解决此问题,该标头可以通过cacheControl设置在每个对象的基础上进行控制.
In the future, you can fix this by explicitly setting a shorter Cache-Control header, which can be controlled on a per-object basis with the cacheControl setting.
现在,您可以通过添加一些组成的额外URL查询参数来解决此问题,例如?ignoreCache = 1
Right now, you can probably get around this by tacking on some made up extra URL query parameter, like ?ignoreCache=1
更多: https://cloud.google.com/storage/docs/xml-api/reference-headers#cachecontrol
这篇关于Google云存储:如何重置边缘缓存?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!