关于缓存控制

时间:2017-03-17 19:14:40

标签: php google-cloud-storage

有没有办法将存储桶中的所有文件设置为公共和公共的默认缓存控制,max-age = 7776000?如果没有,我应该如何调整我的PHP代码以使用分段上传?

这是我的媒体上传代码:

$file_up            = file_get_contents($file['full_path']);

clearstatcache();                        

$file['size']       = filesize($file['full_path']);
$access_token       = $this->googleapi->get_access_token();                             

$header = array(
    'Content-Type: audio/mp3',
    'Content-Length: '.$file['size'].''
);

$url    = "https://www.googleapis.com/upload/storage/v1/b/MY_BUCKET/o?uploadType=media&predefinedAcl=publicRead&name=".$file['name_ext'].'&access_token='.$access_token;

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_TIMEOUT, 120);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_POSTFIELDS, $file_up);
curl_setopt($ch, CURLOPT_HTTPHEADER, $header); 
$dxa = curl_exec($ch);
curl_close($ch);

1 个答案:

答案 0 :(得分:0)

您需要设置" cacheControl"属性作为上传的一部分。为此,您需要执行multipartresumable uploadType,并将cacheControl设置为您指定的元数据的一部分。请按照此处的指南进行操作:https://cloud.google.com/storage/docs/json_api/v1/how-tos/upload

也就是说,通过PHP SDK

可以更容易地做到这一点
$options = [
    'name' => '/images/new-name.jpg',
    'metadata' => [
       'cacheControl' => 'public,max-age=7776000'
]];
$object = $bucket->upload(
    fopen(__DIR__ . '/image.jpg', 'r'),
    $options);