Azure Storage 是微软 Azure 云提供的云端存储解决方案,当前支持的存储类型有 Blob、Queue、File 和 Table。

笔者在《Azure Blob Storage 基本用法》中介绍了 Blob Storage 的基本用法,本文将介绍 File Storage 的主要使用方法。

File Storage 是什么?

Azure File Storage 是一个通过 Server Message Block (SMB) 协议提供云端文件共享的服务。通过 File Storage 共享的文件,能够被加载为云端或本地主机的磁盘,应用程序可以通过文件 API 像访问本地文件一样访问这些文件。

下面是 File Storage 典型的应用场景:

  1. 轻松迁移那些有磁盘读写操作的应用到云端。不用修改程序,只要通过 File Storage 加载相应的文件即可。
  2. 存放共享的应用程序配置文件。
  3. 存放日志等应用程序诊断数据。
  4. 存放管理员的常用工具。

Azure File Storage的结构

下图描述了 File Storage 的基本组织结构:

  • Azure Storage Account:

Storage Account 是用来管理 Azure Storage 的一个命名空间,主要用来控制存储数据的访问权限和计费。对 Blob、Queue、File 和 Table 这些 Azure 提供的存储服务的访问控制,都是通过 Storage Account 来进行的,所以要想使用 File Storage,需要先创建你的 Storage Account。

  • Share:

Share 是管理共享文件的单位,任何要共享的文件和目录都必须属于某个 Share。一个 Storage Account 下的 Share 数量是不受限制的,每个 Share 中可以存放任何数量的文件。但是每个 Share 中最多能存放5TB 的数据。

  • Directory:

与 Blob Storage 不同,File Storage 支持真正的文件目录。你可以根据需要来创建目录。

  • File:

File 是真正被共享的文件,每个文件最大 1TB。

  • URL format:

与 Blob Storage 相似,File Storage 中的每个文件都可以通过 URL 来访问。URL 的详细格式为:

https://<storage account>.file.core.windows.net/<share>/<directory/directories>/<filename>

下面是个更真实的例子:

https://nickdemo.file.core.windows.net/demofiles/temp.txt

如果您还不熟悉 Azure Storage Account 的使用,以及如何通过 WindowsAzure.Storage 库访问 Azure Storage,请参考前文《Azure Table storage 基本用法》中的介绍,这里就不重复了。

为了方便查看 C# 代码执行的结果,本文使用了 MS 发布的一个 Azure Storage 客户端工具:Microsoft Azure Storage Explorer,文中简称为 Storage Explorer。下面是 File Storage 的一个截图:

接下来我们通过 C# 代码来介绍如何操作 File Storage。

创建 File Share

第一步我们先创建名为“mylogs”的 Share:

//CloudStorageAccount 类表示一个 Azure Storage Account,我们需要先创建它的实例,才能访问属于它的资源。
//注意连接字符串中的xxx和yyy,分别对应Access keys中的Storage account name 和 key。
CloudStorageAccount storageAccount = CloudStorageAccount.Parse("DefaultEndpointsProtocol=https;AccountName=xxx;AccountKey=yyy");
//CloudFileClient 类是 Windows Azure File Service 客户端的逻辑表示,我们需要使用它来配置和执行对 File Storage 的操作。
CloudFileClient fileClient = storageAccount.CreateCloudFileClient();
//CloudFileShare 表示一个 File Share 对象。
CloudFileShare share = fileClient.GetShareReference(shareName);
//如果不存在就创建 File Share。
share.CreateIfNotExists();

运行上面的代码,然后打开 Storage Explorer,看到名为 ”mylogs” 的 Share 已经创建了:

上传文件

File Storage 支持真正的文件目录。所以在上传文件前需要确定要把文件上传到哪个目录下。每一个 File Share 都有一个根目录,我们可以先取到这个根目录,然后再创建子目录或是直接上传文件。下面的代码会在根目录下创建一个叫 “web”的子目录,然后上传文件 web.log 到 web 目录中:

