Azure File服務(5): Java開發

 

使用Java管理Azure文件共享服務

 

Azure文件共享服務提供了多種方式的訪問接口,包括Powershell,.Net, Java, Python等等,本章主要介紹如何使用Java來訪問Azure File存儲。

 

  1. Java基本開發環境的搭建,Eclipse的插件安裝,IntelliJ IDEA的插件安裝,請參考我的文檔:

http://cloudapps.blog.51cto.com/3136598/1772092

  1. 關於訪問連接串,SDK默認的連接串是指向global Azure的,即"*.core.windows.net",但中國區的Azure的訪問的服務URL是".core.chinacloudapi.cn",所以需要在鏈接字符串中指定EndpointSuffix。

     

  2. 關於存儲的訪問協議,默認情況下是https協議,但你也可以指定爲http協議,一般建議在Azure內部訪問存儲的時候使用http,而在外部訪問的時候使用https進行加密傳輸。

    public static final String storageConnectionString =

             "DefaultEndpointsProtocol=http;" +

             "AccountName=mystorageacctfile;" +

             "AccountKey=YOURStorageAccountKey;" +

             "EndpointSuffix=core.chinacloudapi.cn";

     

    如果需要進行加密傳輸,修改DefaultEndpointsProtocol=https.

     

  3. Fileshare的名字命名是有要求的,例如必須全部小寫等,否則在Java裏面你會看到如下錯誤:

具體命名規則請參考:https://msdn.microsoft.com/library/azure/dn167011.aspx

  1. 首先需要初始化存儲上下文,得到文件訪問句柄:

    storageAccount = CloudStorageAccount.parse(storageConnectionString);

             System.out.println(storageAccount.getBlobEndpoint());

              

             CloudFileClient fileClient = storageAccount.createCloudFileClient();

     

  2. 創建一個新的文件共享:

    CloudFileShare share = fileClient.getShareReference(myFileShare);

            

    if (share.createIfNotExists())

    {

    System.out.println("New file share:" + myFileShare +"created!");

    }

     

  3. 文件共享創建完成後,我們在該文件共享下建立一個目錄:

    //Get a reference to the root directory for the share.

    CloudFileDirectory rootDir = share.getRootDirectoryReference();

            

    //Get a reference to the sampledir directory

    CloudFileDirectory sampleDir = rootDir.getDirectoryReference(mydirectory);

     

    if (sampleDir.createIfNotExists())

    {

    System.out.println("sampledir created");

    }

    else {

    System.out.println("sampledir already exists");

}

  1. 上傳或者下載一個文件共享中的文件,下載文件可以將他通過Outstream寫入到本地文件等多種方式,本示例中直接打印出來:

    //upload a test file to the sampledir

    CloudFile cloudFile = sampleDir.getFileReference("hdinsight.publishsettings");

            

    if(!cloudFile.exists())

    {

        cloudFile.uploadFromFile(testfilePath);

    }

    else

    {

        //Download file if exists

        System.out.println(cloudFile.downloadText());

    }

  2. 以下例子展示瞭如何刪除一個文件,刪除一個目錄,請注意在刪除目錄的時候,該目錄下必須沒有任何文件,否則會報錯:

     

    CloudFile cloudFile = sampleDir.getFileReference(testFilename);

    //Delete specified file

    if ( cloudFile.deleteIfExists() )

{

     System.out.println(testFilename + " was deleted!");

        

}

 

//Get a reference to the root directory for the share.

CloudFileDirectory rootDir = share.getRootDirectoryReference();

          

//Get a reference to the sampledir directory

CloudFileDirectory sampleDir = rootDir.getDirectoryReference(mydirectory);

// Delete the directory

if ( sampleDir.deleteIfExists() )

{

     System.out.println("Directory "sampleDir +" was deleted!");

}

 

10.關於在你調用Azure file接口的時候,使用https鏈接,即將鏈接字符串中的DefaultEndpointsProtocol設置https你可能會碰到如下錯誤:

 

即使你使用的是最新的Azure China WoSign的證書,也會出現上述問題,具體原因和Azure China沒有關係,你懂的:)解決辦法請參考我的博文:

 

http://cloudapps.blog.51cto.com/3136598/1744342

 


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章