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

 

Azure File SMB3.0文件共享服务(5)的更多相关文章

  1. Azure File SMB3.0文件共享服务(3)

    在Windows上使用Azure文件共享服务 之前简单介绍过,你可以通过SMB 3.0协议,将Azure文件共享挂载在本地,就如使用一个网络驱动器是一样的,但需要注意不同版本的Windows对于SMB ...

  2. Azure File SMB3.0文件共享服务(4)

    在Linux上使用Azure文件共享服务 使用SMB 3.0从用户自己的数据连接到Azure,需要加密连接,但目前的Linux SMB客户端都暂时都不支持,Linux的开源社区正在努力将该功能添加到L ...

  3. Azure File SMB3.0文件共享服务(1)

    Azure Storage File是Azure推出的文件共享服务,目前的版本同时支持SMB 2.1和SMB 3.0协议.文件共享服务非常适合那些希望把自己数据中心中使用文件共享的应用程序,在云端需要 ...

  4. Azure File SMB3.0文件共享服务(2)

    使用Powershell创建文件共享 Azure的文件存储结构如下所示,最基本的文件存储包含存储账号,文件共享,在文件共享下面你可以建立文件目录,上传文件: 在开始使用Powershell创建文件共享 ...

  5. Windows Azure文件共享服务--File Service

    部署在Windows Azure上的虚拟机之间如何共享文件?例如:Web Server A和Web Server B组成负载均衡集群,两个服务器需要一个共享目录来存储用户上传的文件.通常,大家可能首先 ...

  6. Microsoft Azure File 服务简介

    我们非常高兴地宣布在微软Azure中国区推出 Microsoft Azure File 服务预览版.Azure File 服务使用标准 SMB 2.1 协议提供文件共享.Azure 中运行的应用程序现 ...

  7. Azure File文件共享(6):使用Python开发

    Azure文件共享服务提供了多种方式的访问接口,包括Powershell,.Net, Java, Python等等,本章主要介绍如何使用Python来访问Azure File存储. 关于Python环 ...

  8. Azure AD Domain Service(二)为域服务中的机器配置 Azure File Share 磁盘共享

    一,引言 Azure File Share 是支持两种认证方式的! 1)Active Directory 2)Storage account key 记得上次分析的 "Azure File ...

  9. Windows Azure Storage (20) 使用Azure File实现共享文件夹

    <Windows Azure Platform 系列文章目录> Update 2016-4-14.在Azure VM配置FTP和IIS,请参考: http://blogs.iis.net/ ...

随机推荐

  1. query通用开源框架

    Jquery通用开源框架之[ejq.js] 简介 ejq是一款非常小巧的JS工具库,未压缩才50K,在jquery的基础上对jquery缺失部分作了很好的弥补作用. 优点: 1.具有内置的模板解析引擎 ...

  2. 苹果开发证书相关BLOG与Delphi IOS环境安装(超详细)

    注:有好的资源,请添加了上传,上传后,通知管理员,删除旧文件,累积相关的学习资源,方便新手学习 一.相关论坛http://www.2ccc.com/ delphi 合子 www.2pascal.com ...

  3. C# 动态Linq(结合反射)

      这篇文章决定对最近一个单机版Web程序用到的东西总结一下. 一.反射Linq之OrderBy 动态Linq结合反射对某字段排序: namespace 动态Linq { class Program ...

  4. 函数fold 或reduce用法

    http://yi-programmer.com/2011-02-24_fold.html http://c2.com/cgi/wiki?FoldFunction http://rwh.readthe ...

  5. python模块学习之random

    模块源码: Source code: Lib/random.py 文档:http://docs.python.org/2/library/random.html 常用方法: random.random ...

  6. Bash Shell 快捷键的学习使用

    原文地址: http://dbanotes.net/tech-memo/shell_shortcut.html 这篇 Bash Shell Shortcuts 的快捷键总结的非常好.值得学习.下面内容 ...

  7. HTML5 canvas translate() 方法

    HTML5 canvas translate() 方法 translate() 方法重新映射画布上的 (0,0) 位置.

  8. [C# 基础知识系列]专题九: 深入理解泛型可变性

    引言: 在C# 2.0中泛型并不支持可变性的(可变性指的就是协变性和逆变性),我们知道在面向对象的继承中就具有可变性,当方法声明返回类型为Stream,我们可以在实现中返回一个FileStream的类 ...

  9. Davinci开发板DM368 nandwrite.c简要分析

    #include <stdio.h> #include <stdlib.h> #include <sys/stat.h> #include <limits.h ...

  10. php array相关函数个人小结

    1.array_chunk() 把一个数组分割为新的数组块. 其中每个数组的单元数目由 size 参数决定.最后一个数组的单元数目可能会少几个. 例子   <?php $a=array(&quo ...