【发布时间】:2014-10-25 19:39:33
【问题描述】:
bucket在我的项目中,我正在将用户的文件上传到云存储,所以当用户想要下载该文件时,我想从谷歌云生成signedurl,以限制对文件的访问仅限于该特定用户,所以我尝试了以下php代码来做到这一点
`
function storageURL( $id, $method = 'GET', $duration = 10 ) {
$expires = time( ) + $duration*60;
$content_type = ($method == 'PUT') ? 'application/x-www-form-urlencoded' : '';
$to_sign = ($method . "\n" .
/* Content-MD5 */ "\n" .
$content_type . "\n" .
$expires . "\n" . s
'/bucket/' . $id);
$signature = '';
$signer = new Google_Signer_P12(file_get_contents(__DIR__.'/'."key.p12"), "notasecret");
$signature = $signer->sign($to_sign);
$signature = Google_Utils::urlSafeB64Encode($signature);
return ('https://bucket.storage.googleapis.com/' . $id .
'?GoogleAccessId=' . 'MyServiceAccount@developer.gserviceaccount.com' .
'&Expires=' . $expires . '&Signature=' . $signature);
}
echo storageURL("object_file.docx");
?>`
当我将此 url 复制到浏览器的地址栏中时,它会显示 SignatureDoesNotMatch 错误,我会将此 XML 作为输出
<Error>
<Code>SignatureDoesNotMatch</Code>
<Message>
The request signature we calculated does not match the signature you provided. Check your Google secret key and signing method.
</Message>
<StringToSign>
GET 1409582445 /bucket/object_file.docx
</StringToSign>
</Error>
我无法理解我的代码出了什么问题...
附:当我将其粘贴到浏览器地址栏“https://bucket.storage.googleapis.com/object_file.docx”时,我可以下载该文件
【问题讨论】:
标签: php authentication google-cloud-storage google-api-php-client