Azure Storage 系列(七)使用Azure File Storage

一,引言

  今天我们开始介绍 Storage 中的最后一个类型的存储—– File Storage(文件存储),Azure File Storage 在云端提供完全托管的文件共享,这些共享项可通过行业标准的服务器消息块 (SMB) 协议进行访问。 Azure 文件共享可由云或者 Windows、Linux 和 macOS 的本地部署同时装载。 此外,可以使用 Azure 文件同步将 Azure 文件共享缓存在 Windows Server 上,以加快访问速度(与在数据使用位置进行访问的速度相当)。

  (一) 就有人问,既然又是也是作为文件存储,项目系统中生产的一些日志,或者上传的图片可以指定虚拟目录用来保存,或者使用 Blob Storage,使用 File Storage的好处是什么?

  答:1,取代或补充本地文件服务器可以使用 Azure 文件来完全取代或补充传统的本地文件服务器或 NAS 设备。 流行的操作系统可在世界各地直接装载 Azure 文件共享。 此外,可以使用 Azure 文件同步将 Azure 文件共享复制到本地或云中的 Windows Server,以便在使用位置对数据进行高性能的分布式缓存。 使用最新版本的 Azure 文件存储 AD 身份验证,Azure 文件共享可继续使用本地托管的 AD 进行访问控制。

2,“直接迁移”应用程序借助 Azure 文件可以轻松地将预期使用文件共享存储文件应用程序或用户数据的应用程序“直接迁移”到云中。 Azure 文件既支持“经典”直接迁移方案(应用程序及其数据将移到 Azure 中),也支持“混合”直接迁移方案(应用程序数据将移到 Azure 文件中,应用程序继续在本地运行)。

3,简化云开发还可以通过众多方式使用 Azure 文件来简化新的云开发项目。 例如:

    • 共享应用程序设置:
      分布式应用程序的常见模式是将配置文件置于某个中心位置,然后可以从许多应用程序实例访问这些文件。 应用程序实例可以通过文件 REST API 加载其配置,人类可以根据需要通过本地装载 SMB 共享来访问这些配置。

    • 诊断共享:
      Azure 文件共享是云应用程序写入其日志、指标和故障转储的方便位置。 应用程序实例可以通过文件 REST API 写入日志,开发人员可以通过在其本地计算机上装载文件共享来访问这些日志。 这就带来了极大的灵活性,因为开发人员可以利用云开发,同时又不需要放弃他们所熟悉和喜爱的任何现有工具。

    • 开发/测试/调试:
      开发人员或管理员在云中的 VM 上工作时,通常需要一套工具或实用程序。 将此类实用程序和工具复制到每个 VM 可能非常耗时。 通过在 VM 上本地装载 Azure 文件共享,开发人员和管理员可以快速访问其工具和实用程序,而无需进行复制。

主要优点:

  1,共享访问:Azure 文件共享支持行业标准 SMB 协议,这意味着,你可以无缝地将本地文件共享替换为 Azure 文件共享,不需担心应用程序兼容性。 

  2,完全托管:不需管理硬件或 OS 即可创建 Azure 文件共享。

  3,脚本和工具:在管理 Azure 应用程序的过程中,可以使用 PowerShell cmdlet 和 Azure CLI 来创建、装载和管理 Azure 文件共享。

  4,复原能力:Azure 文件是从头开始构建的,我们的目的是确保其始终可用。

  5,熟悉的可变成性:在 Azure 中运行的应用程序可以通过文件系统 I/O API 访问共享中的数据。

好了,那今天的分析就开始。

——————–我是分割线——————–

Azure Blob Storage 存储系列:

1,Azure Storage 系列(一)入门简介 

2,Azure Storage 系列(二) .NET Core Web 项目中操作 Blob 存储

3,Azure Storage 系列(三)Blob 参数设置说明

4,Azure Storage 系列(四)在.Net 上使用Table Storage

5,Azure Storage 系列(五)通过Azure.Cosmos.Table 类库在.Net 上使用 Table Storage  

6,Azure Storage 系列(六)使用Azure Queue Storage

二,正文

1,Azure Portal 创建文件共享

找到之前创建好的 ”cnbateblogaccount“,选择 ”File service=》File shares“

添加新的文件共享,

Name:”bloglogfile“

