Azure File SMB3.0文件共享服务(5)
使用Java管理Azure文件共享服务
Azure文件共享服务提供了多种方式的访问接口,包括Powershell,.Net, Java, Python等等,本章主要介绍如何使用Java来访问Azure File存储。
- Java基本开发环境的搭建,Eclipse的插件安装,IntelliJ IDEA的插件安装,请参考我的文档:
http://cloudapps.blog.51cto.com/3136598/1772092
- 关于访问连接串,SDK默认的连接串是指向global Azure的,即"*.core.windows.net",但中国区的Azure的访问的服务URL是".core.chinacloudapi.cn",所以需要在链接字符串中指定EndpointSuffix。
- 关于存储的访问协议,默认情况下是https协议,但你也可以指定为http协议,一般建议在Azure内部访问存储的时候使用http,而在外部访问的时候使用https进行加密传输。
public
static
final String storageConnectionString ="DefaultEndpointsProtocol=http;" +
"AccountName=mystorageacctfile;" +
"AccountKey=YOURStorageAccountKey;" +
"EndpointSuffix=core.chinacloudapi.cn";
如果需要进行加密传输,修改DefaultEndpointsProtocol=https.
- Fileshare的名字命名是有要求的,例如必须全部小写等,否则在Java里面你会看到如下错误:

具体命名规则请参考:https://msdn.microsoft.com/library/azure/dn167011.aspx
- 首先需要初始化存储上下文,得到文件访问句柄:
storageAccount = CloudStorageAccount.parse(storageConnectionString);
System.out.println(storageAccount.getBlobEndpoint());
CloudFileClient fileClient = storageAccount.createCloudFileClient();
- 创建一个新的文件共享:
CloudFileShare share = fileClient.getShareReference(myFileShare);
if (share.createIfNotExists())
{
System.out.println("New file share:" + myFileShare +"created!");
}
- 文件共享创建完成后,我们在该文件共享下建立一个目录:
//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");
}
- 上传或者下载一个文件共享中的文件,下载文件可以将他通过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());
}
- 以下例子展示了如何删除一个文件,删除一个目录,请注意在删除目录的时候,该目录下必须没有任何文件,否则会报错:
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)的更多相关文章
- Azure File SMB3.0文件共享服务(3)
在Windows上使用Azure文件共享服务 之前简单介绍过,你可以通过SMB 3.0协议,将Azure文件共享挂载在本地,就如使用一个网络驱动器是一样的,但需要注意不同版本的Windows对于SMB ...
- Azure File SMB3.0文件共享服务(4)
在Linux上使用Azure文件共享服务 使用SMB 3.0从用户自己的数据连接到Azure,需要加密连接,但目前的Linux SMB客户端都暂时都不支持,Linux的开源社区正在努力将该功能添加到L ...
- Azure File SMB3.0文件共享服务(1)
Azure Storage File是Azure推出的文件共享服务,目前的版本同时支持SMB 2.1和SMB 3.0协议.文件共享服务非常适合那些希望把自己数据中心中使用文件共享的应用程序,在云端需要 ...
- Azure File SMB3.0文件共享服务(2)
使用Powershell创建文件共享 Azure的文件存储结构如下所示,最基本的文件存储包含存储账号,文件共享,在文件共享下面你可以建立文件目录,上传文件: 在开始使用Powershell创建文件共享 ...
- Windows Azure文件共享服务--File Service
部署在Windows Azure上的虚拟机之间如何共享文件?例如:Web Server A和Web Server B组成负载均衡集群,两个服务器需要一个共享目录来存储用户上传的文件.通常,大家可能首先 ...
- Microsoft Azure File 服务简介
我们非常高兴地宣布在微软Azure中国区推出 Microsoft Azure File 服务预览版.Azure File 服务使用标准 SMB 2.1 协议提供文件共享.Azure 中运行的应用程序现 ...
- Azure File文件共享(6):使用Python开发
Azure文件共享服务提供了多种方式的访问接口,包括Powershell,.Net, Java, Python等等,本章主要介绍如何使用Python来访问Azure File存储. 关于Python环 ...
- Azure AD Domain Service(二)为域服务中的机器配置 Azure File Share 磁盘共享
一,引言 Azure File Share 是支持两种认证方式的! 1)Active Directory 2)Storage account key 记得上次分析的 "Azure File ...
- Windows Azure Storage (20) 使用Azure File实现共享文件夹
<Windows Azure Platform 系列文章目录> Update 2016-4-14.在Azure VM配置FTP和IIS,请参考: http://blogs.iis.net/ ...
随机推荐
- Knight Moves(BFS,走’日‘字)
Knight Moves Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Tota ...
- HDU 1172 猜数字(DFS)
猜数字 Time Limit:10000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u Submit Status ...
- 利用Azure Automation实现云端自动化运维(1)
Azure Automation是Azure上的一个自动化工作流引擎,基于Powershell,来帮助用户简化,集成和自动化Azure上的运维工作,例如: 实现定时开关虚拟机,节约成本 实现定时创建删 ...
- JavaScript事件处理
客户端javascript程序采用了异步事件驱动程序,在这种程序设计风格下,当文档,浏览器,元素,或与之相关的对象发生某些有趣的事件时,web浏览器就会产生事件.事件本身不是javascript对象. ...
- centos 6.5 安装 redis
下载软件: wget wget http://download.redis.io/releases/redis-2.8.7.tar.gz 2.解压软件并编译安装: tar -zxvf redis-2. ...
- 汉子英文同行 连续英文不折行断行 的问题 兼容FIREFOX浏览器CSS
#intro {white-space: normal;word-break: break-all;overflow: hidden;} --------------------- 案例2
- UESTC_The Most Wonderful Competition CDOJ 56
The Most Wonderful Competition Time Limit: 3000/1000MS (Java/Others) Memory Limit: 65535/65535KB ...
- HDU1232 畅通工程 (并查集模板题)
畅通工程 Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submis ...
- ※数据结构※→☆非线性结构(tree)☆============二叉树 顺序存储结构(tree binary sequence)(十九)
二叉树 在计算机科学中,二叉树是每个结点最多有两个子树的有序树.通常子树的根被称作“左子树”(left subtree)和“右子树”(right subtree).二叉树常被用作二叉查找树和二叉堆或是 ...
- JNI的替代者—使用JNA访问Java外部功能接口
摘自:http://www.cnblogs.com/lanxuezaipiao/p/3635556.html JNI的替代者-使用JNA访问Java外部功能接口 1. JNA简单介绍 先说JNI(Ja ...