1. 程式人生 > >Azure Storage 系列(七)使用Azure File Storage

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 來訪問,例如:

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,今天的分享到此介紹。撒花