gpt4 book ai didi

java - Azure存储服务Rest API : Get Container Metadata

转载 作者:太空宇宙 更新时间:2023-11-04 10:13:14 25 4
gpt4 key购买 nike

调用获取容器元数据时出现以下错误。

Response Code : 403

Response Message : Server failed to authenticate the request. Make sure the value of Authorization header is formed correctly including the signature.

StringToSign = "GET\n\n\n\n\n\n\n\n\n\n\n\nx-ms-date:" + date + "\nx-ms-version:" + "2014-02-14\n" + "/" + storageAccount + "/"+ "container-test"+"\nrestype:container\ncomp:metadata";

https://learn.microsoft.com/en-us/rest/api/storageservices/get-container-metadata

最佳答案

尝试以下代码。

public static void getContainerMetadata() throws Exception {
// Account info
String accountName = "accountName";
String accountKey = "accountKey";

// Request Uri and Method
String containerName = "containerName";
String requestUri = "https://"+accountName+".blob.core.windows.net/"+containerName+"?restype=container&comp=metadata";
HttpURLConnection connection = (HttpURLConnection) (new URL(requestUri)).openConnection();
connection.setRequestMethod("GET");

// Request Headers
// 1. x-ms-version
String serviceVersion = "2018-03-28";
// 2. x-ms-date
SimpleDateFormat fmt = new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss");
fmt.setTimeZone(TimeZone.getTimeZone("GMT"));
String date = fmt.format(Calendar.getInstance().getTime()) + " GMT";
// 3. Authorization
String authKeyFormat = "SharedKey";
String caHeader = "x-ms-date:"+date+"\nx-ms-version:"+serviceVersion+"\n";
String caResource = "/"+accountName+"/"+containerName+"\ncomp:metadata\nrestype:container";
String signStr = "GET\n\n\n\n\n\n\n\n\n\n\n\n"+caHeader+caResource;
String authorization = getAuthorization(accountName, authKeyFormat, signStr, accountKey);

// Send request
connection.setRequestProperty("x-ms-version", serviceVersion);
connection.setRequestProperty("x-ms-date",date);
connection.setRequestProperty("Authorization", authorization);
connection.connect();

// OutPut response status
System.out.println("Response message : " + connection.getResponseMessage());
System.out.println("Response code : " + connection.getResponseCode());

// OutPut Metadata
connection.getHeaderFields().forEach(new BiConsumer<String, List<String>>() {
@Override
public void accept(String s, List<String> strings) {
if(s!=null && s.startsWith("x-ms-meta")){
System.out.println(s+":"+strings.get(0));
}
}
});
}

private static String getAuthorization(String accountName, String authKeyFormat, String signStr, String accountKey) throws NoSuchAlgorithmException, UnsupportedEncodingException, InvalidKeyException {

SecretKeySpec secretKey = new SecretKeySpec(Base64.getDecoder().decode(accountKey), "HmacSHA256");
Mac sha256HMAC = Mac.getInstance("HmacSHA256");
sha256HMAC.init(secretKey);
String signature = Base64.getEncoder().encodeToString(sha256HMAC.doFinal(signStr.getBytes("UTF-8")));

return authKeyFormat+" "+accountName+":"+signature;
}

关于java - Azure存储服务Rest API : Get Container Metadata,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52056199/

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