//获得根目录的引用。
CloudFileDirectory rootDir = share.GetRootDirectoryReference();
//创建子目录 "web" 的引用。
CloudFileDirectory webDir = rootDir.GetDirectoryReference("web");
//创建子目录 "web"。
webDir.CreateIfNotExists();
//创建文件 "web.log" 的引用。
CloudFile cloudFile = webDir.GetFileReference("web.log");
string localFile = @"F:\temp\web.log";
using (var fileStream = System.IO.File.OpenRead(localFile))
{
//上传文件。
cloudFile.UploadFromStream(fileStream);
}

在Storage Explorer 中检查下结果:

复制文件

Azure Storage 支持在 Blob Storage 和 File Storage 之间相互复制文件,但这样的操作涉及的访问权限管理相对复杂一些。本文仅介绍文件在同一个 File Storage 中的复制操作。下面的代码复制 web.log 文件并创建 web.copy.log 文件:

CloudFileShare share = GetFileShare(_currentShareName);
CloudFileDirectory rootDir = share.GetRootDirectoryReference();
CloudFileDirectory webDir = rootDir.GetDirectoryReference("web");
CloudFile cloudFile = webDir.GetFileReference("web.log");
if (cloudFile.Exists())
{
//由 web.log 文件创建 web.copy.log 文件。
CloudFile copyFile = webDir.GetFileReference("web.copy.log");
copyFile.StartCopy(cloudFile);
}

查看复制操作的结果:

设置 Share 的最大容量

前面我们提到每个 Share 中最多能存放5TB 的数据。但有时可能需要限制一下它的最大值,比如最多只能存放1TB 的数据:

//指定最大容量为 1024,单位是GB。
share.Properties.Quota = ;
share.SetProperties();

代码很简单,如果想要查看 Share 的最大容量是多少,直接取share.Properties.Quota 属性的值就可以了。

现在我们在云端有一个 1TB 大小的共享目录,如何使用呢?

把 Share 映射为本地机器的网络硬盘

用管理员权限启动 cmd.exe,执行下面的命令:

cmdkey /add:<storage-account-name>.file.core.windows.net /user:<storage-account-name> /pass:<storage-account-key>
net use z: \\<storage-account-name>.file.core.windows.net\mylogs

注意,请把上面命令中的 < storage-account-name > 和 < storage-account-key >进行替换。

再用非管理员权限启动 cmd.exe,再执行一次net use 命令:

net use z: \\<storage-account-name>.file.core.windows.net\mylogs

如果不第二次执行 net use 命令,资源管理器中是看不到驱动器盘符的:

注意:一定要在防火墙 Outbound 规则中放行 SMB 协议使用的 TCP 445端口。

这就搞定了!看起来还不错吧?

总结

虽然我们看到File Storage 和前文中介绍的Blob Storage 存在着很多相似的地方,但本质上它们是不一样的。Blob Storage 本质上是一个个网络上的文件,而 File Storage 则是通过 SMB 协议实现的网络共享文件,能够被操作系统映射成本地的磁盘是其最大特征。也只有这一点才能让应用程序通过文件操作API,完成对远程文件的访问。

相关阅读:

最全的Windows Azure学习教程汇总

Azure Blob Storage 基本用法 -- Azure Storage 之 Blob

Azure Queue Storage 基本用法 -- Azure Storage 之 Queue

Azure Table storage 基本用法 -- Azure Storage 之 Table

