【问题标题】:using google-api-php-client not able to create proper signedurl使用 google-api-php-client 无法创建正确的签名 URL
【发布时间】: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


    【解决方案1】:

    查看此部分:

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

    这是 GCS 根据传入请求确定您将签署的字符串。在此过程中,换行符在某处被剥离,但那里有三件事:GET 一词、时间戳和对象的路径。

    您的代码有一个该字符串没有的东西:Content_Type。用户正在下载对象,因此他们没有提供内容类型。将该行留空。

    【讨论】:

    • 我将 $content_type 留空,在代码中如果未放置则将其设置为空...我无法理解此代码有什么问题,我是 GCS 的新手, 请您在适当的地方指出我...
    • 我要签名的字符串是 GET\n\n\n1409582445\n/bucket/object_file.docx
    • 假设服务帐户拥有或有权访问该文件,是的。但是,如果您的问题是权限被拒绝,那将是另一个错误。
    • 如此链接中所述,如果我尝试使用 gsutil 生成 signurl,则会显示服务帐户没有权限的错误,stackoverflow.com/questions/25562798/… ..... 使用 signurl 在此链接上查看我的执行oi60.tinypic.com/2uhlqgp.jpg
    • 啊,你的文件名里有空格?是的,您需要用 %20 替换它。尝试使用 PHP rawurlencode() 函数。
    【解决方案2】:

    由于 urlencoding 的一些错误,我尝试访问的文件的名称中有空格我无法为其创建正确的 url ...

    所以对于没有空格的文件我可以正常工作

    参考 -> Using google cloud storage and gsutil not able to generate valid signedurl

    【讨论】:

      猜你喜欢
      • 2016-05-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-04-10
      • 2020-06-29
      相关资源
      最近更新 更多