使用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. project euler 12 Highly divisible triangular number

    Highly divisible triangular number Problem 12 The sequence of triangle numbers is generated by addin ...

  2. py2exe 生成带图标的单个文件实例

    随便拉了个学习时用的测试程序来做的实例,原程序如下: #Filename:for.py count=0 for i in range(1,100,2): count+=i else: print 't ...

  3. Service、Alarm与BroadcastReceiver的使用方法

    1:定义一个服务类,在服务类中使用AlarmManager 来管理服务的运行 public class WtacService extends Service{ private AlarmManage ...

  4. 24c16读写驱动程序

    //////////////////////////////////////////////////////////////// //24c16读写驱动程序 // //模块调用: //读数据:read ...

  5. UltraEdit的语法高亮显示配置

    今天吴同学看到我电脑中有UltraEdit好奇地问我会不会用,我那个汗啊,不会用我装它干什么啊?其实当时装UltraEdit主要是用来写Java的,没有想到,工作一忙顾及不上学习Java的事情了.于是 ...

  6. Linux系统编程(7)—— 进程之进程概述

    我们知道,每个进程在内核中都有一个进程控制块(PCB)来维护进程相关的信息,Linux内核的进程控制块是task_struct结构体.现在我们全面了解一下其中都有哪些信息. 进程id.系统中每个进程有 ...

  7. 【CF 676B Pyramid of Glasses】模拟,递归

    题目链接:http://codeforces.com/problemset/problem/676/B 题意:一个n层的平面酒杯金字塔,如图,每个杯子的容量相同.现在往最顶部的一个杯子倒 t 杯酒,求 ...

  8. java实现写大量数据到文件中

    生成.txt文件 生成.csv文件 生成.xls文件 import java.io.BufferedWriter; import java.io.File; import java.io.FileOu ...

  9. zabbix安装及配置(rpm包安装mysql,php,apache,zabbix)

    zabbix安装及配置 一.安装mysql.php.apache.zabbix 安装环境: 操作系统:rhel6.3-x86-64  mysql:5.6.23   --官网下载rpm包安装php:5. ...

  10. cacti出现snmp error

    一.在被监控机器中确认snmp是否启动 service snmpd status [root@test2 ~]# service snmpd status snmpd (pid  1247) 正在运行 ...