gpt4 book ai didi

java - 创建签名 URL 以将对象放入 Google Storage

转载 作者:行者123 更新时间:2023-11-30 02:40:37 25 4
gpt4 key购买 nike

我正在努力创建签名 URL 以将文件上传到 Google 存储。首先,我使用 JSON key 获取私钥和​​ clientId:

AuthCredentials.ServiceAccountAuthCredentials serviceAccountAuthCredentials = AuthCredentials.ServiceAccountAuthCredentials.createForJson(json_resource.getInputStream());
PrivateKey key = serviceAccountAuthCredentials.credentials().getPrivateKey();
Signature signer = Signature.getInstance("SHA256withRSA");
signer.initSign(key);

之后,创建要签名的字符串:

String upload_uri = "PUT\n\n" + expiration +
"\n/" + bucketName + "/" + folderPath + "/" + fileName;

然后签名字符串:

signer.update(stringToSign.getBytes("UTF-8"));
byte[] rawSignature = signer.sign();
String signature = new String(Base64.encodeBase64(rawSignature, false), "UTF-8");

然后使用签名字符串编写 URL:

final String clientId = serviceAccountAuthCredentials.account();
String url = "http://storage.googleapis.com/" +
bucketName + "/" + folderPath + "/" + fileName +
"?GoogleAccessId=" + clientId +
"&Expires=" + expiration +
"&Signature=" + URLEncoder.encode(signature, "UTF-8");

使用此 URL 我收到错误:

SignatureDoesNotMatchThe request signature we calculated does not match the signature you provided. Check your Google secret key and signing method.GET 1485340222074 /bucketName/fileName

最佳答案

您好,请检查我这边的以下工作代码。我觉得您缺少一些参数作为 get 或 token:`

    Calendar calendar = new GregorianCalendar();
Long lExpireDate = Long.valueOf(calendar.getTime().getTime() + 7*86400000);//7 days validity
String strExpireDate = fromDateLongtoStringISO8601(lExpireDate);

String HTTP_Verb = "GET";
String Content_MD5 = "";
String Content_Type = "";//"image/jpeg";
String Expiration = String.valueOf(lExpireDate/1000);
String Canonicalized_Resource = "/"+bucketName+"/"+objectName;
String StringToSign = HTTP_Verb + "\n" +
Content_MD5 + "\n" +
Content_Type + "\n" +
Expiration + "\n" +
Canonicalized_Resource;


byte[] blob = StringToSign.getBytes();
String BASE_URL = "https://storage.googleapis.com"+Canonicalized_Resource;

ArrayList<String> scopes = new ArrayList<>();
scopes.add(SQLAdminScopes.CLOUD_PLATFORM);
appIdentity = AppIdentityServiceFactory.getAppIdentityService();
AppIdentityService.GetAccessTokenResult accessToken = appIdentity.getAccessToken(scopes);

AppIdentityService.SigningResult result = appIdentity.signForApp(blob);
byte[] signatureByte = result.getSignature();
String urlEncoded = com.google.api.client.util.Base64.encodeBase64String(signatureByte);
String strURLBase64Encoded = URLEncoder.encode(urlEncoded);

String GOOGLE_ACCESS_STORAGE_ID = appIdentity.getServiceAccountName();
//GOOGLE_ACCESS_STORAGE_ID contains the email form of the client ID.

String concatenatedURL = BASE_URL + "?GoogleAccessId=" + GOOGLE_ACCESS_STORAGE_ID
+ "&Expires=" + strExpireDate
+ "&access_token=" + URLEncoder.encode(accessToken.getAccessToken())
+ "&Signature=" + strURLBase64Encoded;
entity.setProperty(Media.URLD, concatenatedURL);

关于java - 创建签名 URL 以将对象放入 Google Storage,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41849538/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com