Google 云存储:如何重置边缘缓存?
Google cloud storage: How can I reset edge cache?
我更新了一张图片(来自 PHP),但仍然下载了旧版本的图片。
如果我在GCS控制台上下载镜像,我可以下载新版本的镜像。但是,这个url低于returns旧版本。
https://storage.googleapis.com/[bucket名称]/样本-image.png
旧图像似乎在 Google 的边缘缓存中。
有些文章说我应该删除图像对象,然后插入新的图像对象,以便清除边缘缓存。
有人知道吗?
更新 1
这是我在 GCE 上的 PHP 代码。
$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,
]);
好像只有这几个参数有效。我不认为我可以设置任何缓存设置。
// 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...? (或者可能需要安装)
$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);
How do I upload images to the Google Cloud Storage from PHP form?
更新 2
API 文档显示请求正文的 cacheControl 属性。我想直接使用 API(不是通过 SDK)是一种方法。我试试看
https://cloud.google.com/storage/docs/json_api/v1/objects/insert
cacheControl string Cache-Control directive for the object data. writable
我想我终于找到了!
$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']
);
Set Cache-Control php client on Google Cloud Storage Object
我们可以查看结果
gsutil ls -L gs://...
默认情况下,如果所有匿名用户都可以公开访问 object,并且您没有另外指定 cacheControl 设置,则 GCS 将提供 3600 秒的 Cache-Control header,或 1 小时。如果您正在获取过时的 object 数据并且没有弄乱缓存控制设置,我假设您正在提供可公开访问的 objects。不过,我不确定 Google 本身是否正在缓存您的 object 数据,或者您和 Google 之间是否存在其他缓存。
将来,您可以通过显式设置更短的 Cache-Control header 来解决此问题,可以使用 cacheControl 设置在 per-object 的基础上进行控制。
现在,您可能可以通过添加一些额外的 URL 查询参数来解决这个问题,例如 ?ignoreCache=1
更多:https://cloud.google.com/storage/docs/xml-api/reference-headers#cachecontrol
我更新了一张图片(来自 PHP),但仍然下载了旧版本的图片。
如果我在GCS控制台上下载镜像,我可以下载新版本的镜像。但是,这个url低于returns旧版本。
https://storage.googleapis.com/[bucket名称]/样本-image.png
旧图像似乎在 Google 的边缘缓存中。
有些文章说我应该删除图像对象,然后插入新的图像对象,以便清除边缘缓存。
有人知道吗?
更新 1
这是我在 GCE 上的 PHP 代码。
$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,
]);
好像只有这几个参数有效。我不认为我可以设置任何缓存设置。
// 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...? (或者可能需要安装)
$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);
How do I upload images to the Google Cloud Storage from PHP form?
更新 2
API 文档显示请求正文的 cacheControl 属性。我想直接使用 API(不是通过 SDK)是一种方法。我试试看
https://cloud.google.com/storage/docs/json_api/v1/objects/insert
cacheControl string Cache-Control directive for the object data. writable
我想我终于找到了!
$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']
);
Set Cache-Control php client on Google Cloud Storage Object
我们可以查看结果
gsutil ls -L gs://...
默认情况下,如果所有匿名用户都可以公开访问 object,并且您没有另外指定 cacheControl 设置,则 GCS 将提供 3600 秒的 Cache-Control header,或 1 小时。如果您正在获取过时的 object 数据并且没有弄乱缓存控制设置,我假设您正在提供可公开访问的 objects。不过,我不确定 Google 本身是否正在缓存您的 object 数据,或者您和 Google 之间是否存在其他缓存。
将来,您可以通过显式设置更短的 Cache-Control header 来解决此问题,可以使用 cacheControl 设置在 per-object 的基础上进行控制。
现在,您可能可以通过添加一些额外的 URL 查询参数来解决这个问题,例如 ?ignoreCache=1
更多:https://cloud.google.com/storage/docs/xml-api/reference-headers#cachecontrol