github:https://github.com/yunqian44/Azure.Storage.git
做者:Allen
版權:轉載請在文章明顯位置註明做者及出處。如發現錯誤,歡迎批評指正。
今天咱們開始介紹 Storage 中的最後一個類型的存儲----- File Storage(文件存儲),Azure File Storage 在雲端提供徹底託管的文件共享,這些共享項可經過行業標準的服務器消息塊 (SMB) 協議進行訪問。 Azure 文件共享可由雲或者 Windows、Linux 和 macOS 的本地部署同時裝載。 此外,可使用 Azure 文件同步將 Azure 文件共享緩存在 Windows Server 上,以加快訪問速度(與在數據使用位置進行訪問的速度至關)。html
(一) 就有人問,既然又是也是做爲文件存儲,項目系統中生產的一些日誌,或者上傳的圖片能夠指定虛擬目錄用來保存,或者使用 Blob Storage,使用 File Storage的好處是什麼?git
答:1,取代或補充本地文件服務器:可使用 Azure 文件來徹底取代或補充傳統的本地文件服務器或 NAS 設備。 流行的操做系統可在世界各地直接裝載 Azure 文件共享。 此外,可使用 Azure 文件同步將 Azure 文件共享複製到本地或雲中的 Windows Server,以便在使用位置對數據進行高性能的分佈式緩存。 使用最新版本的 Azure 文件存儲 AD 身份驗證,Azure 文件共享可繼續使用本地託管的 AD 進行訪問控制。github
2,「直接遷移」應用程序:藉助 Azure 文件能夠輕鬆地將預期使用文件共享存儲文件應用程序或用戶數據的應用程序「直接遷移」到雲中。 Azure 文件既支持「經典」直接遷移方案(應用程序及其數據將移到 Azure 中),也支持「混合」直接遷移方案(應用程序數據將移到 Azure 文件中,應用程序繼續在本地運行)。windows
3,簡化雲開發:還能夠經過衆多方式使用 Azure 文件來簡化新的雲開發項目。 例如:緩存
共享應用程序設置:
分佈式應用程序的常見模式是將配置文件置於某個中心位置,而後能夠從許多應用程序實例訪問這些文件。 應用程序實例能夠經過文件 REST API 加載其配置,人類能夠根據須要經過本地裝載 SMB 共享來訪問這些配置。服務器
診斷共享:
Azure 文件共享是雲應用程序寫入其日誌、指標和故障轉儲的方便位置。 應用程序實例能夠經過文件 REST API 寫入日誌,開發人員能夠經過在其本地計算機上裝載文件共享來訪問這些日誌。 這就帶來了極大的靈活性,由於開發人員能夠利用雲開發,同時又不須要放棄他們所熟悉和喜好的任何現有工具。async
開發/測試/調試:
開發人員或管理員在雲中的 VM 上工做時,一般須要一套工具或實用程序。 將此類實用程序和工具複製到每一個 VM 可能很是耗時。 經過在 VM 上本地裝載 Azure 文件共享,開發人員和管理員能夠快速訪問其工具和實用程序,而無需進行復制。分佈式
主要優勢:ide
1,共享訪問:Azure 文件共享支持行業標準 SMB 協議,這意味着,你能夠無縫地將本地文件共享替換爲 Azure 文件共享,不需擔憂應用程序兼容性。 工具
2,徹底託管:不需管理硬件或 OS 便可建立 Azure 文件共享。
3,腳本和工具:在管理 Azure 應用程序的過程當中,可使用 PowerShell cmdlet 和 Azure CLI 來建立、裝載和管理 Azure 文件共享。
4,復原能力:Azure 文件是從頭開始構建的,咱們的目的是確保其始終可用。
5,熟悉的可變成性:在 Azure 中運行的應用程序能夠經過文件系統 I/O API 訪問共享中的數據。
好了,那今天的分析就開始。
--------------------我是分割線--------------------
Azure Blob 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
找到以前建立好的 」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.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 }
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 }
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 }
2.3,添加對FileService,以及 CloudStorageAccount 的依賴注入
services.AddSingleton(x => new AzureStorage.CloudStorageAccount(new AzureStorage.Auth.StorageCredentials("cnbateblogaccount", "e2T2gYREFdxkYIJocvC4Wut7khxMWJCbQBp8tPM2EJt37QaUUlflTPAlkoJzIlY29aGYt8WW0xx1bckO4hLKJA=="),true)); services.AddSingleton<IFileService, FileService>();
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,今天的分享到此介紹。撒花🎉🎉🎉🎉🎉