我正在尝试使用Java通过REST API访问Azure Data Lake Storage Gen 2中的文件系统。这就是我建立请求的方式:
public static void main(String[] args) throws Exception {
String urlString = "https://" + account + ".dfs.core.windows.net/sterisfiles?resource=filesystem";
HttpURLConnection connection = (HttpURLConnection)(new URL(urlString)).openConnection();
getFileRequest(connection, account, key);
connection.connect();
System.out.println("Response message : "+connection.getResponseMessage());
}
public static void getFileRequest(HttpURLConnection request, String account, String key) throws Exception{
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 stringToSign = "GET\n"
+ "\n" // content encoding
+ "\n" // content language
+ "\n" // content length
+ "\n" // content md5
+ "\n" // content type
+ "\n" // date
+ "\n" // if modified since
+ "\n" // if match
+ "\n" // if none match
+ "\n" // if unmodified since
+ "\n" // range
+ "x-ms-date:" + date + "\n"
+ "x-ms-version:2014-02-14\n" //headers
+ "/"+account + request.getURL().getPath();
String auth = getAuthenticationString(stringToSign);
request.setRequestMethod("GET");
request.setRequestProperty("x-ms-date", date);
request.setRequestProperty("x-ms-version", "2014-02-14");
request.setRequestProperty("Authorization", auth);
}
private static String getAuthenticationString(String stringToSign) throws Exception{
Base64 base64 = new Base64();
Mac mac = Mac.getInstance("HmacSHA256");
mac.init(new SecretKeySpec(base64.decode(key), "HmacSHA256"));
String authKey = new String(base64.encode(mac.doFinal(stringToSign.getBytes("UTF-8"))));
String auth = "SharedKey " + account + ":" + authKey;
return auth;
}
这将引发 403 错误,并显示以下消息: 服务器无法验证请求。确保正确构成Authorization标头的值(包括签名)。
我的请求标头不正确吗?
答案 0 :(得分:1)
根据我的测试,我们可以使用Azure AD身份验证来调用Azure数据湖存储Gen2 REST API。有关更多详细信息,请参阅https://social.msdn.microsoft.com/Forums/en-US/45be0931-379d-4252-9d20-164261cc64c5/error-while-calling-adls-gen-2-rest-api-to-create-file?forum=AzureDataLake。
unsafe
az ad sp create-for-rbac -n 'your sp name' --role 'Storage Blob Data Owner' --scope 'your scope such as your storage account scope'
调用rest api 一种。创建文件系统
Method : POST
URL: https://login.microsoftonline.com/<your Azure AD tenant domain>/oauth2/token
Body:
grant_type =client_credentials
client_id=<the appid you copy>
client_secret=<the password you copy>
resource=https://storage.azure.com
b。列表文件系统
PUT https://{accountName}.{dnsSuffix}/{filesystem}?resource=filesystem