使用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. python运维开发(二十一)----文件上传和验证码+session

    内容目录: 文件上传 验证码+session 文件和图片的上传功能 HTML Form表单提交,实例展示 views 代码 HTML ajax提交 原生ajax提交,XMLHttpRequest方式上 ...

  2. django框架介绍

    主要内容 1.        Django框架发展 2.        Django架构,MTV模式 3.        开发流程 4.        开发实例——Poll python下各种框架 一 ...

  3. 使用Calendar增加日期

    /** * @Description: 当前日期加上n个月返回long date */ public static long getLongDateAddMonth(int n){ Calendar ...

  4. hdu 1208 Pascal's Travels

    http://acm.hdu.edu.cn/showproblem.php?pid=1208 #include <cstdio> #include <cstring> #inc ...

  5. poj 3318 Matrix Multiplication

    http://poj.org/problem?id=3318 矩阵A*矩阵B是否等于矩阵C #include <cstdio> #include <cstring> #incl ...

  6. DropBox与Box的区别,包括直接的投资人的评价(本地Sync可能还是挺重要的)

    作者:曲凯链接:http://www.zhihu.com/question/22207220/answer/20642357来源:知乎著作权归作者所有,转载请联系作者获得授权. Box和Dropbox ...

  7. perl 爬取csdn

    <pre name="code" class="python">use LWP::UserAgent; use POSIX; use HTML::T ...

  8. HDU1506(单调栈或者DP) 分类: 数据结构 2015-07-07 23:23 2人阅读 评论(0) 收藏

    Largest Rectangle in a Histogram Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 ...

  9. Machine Learning - Lecture 16

    Reinforcement Learning (R.L.) ① MDPs (Markov Decision Processes) ② Value Functions ③ Value Iteration ...

  10. UIView 弹出动画

    // 展开动画 - (void)beginAnimations { CGContextRef context = UIGraphicsGetCurrentContext(); [UIView begi ...