一,引言

  今天我们开始介绍 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 来访问,例如:

https://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:https://github.com/yunqian44/Azure.Storage.git

作者:Allen

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

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

Azure Storage 系列(七)使用Azure File Storage的更多相关文章

  1. Windows Azure 入门系列课程Windows Azure 入门系列课程

    Windows Azure 入门系列课程 https://www.microsoft.com/china/msdn/events/webcasts/shared/webcast/NewSeries/A ...

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

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

  3. Azure 基础:File Storage

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

  4. Azure Terraform(七)利用Azure DevOps 实现自动化部署基础资源(补充)

    一,引言 之前一篇文章有讲解到利用 利用Azure DevOps 实现自动化部署基础资源,当时 TF 代码没有针对 Azure 各个资源的封装,所有的资源代码全部写在一个 main.tf 文件中.然后 ...

  5. Windows Azure入门教学系列 (七):使用REST API访问Storage Service

    本文是Windows Azure入门教学的第七篇文章. 本文将会介绍如何使用REST API来直接访问Storage Service. 在前三篇教学中,我们已经学习了使用Windows Azure S ...

  6. Windows Azure入门教学系列 (四):使用Blob Storage

    本文将会介绍如何使用Blob Storage.Blob Storage可以看做是云端的文件系统.与桌面操作系统上不同,我们是通过REST API来进行对文件的操作.有关REST API的详细信息,请参 ...

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

    一,引言 今天我们就不多说废话了,直接进入正题,Azure Table Storage.开始内容之前,我们先介绍一下Azure Table Storage. 1,什么是Azure Table Stor ...

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

    一,引言 上一篇文章我们在.NET 项目中添加了 “WindowsAzure.Storage” 的 NuGet 包进行操作Table 数据,但是使用的 “WindowsAzure.Storage”  ...

  9. Azure Storage 系列(六)使用Azure Queue Storage

    一,引言 在之前介绍到 Azure Storage 第一篇文章中就有介绍到 Azure Storage 是 Azure 上提供的一项存储服务,Azure 存储包括 对象.文件.磁盘.队列和表存储.这里 ...

随机推荐

  1. postman 基本应用

    前言 进行post高级应用的一个整理. 正文 批量测试和简单自动化测试 在点击collects的列表中,会弹出下面这个选项. 上面有3个按钮,分别是分享.运行.展示在网页中. 那么就看下这个运行吧. ...

  2. SSM项目_Eclipse卡进程 一直loading加载spring-xx.xsd/无法加载SpringXSD文件

    你遇到了套娃,请进https:////www.cnblogs.com/steamer/articles/12500645.html查看答案

  3. AtCoder Beginner Contest 177 题解

    AtCoder Beginner Contest 177 题解 目录 AtCoder Beginner Contest 177 题解 A - Don't be late B - Substring C ...

  4. 硬盘网盘U盘全部可以丢掉了,这个设备可以让你享受随身带着几个T的感受

    前言 有小伙伴问我,你怎么老写技术类文章,能不能写点别的. 其实我兴趣挺广泛的,早年还有机会做个游戏博主,可惜最近2年金盆洗手了.戒了手游,ns和ps4都在吃灰.能完整玩完的游戏屈指可数.但是对于折腾 ...

  5. 盒子上下滚动到js 底部触发的事件

    //html是用法举列子,js亲测有效(把这段js#scro加到你要滚动的盒子) <div id="scro">  <div>1</div> & ...

  6. [工作积累] shadowmap 改进

    前面几篇阴影相关的: https://www.cnblogs.com/crazii/p/5443534.html 这个是在做bh3 MMD角色自阴影时的笔记 https://www.cnblogs.c ...

  7. tp5下的文件上传与下载类

    class FieldInterfun extends Controller { /** * [upload 上传文件] * @param [type] $file [description] * @ ...

  8. java 注解开发

    目录 注解 JDK自带的注解三个 注解分类 按照运行机制 按照来源分类 自定义注解的语法要求 元注解 解析注解 获取注解的注解 Spring中的注解 组合注解 注解 JDK自带的注解三个 @Overr ...

  9. java基础(swing+jsp+mybatis配置)

    JAVA SE GUI编程(swing) # 组件 描述 1 JFrame 一个普通的窗口(绝大多数 Swing 图形界面程序使用 JFrame 作为顶层容器) 2 JDialog 对话框 常用的中间 ...

  10. linux 用户、用户组的操作

    1.查看linux系统下的所有用户 方式一:grep bash /etc/passwd 方式二:查看etc/passwd文件,其他第三个参数大于500的都是用户自己新增的 vim /etc/passw ...