繁体   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