使用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. Java中操作时间比较好用的类

    项目中经常用到日期的操作,包括日期的格式化.下面是几个比较常用的工具类. import java.text.SimpleDateFormat; import java.util.Date; impor ...

  2. Debain 7.2安装配置

    一 下载安装Debian 7.2 安装debian CD1,在最后一步,使用网络安装基本界面. 二 修改源 cd /etc/apt mv sources.list sources.list.bak g ...

  3. YY语音从4.0版本开始是基于Qt的开发过程,以及碰到的问题

    作者:姚冬链接:http://www.zhihu.com/question/21359230/answer/20127715来源:知乎著作权归作者所有,转载请联系作者获得授权. YY语音从4.0版本开 ...

  4. 懒猫们终究要付出代码(本领是一生的),鲸鱼们的短视(逐小利而暴死)——这么说我应该只去互联网公司:IM,云存储,邮箱(别的一概不考虑)

    摘自周鸿伟的书,好像:

  5. 查看mysql 服务器上的连接进程

    SHOW PROCESSLIST #展示全部的连接进程 select * from information_schema.processlist where  db='qwuliu' #可以进行筛选查 ...

  6. Linux和windows动态库

    转载:http://www.cnblogs.com/chio/archive/2008/11/13/1333119.html 态链接库技术实现和设计程序常用的技术,在Windows和Linux系 统中 ...

  7. ./configure : /bin/sh^M : bad interpreter

    用命令行来编译Qt的时候发生标题尚的错误. 原因是文件中带有DOS行结束符,必须把它转换成UNix结束符 references: http://stackoverflow.com/questions/ ...

  8. jQuery多版本的使用,同一文件多个版本引用

    <html> <meta http-equiv="Content-Type" content="text/html; charset=utf-8&quo ...

  9. 【DP问题集】动态规划试题

    1.背包问题 给定n种物品和一背包.物品i的重量是wi,其价值为pi,背包的容量为C.问应如何选择装入背包的物品,使得装入背包中物品的总价值最大? 分析: ①每个物品只有两种选择,要么就是塞到包里面, ...

  10. Machine Learning - Lecture 16

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