From fa18386a611151deef401379c031c3b2f1c92c03 Mon Sep 17 00:00:00 2001 From: Avijit Samanta Date: Thu, 29 Mar 2018 15:16:52 +0800 Subject: [PATCH 1/2] "UploadFromFileAsync" will not work as the file has not been saved locally before upload to blob storage; As it has already been available with request stream "stream" mechanism should be used. --- WebApp-Storage-DotNet/Controllers/HomeController.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/WebApp-Storage-DotNet/Controllers/HomeController.cs b/WebApp-Storage-DotNet/Controllers/HomeController.cs index 9dd9ec5..0708920 100644 --- a/WebApp-Storage-DotNet/Controllers/HomeController.cs +++ b/WebApp-Storage-DotNet/Controllers/HomeController.cs @@ -112,7 +112,7 @@ public async Task UploadAsync() for (int i = 0; i < fileCount; i++) { CloudBlockBlob blob = blobContainer.GetBlockBlobReference(GetRandomBlobName(files[i].FileName)); - await blob.UploadFromFileAsync(files[i].FileName, FileMode.Open); + await blob.UploadFromStreamAsync(files[i].InputStream); } } return RedirectToAction("Index"); From 9a1a9c907711250e94b2162ebb053dc3dac4004a Mon Sep 17 00:00:00 2001 From: Avijit Samanta Date: Thu, 29 Mar 2018 17:44:14 +0800 Subject: [PATCH 2/2] did some code cleanup and common practices applied --- .../Controllers/HomeController.cs | 15 +++++++-------- 1 file changed, 7 insertions(+), 8 deletions(-) diff --git a/WebApp-Storage-DotNet/Controllers/HomeController.cs b/WebApp-Storage-DotNet/Controllers/HomeController.cs index 0708920..8efc749 100644 --- a/WebApp-Storage-DotNet/Controllers/HomeController.cs +++ b/WebApp-Storage-DotNet/Controllers/HomeController.cs @@ -19,10 +19,8 @@ namespace WebApp_Storage_DotNet.Controllers using System.Web; using System.Threading.Tasks; using System.IO; - using Microsoft.WindowsAzure; using Microsoft.WindowsAzure.Storage; using Microsoft.WindowsAzure.Storage.Blob; - using Microsoft.Azure; using System.Configuration; /// @@ -63,7 +61,7 @@ public async Task Index() { // Retrieve storage account information from connection string // How to create a storage connection string - http://msdn.microsoft.com/en-us/library/azure/ee758697.aspx - CloudStorageAccount storageAccount = CloudStorageAccount.Parse(ConfigurationManager.AppSettings["StorageConnectionString"].ToString()); + CloudStorageAccount storageAccount = CloudStorageAccount.Parse(ConfigurationManager.AppSettings["StorageConnectionString"]); // Create a blob client for interacting with the blob service. blobClient = storageAccount.CreateCloudBlobClient(); @@ -80,7 +78,7 @@ public async Task Index() List allBlobs = new List(); foreach (IListBlobItem blob in blobContainer.ListBlobs()) { - if (blob.GetType() == typeof(CloudBlockBlob)) + if (blob is CloudBlockBlob) allBlobs.Add(blob.Uri); } @@ -163,9 +161,10 @@ public async Task DeleteAll() { foreach (var blob in blobContainer.ListBlobs()) { - if (blob.GetType() == typeof(CloudBlockBlob)) + var blockBlob = blob as CloudBlockBlob; + if (blockBlob != null) { - await ((CloudBlockBlob)blob).DeleteIfExistsAsync(); + await blockBlob.DeleteIfExistsAsync(); } } @@ -182,10 +181,10 @@ public async Task DeleteAll() /// /// string GetRandomBlobName(string filename): Generates a unique random file name to be uploaded /// - private string GetRandomBlobName(string filename) + private static string GetRandomBlobName(string filename) { string ext = Path.GetExtension(filename); - return string.Format("{0:10}_{1}{2}", DateTime.Now.Ticks, Guid.NewGuid(), ext); + return $"{DateTime.Now.Ticks:10}_{Guid.NewGuid()}{ext}"; } } }