【问题标题】:Code sample for Microsoft Azure File Storage using REST API(JAVA)使用 REST API(JAVA) 的 Microsoft Azure 文件存储的代码示例
【发布时间】:2017-09-09 10:46:09
【问题描述】:

我需要使用 JAVA 使用 REST API 获取 Azure 文件存储中文件共享中可用文件的内容。有官方文件可用,但我很困惑,因为没有明确的解释。所以,如果有人能提供一个样本,那对我真的很有帮助。

【问题讨论】:

  • 请分享您到目前为止编写的代码以及您面临的问题。你有什么理由不想使用 JAVA SDK?
  • 感谢您的回复。但是,我已经设法解决了这个问题,并且我在下面的答案中发布了相同的内容。如果需要任何优化,请建议我。
  • 我已经使用 JAVA SDK 做到了,并且也成功了。但根据我的要求,我需要涵盖所有可能的方式。

标签: java rest azure azure-storage azure-storage-files


【解决方案1】:

我在正确生成身份验证字符串时遇到问题,它给出了错误: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;
}}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-03-08
    • 2021-02-18
    • 2017-10-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-09-10
    相关资源
    最近更新 更多