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: