2017-04-13 9 views
0

JAVA를 사용하는 REST API를 사용하여 Azure 파일 저장소의 파일 공유에서 사용할 수있는 파일의 내용을 가져와야합니다. 사용할 수있는 공식 문서가 있지만 명확한 설명이 없으므로 혼란 스럽습니다. 그래서, 어떤 사람이 표본을 제공 할 수 있다면 그것은 저에게 정말로 도움이 될 것입니다.REST API (Java)를 사용하여 Microsoft Azure 파일 저장소 코드 샘플

+0

지금까지 작성한 코드와 직면 한 문제점을 공유하십시오. JAVA SDK를 사용하고 싶지 않은 이유가 있습니까? –

+0

답변 해 주셔서 감사합니다. 그러나, 나는 그 문제를 해결할 수 있었고 나는 아래의 답변에 같은 것을 올리고있다. 최적화가 필요한 경우 나에게 제안하십시오. – ashishakp

+0

JAVA SDK를 사용하여이 작업을 수행했으며 성공했습니다. 그러나 내 요구 사항에 따라 가능한 모든 방법을 다룰 필요가 있습니다. – ashishakp

답변

3

인증 문자열 생성에 문제가있어 오류 : 403, 메시지 : 금지되었습니다. 그러나 아래 코드를 사용하여 성공적으로 관리했습니다.

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; 
}}