I'm using Azure REST API to get Storage Blob Size Details using java, I got response "Server failed to authenticate the request. Make sure the value of Authorization header is formed correctly including the signature"
public static void getContainer() throws Exception {
// Account info
String accountName = "StorageName";
String accountKey = "StorageKey";
// Request Uri and Method
String containerName = "ContainerName";
String requestUri = "https://" + accountName + ".blob.core.windows.net/" + containerName + "?restype=container&comp=metadata";
System.out.println("requestUri = " + requestUri);
HttpURLConnection connection = (HttpURLConnection) (new URL(requestUri)).openConnection();
connection.setRequestMethod("GET");
// Request Headers
// 1. x-ms-version, recommend to use the latest version if possible
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";
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" + caHeader + caResource;
System.out.println("signStr = " + signStr);
String authorization = getAuthorization(accountName, authKeyFormat, signStr, accountKey);
System.out.println("x-ms-version = " + serviceVersion);
System.out.println("x-ms-date = " + date);
System.out.println("Authorization = " + authorization);
connection.setRequestProperty("x-ms-version", serviceVersion);
connection.setRequestProperty("x-ms-date", date);
connection.setRequestProperty("Authorization", authorization);
connection.setDoOutput(true);
connection.setFixedLengthStreamingMode(0);
System.out.println("Response message : " + connection.getResponseMessage());
System.out.println("Response code : " + connection.getResponseCode());
}
private static String getAuthorization(String accountName, String authKeyFormat, String signStr, String accountKey) throws NoSuchAlgorithmException, UnsupportedEncodingException, InvalidKeyException, java.security.InvalidKeyException, Base64DecodingException {
SecretKeySpec secretKey = new SecretKeySpec(Base64.decode(accountKey), "HmacSHA256");
Mac sha256HMAC = Mac.getInstance("HmacSHA256");
sha256HMAC.init(secretKey);
String signature = Base64.encode(sha256HMAC.doFinal(signStr.getBytes("UTF8")));
return authKeyFormat + " " + accountName + ":" + signature;
}
Storage request and response detail
GET - https://StorageName.blob.core.windows.net/ContainerName?restype=container&comp=metadata
x-ms-version = 2018-03-28
x-ms-date = Tue, 18 Jun 2019 13:46:41 GMT
Authorization = SharedKey StorageName:Pp8E/FAxeIHDYs17r2GRYvL8xAgJ/D5eJuqlVW3+aiU=
Response message : Server failed to authenticate the request. Make sure the value of Authorization header is formed correctly including the signature. Response code : 403
we aren't able to authenticate the storage Account, hence we are not able to get the blob content size