使用google-api-php-client无法创建正确的signedurl [英] using google-api-php-client not able to create proper signedurl
问题描述
bucket在我的项目中,我正在将用户的文件上传到云存储中,因此当用户要下载该文件时,我想从google cloud中生成signdurl,以将对文件的访问限制为仅该特定用户,因此,我尝试了以下操作php代码可以做到这一点
bucketIn my project, I am uploading user's files to cloud storage, so when user wants to download that file I want to generate signedurl from google cloud, to limit access to the files to that specific user only, So I have tried following php code to do that
`
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作为输出
When I copy this url into browser's addressbar it shows me SignatureDoesNotMatch error, I get this XML as output
<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>
我无法理解我的代码出了什么问题...
I am not able to understand what is wrong with my code...
P.S.将文件粘贴到浏览器地址栏中" https://bucket.storage. googleapis.com/object_file.docx "
P.S. I am able to download the file when i paste this in browser addressbar "https://bucket.storage.googleapis.com/object_file.docx"
推荐答案
由于urlencoding的一些错误,我试图访问的文件的名称中有空格,我无法为其创建适当的URL ...
The file I was trying to access has spaces in its name due to some bugs with urlencoding I wasn't able to create proper url for it ...
因此,对于没有空格的文件,我可以正常使用
So for files without spaces works fine with me
参考-> 查看全文