zon*_*ono 9 google-cloud-storage
我更新了一张图片(来自PHP),但仍然下载了旧版本的图片.
如果我在GCS控制台上下载图像,我可以下载新版本的图像.但是,下面的url会返回旧版本.
https://storage.googleapis.com/[bucket name] /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,
]);
Run Code Online (Sandbox Code Playgroud)
似乎只有这些参数才有效.我认为我不能设置任何缓存设置.
// 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'),
);
Run Code Online (Sandbox Code Playgroud)
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);
Run Code Online (Sandbox Code Playgroud)
更新2
API doc显示Request body的cacheControl属性.我想直接使用API(不是通过SDK)是一种方式.我会尝试.
https://cloud.google.com/storage/docs/json_api/v1/objects/insert
cacheControl string Cache-Control directive for the object data. writable
Run Code Online (Sandbox Code Playgroud)
我想我终于找到了!
$ obj-> setCacheControl( '无缓存');
更新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']
);
Run Code Online (Sandbox Code Playgroud)
在Google云端存储对象上设置Cache-Control php客户端
我们可以查看结果
gsutil ls -L gs://...
Run Code Online (Sandbox Code Playgroud)
Bra*_*ugh 18
默认情况下,如果所有匿名用户都可以公开访问对象,并且您没有指定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
| 归档时间: |
|
| 查看次数: |
8782 次 |
| 最近记录: |