【发布时间】: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)
-
嗨。是的,我清除了浏览器缓存。 (我也试过秘密模式浏览器,禁用缓存)