Azure 基础:File Storage

前端之家收集整理的这篇文章主要介绍了Azure 基础:File Storage前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

解决方案,当前支持的存储类型有 Blob、Queue、File 和 Table。

中介绍了 Blob Storage 的基本用法,本文将介绍 File Storage 的主要使用方法。

https://.file.core.windows.net///

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

中的介绍,这里就不重复了。

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

代码来介绍如何操作 File Storage。

CloudStorageAccount storageAccount = CloudStorageAccount.Parse(<span style="color: #008000">// <span style="color: #008000">CloudFileClient 类是 Windows Azure File Service 客户端的逻辑表示,我们需要使用它来配置和执行对 File Storage 的操作。
CloudFileClient fileClient =<span style="color: #000000"> storageAccount.CreateCloudFileClient();
<span style="color: #008000">//
<span style="color: #008000">CloudFileShare 表示一个 File Share 对象。

CloudFileShare share =<span style="color: #000000"> fileClient.GetShareReference(shareName);
<span style="color: #008000">//
<span style="color: #008000">如果不存在就创建 File Share。
share.CreateIfNotExists();

代码,然后打开 Storage Explorer,看到名为 "mylogs" 的 Share 已经创建了:

上传文件

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

CloudFileDirectory rootDir = CloudFileDirectory webDir = rootDir.GetDirectoryReference( 文件 "web.log" 的引用。 CloudFile cloudFile = webDir.GetFileReference( localFile = <span style="color: #0000ff">using (<span style="color: #0000ff">var fileStream =<span style="color: #000000"> System.IO.File.OpenRead(localFile))
{
<span style="color: #008000">//
<span style="color: #008000">上传文件

<span style="color: #000000"> cloudFile.UploadFromStream(fileStream);
}

文件

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

CloudFileShare share === rootDir.GetDirectoryReference(CloudFile cloudFile = webDir.GetFileReference(<span style="color: #800000">"<span style="color: #800000">web.log<span style="color: #800000">"<span style="color: #000000">);
<span style="color: #0000ff">if
<span style="color: #000000"> (cloudFile.Exists())
{
<span style="color: #008000">//<span style="color: #008000">由 web.log 文件创建 web.copy.log 文件
CloudFile copyFile = webDir.GetFileReference(<span style="color: #800000">"<span style="color: #800000">web.copy.log<span style="color: #800000">"<span style="color: #000000">);
copyFile.StartCopy(cloudFile);
}

share.Properties.Quota =

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

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

cmdkey /add:.file.core.windows.net /user: /pass: net use z: \\.file.core.windows.net\mylogs

和 < storage-account-key >进行替换。管理员权限启动 cmd.exe,再执行一次net use 命令:

net use z: \\.file.core.windows.net\mylogs

文件,而 File Storage 则是通过 SMB 协议实现的网络共享文件,能够被操作系统映射成本地的磁盘是其最大特征。也只有这一点才能让应用程序通过文件操作 API 完成对远程文件的访问。

猜你在找的Azure相关文章