bucketIn我的项目,我将用户的文件上传到云存储中,所以当用户想要下载该文件时,我想从google生成签名器,以限制对文件的访问权限仅限于该特定用户,因此我尝试使用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>我无法理解我的代码出了什么问题.
当我将文件粘贴到浏览器地址栏"file.docx“中时,我可以下载该文件。
发布于 2014-09-12 12:22:31
我试图访问的文件在其名称中有空格,因为我无法为它创建正确的url .
因此,对于没有空格的文件,我可以很好地工作。
参考-> Using google cloud storage and gsutil not able to generate valid signedurl
发布于 2014-09-01 18:41:52
请参阅本节:
<StringToSign>
GET 1409582445 /bucket/object_file.docx
</StringToSign>这是GCS根据传入的请求决定要签名的字符串。在这个过程中,新行正在被剥离,但是有三件事:单词GET、时间戳和指向对象的路径。
您的代码有一个字符串没有的东西: Content_Type。用户正在下载一个对象,所以他们没有提供内容类型。把那行空着。
https://stackoverflow.com/questions/25608150
复制相似问题