Azure File Storage 基本用法 -- Azure Storage 之 File的更多相关文章

  1. Azure Queue Storage 基本用法 -- Azure Storage 之 Queue

    Azure Storage 是微软 Azure 云提供的云端存储解决方案,当前支持的存储类型有 Blob.Queue.File 和 Table. 笔者在<Azure File Storage 基 ...

  2. Azure Blob Storage 基本用法 -- Azure Storage 之 Blob

    Azure Storage 是微软 Azure 云提供的云端存储解决方案,当前支持的存储类型有 Blob.Queue.File 和 Table. 笔者在<Azure Table storage ...

  3. Azure Table storage 基本用法 -- Azure Storage 之 Table

    Azure Storage 是微软 Azure 云提供的云端存储解决方案,当前支持的存储类型有 Blob.Queue.File 和 Table,其中的 Table 就是本文的主角 Azure Tabl ...

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

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

  5. [Windows Azure] How to use the Queue Storage Service

    How to use the Queue Storage Service version 1.7 version 2.0 This guide will show you how to perform ...

  6. [Windows Azure] How to use the Table Storage Service

    How to use the Table Storage Service version 1.7 version 2.0 This guide will show you how to perform ...

  7. 【Azure 存储服务】Java Azure Storage SDK V12使用Endpoint连接Blob Service遇见 The Azure Storage endpoint url is malformed

    问题描述 使用Azure Storage Account的共享访问签名(Share Access Signature) 生成的终结点,连接时遇见  The Azure Storage endpoint ...

  8. Windows Azure Storage (6) Windows Azure Storage之Table

    <Windows Azure Platform 系列文章目录> 最近想了想,还是有必要把Windows Azure Table Storage 给说清楚. 1.概念 Windows Azu ...

  9. [New Portal]Windows Azure Storage (14) 使用Azure Blob的PutBlock方法,实现文件的分块、离线上传

    <Windows Azure Platform 系列文章目录> 相关内容 Windows Azure Platform (二十二) Windows Azure Storage Servic ...

随机推荐

  1. 如何一步一步用DDD设计一个电商网站(十)—— 一个完整的购物车

     阅读目录 前言 回顾 梳理 实现 结语 一.前言 之前的文章中已经涉及到了购买商品加入购物车,购物车内购物项的金额计算等功能.本篇准备把剩下的购物车的基本概念一次处理完. 二.回顾 在动手之前我对之 ...

  2. UWP学习目录整理

    UWP学习目录整理 0x00 可以忽略的废话 10月6号靠着半听半猜和文字直播的补充看完了微软的秋季新品发布会,信仰充值成功,对UWP的开发十分感兴趣,打算后面找时间学习一下.谁想到学习的欲望越来越强 ...

  3. Android Ormlite 学习笔记2 -- 主外键关系

    以上一篇为例子,进行主外键的查询 定义Users.java 和 Role.java Users -- Role 关系为:1对1 即父表关系 Role -- Users 关系为:1对多 即子表关系 下面 ...

  4. 关于python的bottle框架跨域请求报错问题的处理

    在用python的bottle框架开发时,前端使用ajax跨域访问时,js代码老是进入不了success,而是进入了error,而返回的状态却是200.url直接在浏览器访问也是正常的,浏览器按F12 ...

  5. 【踩坑速记】开源日历控件,顺便全面解析开源库打包发布到Bintray/Jcenter全过程(新),让开源更简单~

    一.写在前面 自使用android studio开始,就被它独特的依赖方式:compile 'com.android.support:appcompat-v7:25.0.1'所深深吸引,自从有了它,麻 ...

  6. javascript动画系列第二篇——磁性吸附

    × 目录 [1]范围限定 [2]拖拽范围 [3]磁性吸附 前面的话 上一篇,我们介绍了元素拖拽的实现.但在实际应用中,常常需要为拖拽的元素限定范围.而通过限定范围,再增加一些辅助的措施,就可以实现磁性 ...

  7. 【手记】注意BinaryWriter写string的小坑——会在string前加上长度前缀length-prefixed

    之前以为BinaryWriter写string会严格按构造时指定的编码(不指定则是无BOM的UTF8)写入string的二进制,如下面的代码: //将字符串"a"写入流,再拿到流的 ...

  8. Maven 代理设置

    在maven的安装目录下 %MAVEN_HOME%/conf/setting.xml 中进行设置 <proxies>    <!-- proxy     | Specificatio ...

  9. 使用github远程仓库

    经过几天对github的研究,终于把自己想完成的给解决了,发现google真的有很多解释,但是很多也会出现一些bug,对于初学者真的很多烦恼,所以整理一份,能给初识github的你有所帮助 一,首先, ...

  10. 【干货分享】流程DEMO-人员调动流程

    流程名: 调动 流程相关文件: 流程包.xml 流程说明: 直接导入流程包文件,即可使用本流程 表单:  流程:  图片:3.png DEMO包下载: http://files.cnblogs.com ...