I need to get the contents of files available in a File Share in Azure File Storage using REST API using JAVA. There are official documents available but I got confused as there is no clear explanation. So, if some one could provide a sample then it will be really helpful for me.
3
-
1Please share the code you have written so far and what issues you're facing. Is there any reason why you don't want to use JAVA SDK?Gaurav Mantri– Gaurav Mantri2017-04-13 10:45:39 +00:00Commented Apr 13, 2017 at 10:45
-
Thanks for your response. However, I have managed to solve the issue and I am posting the same in below answer. If any optimization required then please suggest me.ashishakp– ashishakp2017-04-13 12:08:47 +00:00Commented Apr 13, 2017 at 12:08
-
I have done that using JAVA SDK and succeeded also. But as per my requirements I need to cover all the possible ways.ashishakp– ashishakp2017-04-13 12:20:04 +00:00Commented Apr 13, 2017 at 12:20
Add a comment
|
1 Answer
I was getting problem in generating the Authentication String properly, and it was giving Error : 403, Message : Forbidden. But Using the below code I successfully managed to do that.
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;
}}