【发布时间】:2020-12-18 18:46:09
【问题描述】:
我有一个PHP文件可以将图片上传到谷歌云存储,但是每次用户更改他们的个人资料图片时,它都会长时间显示之前的图片,所以他们认为图片根本没有更新。我想知道避免这个问题需要什么缓存和元数据配置。
我不希望使用“自定义元数据”,因为云文档 https://cloud.google.com/storage/docs/metadata 上的这条消息 ->“请注意,使用自定义元数据会产生存储和网络成本。”
这是我的代码:
require 'vendor/autoload.php';
use Google\Cloud\Storage\StorageClient;
$storage = new StorageClient([
'keyFilePath' => $keypath
]);
$storage->registerStreamWrapper();
$bucket = $storage->bucket($url);
$filename = $_FILES['profile_picture']['name'];
$newFileName = $_POST['user_id'].'_profile.jpg';
$bucket->upload(
fopen($_FILES["profile_picture"]["tmp_name"], 'r'),
[
'name' => $dir.$newFileName,
'metadata' => [
'cacheControl' => 'Cache-Control: no-cache, max-age=0',
]
]
);
我不知道 CacheControl 是否足够(以及 sintax 是否正确)或者我需要包含 Custom-Time 以及它的 sintax 是什么。
我的尝试:
$bucket->upload(
fopen($_FILES["profile_picture"]["tmp_name"], 'r'),
[
'name' => $dir.$newFileName,
'metadata' => [
'cacheControl' => 'Cache-Control: no-cache, max-age=0',
'Custom-Time' => date('Y-m-d\TH:i:s.00\Z');
]
]
);
文档中没有任何关于 PHP 语法的内容:https://cloud.google.com/storage/docs/metadata
请帮忙
【问题讨论】:
标签: php caching syntax google-cloud-storage