Google Cloud已签署Url + SignatureDoesNotMatch

时间:2017-02-10 00:04:51

标签: java google-cloud-storage

尝试获取用于在Google存储空间中下载文件的已签名网址存在问题。获取SignatureDoesNotMatch错误。

private  String signString(GoogleCredential credential, String stringToSign) throws Exception {

// sign data
Signature signer = Signature.getInstance("SHA256withRSA");
signer.initSign(credential.getServiceAccountPrivateKey());
signer.update(stringToSign.getBytes("UTF-8"));
byte[] rawSignature = signer.sign();

return new String(org.apache.commons.codec.binary.Base64.encodeBase64(rawSignature, false), "UTF-8");

}

//Here is the code to get signed url
private getFileUrl(GoogleCredential credential,String bucketName, String filePath) {
    String signedParam = signString(credential, "GET\n\n\n"+expiration+"\n"+bucketName+"/"+filePath);

   // construct URL
   String url = "http://storage.googleapis.com/" + bucketName + "/" + filePath +
                "?GoogleAccessId=" + credential.getServiceAccountId() +
                "&Expires=" + expiration +
                "&Signature=" + URLEncoder.encode(signedParam, "UTF-8");
   return url;
}

我在这里遗漏了什么?苦苦挣扎了一会儿。非常感谢任何帮助。

感谢。

1 个答案:

答案 0 :(得分:0)

签名网址的GET路径中的存储桶名称应以“/”开头。这解决了这个问题。

String signedParam = signString(credential, "GET\n\n\n"+expiration+"\n/"+bucketName+"/"+filePath);