无法从Google云端存储下载文档

时间:2014-08-03 09:53:38

标签: google-app-engine google-cloud-storage

我能够使用java.But中的httpclient上传文档并从谷歌云存储中下载签名网址,当我在浏览器中放入相同的签名网址时,我无法下载该链接的文档。我正在获取跟随错误

  

我们计算的请求签名与您的签名不符   提供。检查您的Google密钥和签名方法。

但是当我在存储浏览器中标记共享公开复选框时,我可以从生成的签名网址下载。但是我想允许用户从浏览器下载文档而不将其标记为公开共享。 。 我想确认一些令人困惑的部分,如

要创建签名网址后没有Google帐户的用户可以访问该文档,我还必须在存储浏览器中选中公开共享复选框吗?

但我认为如果网址已签名则不应检查共享公开复选框,而没有Google帐户的用户可以访问该文档?但在我的情况下它不会发生。根据链接 https://developers.google.com/storage/docs/accesscontrol#About-CanonicalExtensionHeaders 它谈到了Canonicalized_Extension_Headers。所以我把我的请求标题  request.addHeader( “X-goog-ACL”, “公共读”);

这是我的代码

 // construct URL
        String url = "https://storage.googleapis.com/" + bucket + filename +
                "?GoogleAccessId=" + GOOGLE_ACCESS_ID +
                "&Expires=" + expiration +
                "&Signature=" + URLEncoder.encode(signature, "UTF-8");
        System.out.println(url);
        HttpClient client = new DefaultHttpClient();
        HttpPut request = new HttpPut(url);
        request.addHeader("Content-Type", contentType);
        request.addHeader("x-goog-acl","public-read");// when i put this i get error 
        request.addHeader("Authorization","OAuth 1/zVNpoQNsOSxZKqOZgckhpQ");




        request.setEntity(new ByteArrayEntity(data));

        HttpResponse response = client.execute(request); 

当我把request.addHeader(“x-goog-acl”,“public-read”)时,我得到错误 HTTP / 1.1 403禁止错误。  。但是,当我删除这一行时,它成功上传。似乎我需要设置     request.addHeader(“x-goog-acl”,“public-read”)使其可以公开访问,但是在我的代码上添加错误。

。有什么建议吗?

2 个答案:

答案 0 :(得分:1)

最终解决了它。

要从浏览器运行已烧录的网址,您必须设置HTTP标头。在https://developers.google.com/storage/docs/accesscontrol#Construct-the-String

Content_Type可选。如果您提供此值,则客户端(浏览器)必须将此HTTP标头设置为相同的值。最多的单词。

因此,如果您要为符号字符串提供Content_Type,则必须在浏览器http标头中提供相同的Content_Type。当我在浏览器标题中设置Content_Type时,此错误最终解决了

答案 1 :(得分:0)

这对我有用:

    set_include_path("../src/" . PATH_SEPARATOR . get_include_path());
    require_once 'Google/Client.php';

    function signed_storageURL($filename, $bucket, $p12_certificate_path, $access_id,         $method = 'GET', $duration = 3600 ) 
    { 
        $expires = time( ) + $duration*60; 
        $content_type = ($method == 'PUT') ? 'application/x-www-form-urlencoded' : ''; 
        $to_sign = ($method."\n"."\n".$content_type."\n".$expires."\n".'/'.$bucket.'/'.$filename); 
        $signature = '';

                $signer = new Google_Signer_P12(file_get_contents($p12_certificate_path), 'notasecret');
        $signature = $signer->sign($to_sign);
        $signature = urlencode( base64_encode( $signature ) );

        return ('https://'.$bucket.'.commondatastorage.googleapis.com/'.$filename.'?GoogleAccessId='.$access_id.'&Expires='.$expires.'&Signature='.$signature); 
    }

    $url = signed_storageURL(rawurlencode("áéíóú espaço & test - =.jpg"),'mybucket', 'mykey.p12','myaccount@developer.gserviceaccount.com');
    echo '<a href="'.$url.'">'.$url.'</a>';