[C#][.NET Core] Azure Storage Blob 手把手從頭開始 - 上傳檔案,取得檔案相關資訊

2019-02-01

上一篇文章,我們 傳上一個文字檔,並取得他的公開資訊,這一篇文章 我們要來上傳一個本地的檔案(圖片檔) 並且 會取得 該檔案的相關資訊


物換星移,似乎微軟要換成 v12 版本 了,  請參考新的文章 

上傳圖檔 - 我要將本地的 sample.jpg 上傳到 Blob 上面的 TEST2/TEST21/IMAGES/hamimelon.jpg

var connsctionString = "your_connection_string"; var cloudStorage = Microsoft.WindowsAzure.Storage.CloudStorageAccount.Parse(connsctionString); var cloudBlobClient = cloudStorage.CreateCloudBlobClient(); var cloudBlobContainer = cloudBlobClient.GetContainerReference("donmablogsample"); var resultCreateContainer = cloudBlobContainer.CreateIfNotExistsAsync().Result; Console.WriteLine("donmablogsample create already."); Microsoft.WindowsAzure.Storage.Blob.CloudBlobDirectory cloudBlobDirectory = cloudBlobContainer.GetDirectoryReference("TEST2/TEST21/IMAGES/"); cloudBlobDirectory.GetBlockBlobReference("hamimelon.jpg").UploadFromFileAsync(AppDomain.CurrentDomain.BaseDirectory + "sample.jpg").GetAwaiter().GetResult(); Console.WriteLine("Upload Success");

之後我要取得 TEST2/TEST21/IMAGES/hamimelon.jpg 的檔案資料資訊包含 Size , ContentType , Created , LastModified

var connsctionString = "your_connection_string"; var cloudStorage = Microsoft.WindowsAzure.Storage.CloudStorageAccount.Parse(connsctionString); var cloudBlobClient = cloudStorage.CreateCloudBlobClient(); var cloudBlobContainer = cloudBlobClient.GetContainerReference("donmablogsample"); var resultCreateContainer = cloudBlobContainer.CreateIfNotExistsAsync().Result; Console.WriteLine("donmablogsample create already."); Microsoft.WindowsAzure.Storage.Blob.CloudBlobDirectory cloudBlobDirectory = cloudBlobContainer.GetDirectoryReference("TEST2/TEST21/IMAGES/"); Console.WriteLine("Get File Content : TEST2/TEST21/IMAGES/hamimelon.jpg"); var blockReference = cloudBlobDirectory.GetBlockBlobReference("hamimelon.jpg"); blockReference.FetchAttributesAsync().GetAwaiter().GetResult(); Console.WriteLine(" TEST2/TEST21/IMAGES/hamimelon.jpg Size =>" + blockReference.Properties.Length.ToString("#,##0") + "bytes"); Console.WriteLine(" TEST2/TEST21/IMAGES/hamimelon.jpg ContentType =>" + blockReference.Properties.ContentType); Console.WriteLine(" TEST2/TEST21/IMAGES/hamimelon.jpg Created =>" + blockReference.Properties.Created.Value.ToString("yyyy-MM-dd HH:mm:ss")); Console.WriteLine(" TEST2/TEST21/IMAGES/hamimelon.jpg LastModified =>" + blockReference.Properties.LastModified.Value.ToString("yyyy-MM-dd HH:mm:ss"));




如果你覺得我寫得不好或是太簡單,請看下面的原文吧

reference:

https://docs.microsoft.com/en-us/azure/storage/blobs/storage-blobs-overview

https://docs.microsoft.com/en-us/dotnet/api/microsoft.windowsazure.storage.file.cloudfile.uploadfromfileasync?view=azure-dotnet#Microsoft_WindowsAzure_Storage_File_CloudFile_UploadFromFileAsync_System_String_System_Threading_CancellationToken_


https://docs.microsoft.com/en-us/dotnet/api/microsoft.windowsazure.storage.blob.cloudblob.fetchattributesasync?view=azure-dotnet#Microsoft_WindowsAzure_Storage_Blob_CloudBlob_FetchAttributesAsync_Microsoft_WindowsAzure_Storage_AccessCondition_Microsoft_WindowsAzure_Storage_Blob_BlobRequestOptions_Microsoft_WindowsAzure_Storage_OperationContext_System_Threading_CancellationToken_


當麻許的碎念筆記 2014 | Donma Hsu Design.