Quota(配额):”10GB“(文件共享每个Share 最大为5TB

Tierss(访问层)选择默认: ”Transaction optimized“(事物已优化)

点击 ”Create“

 

点击 ”bloglogfile“ 进入 File share 详情页

 

点击 ”+ Add directory“ 创建文件目录

输入 Name:”Test“ ,点击 ”OK“

这里的文件目录与 Blob Storage 不同,File Storage 支持真正的文件目录。

点击进入”Test“ 目录,这里先试试上传一张图片试试

这里我选择一张叫 ”background“ 的背景图片

勾选 ”Overwrite if files already exist“,点击 “Upload”

我们可以看到上传的图片已经展示出来了,与Blog Storage 是一样的,Azure File Storage 中的每一个文件也是同时URL 来访问,例如:

//cnbateblogaccount.file.core.windows.net/bloglogfile/Test/background.jpg

2,通过代码去操作 File Storage

2.1,添加对 File Storage 支持的 Nuget 包

使用程序包管理控制台进行安装

Install-Package Azure.Storage.Files.Shares -Version 12.4.0

2.2,创建 IFileService 接口定义和 FileService 实现类方法,File控制器等

 1 using System;
 2 using System.Collections.Generic;
 3 using System.IO;
 4 using System.Linq;
 5 using System.Threading.Tasks;
 6 
 7 namespace Azure.Storage.Service
 8 {
 9     public interface IFileService
10     {
11         Task UpLoadFileAsync(string filePath, string fileName);
12 
13         Task DownFileAsync(string fileName, string downloadPath);
14 
15         Task<string> GetFileContentAsync(string fileName);
16 
17         Task<bool> DeleteFileAsync(string name);
18 
19     }
20 }

IFileService.cs

  1 using Microsoft.Azure.Storage;
  2 using Microsoft.Azure.Storage.File;
  3 using System;
  4 using System.Collections.Generic;
  5 using System.IO;
  6 using System.Linq;
  7 using System.Text;
  8 using System.Threading.Tasks;
  9 
 10 namespace Azure.Storage.Service
 11 {
 12     public class FileService : IFileService
 13     {
 14         private readonly CloudFileClient _cloudFileClient;
 15         public FileService(CloudStorageAccount cloudStorageClient)
 16         {
 17             this._cloudFileClient = cloudStorageClient.CreateCloudFileClient();
 18         }
 19 
 20         public async Task<bool> DeleteFileAsync(string filename)
 21         {
 22             var fileShare = _cloudFileClient.GetShareReference("bloglogfile");
 23 
 24             await fileShare.CreateIfNotExistsAsync();
 25             if (fileShare.Exists())
 26             {
 27                 var rootDir = fileShare.GetRootDirectoryReference();
 28                 var portraitDir = rootDir.GetDirectoryReference("portrait");
 29                 await portraitDir.CreateIfNotExistsAsync();
 30 
 31                 if (portraitDir.Exists())
 32                 {
 33                     var file = portraitDir.GetFileReference(filename);
 34 
 35                     return await file.DeleteIfExistsAsync();
 36                 }
 37             }
 38             return false;
 39         }
 40 
 41         public async Task DownFileAsync(string fileName, string downloadPath)
 42         {
 43             
 44             var fileShare = _cloudFileClient.GetShareReference("bloglogfile");
 45 
 46             await fileShare.CreateIfNotExistsAsync();
 47 
 48             if (fileShare.Exists())
 49             {
 50                 var rootDir = fileShare.GetRootDirectoryReference();
 51                 var portraitDir = rootDir.GetDirectoryReference("portrait");
 52                 await portraitDir.CreateIfNotExistsAsync();
 53 
 54                 if (portraitDir.Exists())
 55                 {
 56                     var file = portraitDir.GetFileReference(fileName);
 57 
 58                     await file.DownloadToFileAsync(downloadPath, FileMode.Create);
 59                 }
 60             }
 61         }
 62 
 63         public async Task<string> GetFileContentAsync(string fileName)
 64         {
 65             var fileShare = _cloudFileClient.GetShareReference("bloglogfile");
 66 
 67             await fileShare.CreateIfNotExistsAsync();
 68             if (fileShare.Exists())
 69             {
 70                 var rootDir= fileShare.GetRootDirectoryReference();
 71                 var portraitDir= rootDir.GetDirectoryReference("portrait");
 72                 await portraitDir.CreateIfNotExistsAsync();
 73 
 74                 if (portraitDir.Exists())
 75                 {
 76                     var file= portraitDir.GetFileReference(fileName);
 77 
 78                     return file.DownloadTextAsync().Result;
 79                 }
 80             }
 81             return string.Empty;
 82         }
 83 
 84         public async Task UpLoadFileAsync(string filePath, string fileName)
 85         {
 86             var fileShare = _cloudFileClient.GetShareReference("bloglogfile");
 87 
 88             await fileShare.CreateIfNotExistsAsync();
 89 
 90             if (fileShare.Exists())
 91             {
 92                 var rootDir = fileShare.GetRootDirectoryReference();
 93                 var portraitDir = rootDir.GetDirectoryReference("portrait");
 94                 await portraitDir.CreateIfNotExistsAsync();
 95 
 96                 if (portraitDir.Exists())
 97                 {
 98                     var file = portraitDir.GetFileReference(fileName);
 99 
100                     await file.UploadFromFileAsync(filePath);
101                 }
102             }
103         }
104     }
105 }

FileService.cs

 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Text;
 5 using System.Threading.Tasks;
 6 using Azure.Storage.Service;
 7 using Microsoft.AspNetCore.Http;
 8 using Microsoft.AspNetCore.Mvc;
 9 using Microsoft.VisualBasic;
10 
11 namespace Azure.Storage.Controllers
12 {
13     [Route("File")]
14     public class FileExplorerController : Controller
15     {
16 
17         private readonly IFileService _fileService;
18 
19         public FileExplorerController(IFileService fileService)
20         {
21             this._fileService = fileService;
22         }
23 
24 
25         /// <summary>
26         /// 上传文件
27         /// </summary>
28         /// <returns></returns>
29         [HttpPost("UploadFile")]
30         public async Task UploadFile()
31         {
32             string filePath = "D:\\Azure_File_UpLoad\\100.jpg";
33             string fileName = "100.jpg";
34             await _fileService.UpLoadFileAsync(filePath, fileName);
35         }
36 
37         [HttpGet("DownloadFile")]
38         public async Task DownloadFile()
39         {
40             string filePath = "D:\\Azure_File_DownLoad\\100.jpg";
41             string fileName = "100.jpg";
42             await _fileService.DownFileAsync(fileName, filePath);
43         }
44 
45         [HttpGet("GetFileContent")]
46         public async Task<IActionResult> GetFileContentAsync()
47         {
48             string fileName = "AZ-300考试说明.txt";
49             var data= await _fileService.GetFileContentAsync(fileName);
50             return Ok(data);
51         }
52 
53 
54         [HttpDelete("DeleteFile")]
55         public async Task<IActionResult> DeleteFileAsync()
56         {
57             string fileName = "AZ-300考试说明.txt";
58             await _fileService.DeleteFileAsync(fileName);
59             return Ok();
60         }
61     }
62 }

FileExplorerController.cs

2.3,添加对FileService,以及 CloudStorageAccount 的依赖注入

services.AddSingleton(x => new AzureStorage.CloudStorageAccount(new AzureStorage.Auth.StorageCredentials("cnbateblogaccount", "e2T2gYREFdxkYIJocvC4Wut7khxMWJCbQBp8tPM2EJt37QaUUlflTPAlkoJzIlY29aGYt8WW0xx1bckO4hLKJA=="),true));
services.AddSingleton<IFileService, FileService>();

3,使用 Postman 进行测试

3.1,上传文件

指定要上传的文件路径,已经文件名称

可以看到本地路径 “D:\Azure_File_UpLoad” 目录中有一个叫 “100.jpg” 的图片文件

回到postman,输入上传文件的链接,点击 “Send” 进行上传

回到Azure Portal 找到文件共享目录,我们可以看到已经创建好 “portrait” 的目录,点击进入此目录

可以看到自己刚刚上传的图片文件

3.2,下载文件

指定下载文件的目录 “D:\\Azure_File_DownLoad\\100.jpg”,以及需要下载的文件名称

输入下载文件的链接,点击 “Send”

回到本地计算机的 “D:\Azure_File_DownLoad” 目录,我们可以看到到当前下载的图片文件

接下来,还有获取txt文件内容,删除文件的操作,我这就不再演示了,大家可以自行下载代码进行操作。

 OK,今天的分享到此介绍。撒花🎉🎉🎉🎉🎉

三,结尾

github://github.com/yunqian44/Azure.Storage.git

作者:Allen 

版权:转载请在文章明显位置注明作者及出处。如发现错误,欢迎批评指正。

作者:Allen 版权:转载请在文章明显位置注明作者及出处。如发现错误,欢迎批评指正。
Tags: