• Azure File SMB3.0文件共享服务(5)


     

    使用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

     

  • 相关阅读:
    打印机无法访问打印机怎么连
    IDEA 2018 LICENSE SERVER
    idea 项目打包发布
    Oracle的关于小数的使用
    js代码实现购物车效果
    通过shell定时备份数据库
    (二)Linux实操之——网络配置、进程管理、服务管理、组管理、YUM
    (一)Linux实操之——权限、任务调度、磁盘分区
    idea搭建简易ssm项目
    idea右键无法新建Java Class
  • 原文地址:https://www.cnblogs.com/cloudapps/p/5486887.html
Copyright © 2020-2023  润新知