【问题标题】:Google cloud storage: How can I reset edge cache?谷歌云存储:如何重置边缘缓存?
【发布时间】:2016-05-09 02:56:40
【问题描述】:

我更新了一个图像(来自 PHP),但仍然下载了旧版本的图像。

如果我在 GCS 控制台上下载图像,我可以下载新版本的图像。但是,下面这个网址返回的是旧版本。

https://storage.googleapis.com/[bucketname]/sample-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://...

【问题讨论】:

  • 您清除浏览器缓存了吗? (使用 ctrl-f5)
  • 嗨。是的,我清除了浏览器缓存。 (我也试过秘密模式浏览器,禁用缓存)

标签: google-cloud-storage


【解决方案1】:

默认情况下,如果一个对象可供所有匿名用户公开访问,并且您没有另外指定 cacheControl 设置,GCS 将提供 3600 秒或 1 小时的 Cache-Control 标头。如果您正在获取陈旧的对象数据并且没有弄乱缓存控制设置,我假设您正在提供可公开访问的对象。不过,我不确定 Google 本身是否正在缓存您的对象数据,或者您和 Google 之间是否存在其他缓存。

将来,您可以通过显式设置更短的 Cache-Control 标头来解决此问题,该标头可以使用 cacheControl 设置在每个对象的基础上进行控制。

现在,您可能可以通过添加一些额外的 URL 查询参数来解决这个问题,例如 ?ignoreCache=1

更多:https://cloud.google.com/storage/docs/xml-api/reference-headers#cachecontrol

【讨论】:

  • 完美。这是我想知道的。非常感谢。
  • 有人说stream_context_create是一种可能的方式。你认为是吗? Google API PHP 客户端不允许设置缓存设置。你知道任何解决方法吗? "?ignoreCache=1" 没问题,但我不想更改我的代码。
  • 是的,Google 也在缓存存储桶中的数据,并且此缓存遵循 Cache-Control 设置,因此默认值为 1 小时。桶对象更新后,可能会随机获取新旧对象,直到缓存过期。
  • @Crashalot 这就是云(和 CDN)的工作方式。缓存可能遍布世界各地,您永远无法做出理想的解决方案。如果您需要立即更改,请在每次更改时上传一个新对象,并自行在您的应用中分发新对象 URL。
  • 强制使请求无效的最简单方法是附加一个带有 GUID 值的虚假 URL 查询参数。最规范正确的方法是在请求中包含“Cache-Control: no-cache”标头指令。特别是对于 GCS,您可以通过设置该对象的 cacheControl 属性来禁用该对象的缓存。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2017-10-06
  • 1970-01-01
  • 1970-01-01
  • 2016-10-12
  • 2020-09-10
  • 2015-05-09
相关资源
最近更新 更多