0
我需要使用JAVA使用REST API獲取Azure文件存儲中文件共享中可用文件的內容。有官方文件可用,但我感到困惑,因爲沒有明確的解釋。所以,如果有人能夠提供樣品,那麼它對我來說真的很有幫助。使用REST API的Microsoft Azure文件存儲的代碼示例(JAVA)
我需要使用JAVA使用REST API獲取Azure文件存儲中文件共享中可用文件的內容。有官方文件可用,但我感到困惑,因爲沒有明確的解釋。所以,如果有人能夠提供樣品,那麼它對我來說真的很有幫助。使用REST API的Microsoft Azure文件存儲的代碼示例(JAVA)
我在正確生成驗證字符串時遇到了問題,它給出了Error:403,Message:Forbidden。但使用下面的代碼,我成功地設法做到了這一點。
public class FileStorageServiceWithRest {
private static final String account = "<your_account_name>";
private static final String key = "<your_access_key>";
public static void main(String args[]) throws Exception{
String urlString = "http://" + account + ".file.core.windows.net/myshare/<your_file_name>";
HttpURLConnection connection = (HttpURLConnection)(new URL(urlString)).openConnection();
getFileRequest(connection, account, key);
connection.connect();
System.out.println("Response message : "+connection.getResponseMessage());
System.out.println("Response code : "+connection.getResponseCode());
BufferedReader br = null;
if(connection.getResponseCode() != 200){
br = new BufferedReader(new InputStreamReader((connection.getErrorStream())));
}else{
br = new BufferedReader(new InputStreamReader((connection.getInputStream())));
}
System.out.println("Response body : "+br.readLine());
}
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 + "\nx-ms-version:2014-02-14\n" //headers
+ "/"+account + request.getURL().getPath(); // resources
System.out.println("stringToSign : "+stringToSign);
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{
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;
}}
請分享您迄今爲止編寫的代碼以及您面臨的問題。你有什麼理由不使用JAVA SDK? –
感謝您的回覆。但是,我已經設法解決了這個問題,我在下面的答案中發佈了相同的內容。如果需要優化,請給我建議。 – ashishakp
我已經完成了使用JAVA SDK併成功了。但根據我的要求,我需要涵蓋所有可能的方式。 – ashishakp