簡體   English   中英

使用google-api-php-client無法創建正確的signedurl

[英]using google-api-php-client not able to create proper signedurl

bucket在我的項目中,我正在將用戶的文件上傳到雲存儲中,因此當用戶要下載該文件時,我想從google cloud生成signdurl,以僅允許該特定用戶訪問文件,因此我嘗試了以下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");
?>`

當我將此網址復制到瀏覽器的地址欄中時,它顯示出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 ”后,我可以下載文件

查看本節:

<StringToSign>
    GET 1409582445 /bucket/object_file.docx
</StringToSign>

這是GCS根據傳入請求決定您將簽名的字符串。 換行符一直被剝離,但是這里有三件事:單詞GET,時間戳和對象的路徑。

您的代碼具有此字符串沒有的一件事:Content_Type。 用戶正在下載對象,因此他們沒有提供內容類型。 將該行留空。

由於urlencoding的一些錯誤,我試圖訪問的文件的名稱中有空格,因此我無法為其創建適當的網址...

因此,對於沒有空格的文件,我可以正常使用

參考-> 使用Google雲存儲和gsutil無法生成有效的SignedURL

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM