From 1ece46e729e4793617a449f87d078f3327c69664 Mon Sep 17 00:00:00 2001 From: Mark Hopkins Date: Tue, 21 Jan 2020 16:10:41 -0800 Subject: [PATCH 1/8] Updated and removed using statements --- .../Controllers/HomeController.cs | 6 +--- .../Controllers/ImagesController.cs | 19 ++++-------- .../ImageResizeWebApp.csproj | 30 +++++++++++-------- .../Models/AzureStorageConfig.cs | 7 +---- .../ImageResizeWebApp/Program.cs | 7 ----- .../ImageResizeWebApp/Startup.cs | 7 +---- 6 files changed, 27 insertions(+), 49 deletions(-) diff --git a/ImageResizeWebApp/ImageResizeWebApp/Controllers/HomeController.cs b/ImageResizeWebApp/ImageResizeWebApp/Controllers/HomeController.cs index 4a169fb..6b75103 100644 --- a/ImageResizeWebApp/ImageResizeWebApp/Controllers/HomeController.cs +++ b/ImageResizeWebApp/ImageResizeWebApp/Controllers/HomeController.cs @@ -1,9 +1,5 @@ -using System; -using System.Collections.Generic; -using System.Diagnostics; -using System.Linq; -using System.Threading.Tasks; using Microsoft.AspNetCore.Mvc; +using System.Diagnostics; namespace ImageResizeWebApp.Controllers { diff --git a/ImageResizeWebApp/ImageResizeWebApp/Controllers/ImagesController.cs b/ImageResizeWebApp/ImageResizeWebApp/Controllers/ImagesController.cs index a3fad2a..bc4ca3f 100644 --- a/ImageResizeWebApp/ImageResizeWebApp/Controllers/ImagesController.cs +++ b/ImageResizeWebApp/ImageResizeWebApp/Controllers/ImagesController.cs @@ -1,19 +1,12 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Threading.Tasks; -using Microsoft.AspNetCore.Mvc; +using ImageResizeWebApp.Helpers; using ImageResizeWebApp.Models; +using Microsoft.AspNetCore.Http; +using Microsoft.AspNetCore.Mvc; using Microsoft.Extensions.Options; - +using System; +using System.Collections.Generic; using System.IO; -using Microsoft.Azure; -using Microsoft.WindowsAzure.Storage; -using Microsoft.WindowsAzure.Storage.Blob; -using Microsoft.WindowsAzure.Storage.Auth; -using System.Net.Http; -using Microsoft.AspNetCore.Http; -using ImageResizeWebApp.Helpers; +using System.Threading.Tasks; namespace ImageResizeWebApp.Controllers { diff --git a/ImageResizeWebApp/ImageResizeWebApp/ImageResizeWebApp.csproj b/ImageResizeWebApp/ImageResizeWebApp/ImageResizeWebApp.csproj index f02d019..a646f46 100644 --- a/ImageResizeWebApp/ImageResizeWebApp/ImageResizeWebApp.csproj +++ b/ImageResizeWebApp/ImageResizeWebApp/ImageResizeWebApp.csproj @@ -5,7 +5,7 @@ - netcoreapp2.0 + netcoreapp2.2 true Latest $(AssetTargetFallback);portable-net45+win8+wp8+wpa81; @@ -17,17 +17,23 @@ - - - - - - - - - - - + + + + + + all + runtime; build; native; contentfiles; analyzers; buildtransitive + + + + all + runtime; build; native; contentfiles; analyzers; buildtransitive + + + + + diff --git a/ImageResizeWebApp/ImageResizeWebApp/Models/AzureStorageConfig.cs b/ImageResizeWebApp/ImageResizeWebApp/Models/AzureStorageConfig.cs index 67df445..a12f694 100644 --- a/ImageResizeWebApp/ImageResizeWebApp/Models/AzureStorageConfig.cs +++ b/ImageResizeWebApp/ImageResizeWebApp/Models/AzureStorageConfig.cs @@ -1,9 +1,4 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Threading.Tasks; - -namespace ImageResizeWebApp.Models +namespace ImageResizeWebApp.Models { public class AzureStorageConfig { diff --git a/ImageResizeWebApp/ImageResizeWebApp/Program.cs b/ImageResizeWebApp/ImageResizeWebApp/Program.cs index b17e268..033345c 100644 --- a/ImageResizeWebApp/ImageResizeWebApp/Program.cs +++ b/ImageResizeWebApp/ImageResizeWebApp/Program.cs @@ -1,12 +1,5 @@ -using System; -using System.Collections.Generic; -using System.IO; -using System.Linq; -using System.Threading.Tasks; using Microsoft.AspNetCore; using Microsoft.AspNetCore.Hosting; -using Microsoft.Extensions.Configuration; -using Microsoft.Extensions.Logging; namespace ImageResizeWebApp { diff --git a/ImageResizeWebApp/ImageResizeWebApp/Startup.cs b/ImageResizeWebApp/ImageResizeWebApp/Startup.cs index 058451a..4816214 100644 --- a/ImageResizeWebApp/ImageResizeWebApp/Startup.cs +++ b/ImageResizeWebApp/ImageResizeWebApp/Startup.cs @@ -1,13 +1,8 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Threading.Tasks; +using ImageResizeWebApp.Models; using Microsoft.AspNetCore.Builder; using Microsoft.AspNetCore.Hosting; -using Microsoft.AspNetCore.SpaServices.Webpack; using Microsoft.Extensions.Configuration; using Microsoft.Extensions.DependencyInjection; -using ImageResizeWebApp.Models; namespace ImageResizeWebApp { From 05615536151a49b0f16d12499b9c5609035f3bd4 Mon Sep 17 00:00:00 2001 From: Mark Hopkins Date: Wed, 22 Jan 2020 15:46:35 -0800 Subject: [PATCH 2/8] Updated to v12 --- .../Helpers/StorageHelper.cs | 74 +++++++++++-------- .../ImageResizeWebApp.csproj | 5 +- 2 files changed, 48 insertions(+), 31 deletions(-) diff --git a/ImageResizeWebApp/ImageResizeWebApp/Helpers/StorageHelper.cs b/ImageResizeWebApp/ImageResizeWebApp/Helpers/StorageHelper.cs index 64280c3..85c0c29 100644 --- a/ImageResizeWebApp/ImageResizeWebApp/Helpers/StorageHelper.cs +++ b/ImageResizeWebApp/ImageResizeWebApp/Helpers/StorageHelper.cs @@ -1,14 +1,14 @@ using ImageResizeWebApp.Models; using Microsoft.AspNetCore.Http; -using Microsoft.WindowsAzure.Storage; -using Microsoft.WindowsAzure.Storage.Auth; -using Microsoft.WindowsAzure.Storage.Blob; +using Azure.Storage; +using Azure.Storage.Blobs; using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Threading.Tasks; +using Azure.Storage.Blobs.Models; namespace ImageResizeWebApp.Helpers { @@ -29,23 +29,29 @@ public static bool IsImage(IFormFile file) public static async Task UploadFileToStorage(Stream fileStream, string fileName, AzureStorageConfig _storageConfig) { + // Create a URI to the blob + Uri uri = new Uri("https://" + _storageConfig.AccountName + "/.blob.core.windows.net/" + _storageConfig.ImageContainer + "/" + fileName); + // Create storagecredentials object by reading the values from the configuration (appsettings.json) - StorageCredentials storageCredentials = new StorageCredentials(_storageConfig.AccountName, _storageConfig.AccountKey); + StorageSharedKeyCredential storageCredentials = new StorageSharedKeyCredential(_storageConfig.AccountName, _storageConfig.AccountKey); // Create cloudstorage account by passing the storagecredentials - CloudStorageAccount storageAccount = new CloudStorageAccount(storageCredentials, true); + //CloudStorageAccount storageAccount = new CloudStorageAccount(storageCredentials, true); + //BlobServiceClient blobServiceClient = new BlobServiceClient(uri); // Create the blob client. - CloudBlobClient blobClient = storageAccount.CreateCloudBlobClient(); + BlobClient blobClient = new BlobClient(uri, storageCredentials); // Get reference to the blob container by passing the name by reading the value from the configuration (appsettings.json) - CloudBlobContainer container = blobClient.GetContainerReference(_storageConfig.ImageContainer); + //CloudBlobContainer container = blobClient.GetContainerReference(_storageConfig.ImageContainer); + //BlobContainerClient container = await blobServiceClient.CreateBlobContainerAsync(_storageConfig.ImageContainer); // Get the reference to the block blob from the container - CloudBlockBlob blockBlob = container.GetBlockBlobReference(fileName); + //CloudBlockBlob blockBlob = container.GetBlockBlobReference(fileName); // Upload the file - await blockBlob.UploadFromStreamAsync(fileStream); + //await blockBlob.UploadFromStreamAsync(fileStream); + await blobClient.UploadAsync(fileStream); return await Task.FromResult(true); } @@ -54,41 +60,51 @@ public static async Task> GetThumbNailUrls(AzureStorageConfig _stor { List thumbnailUrls = new List(); + // Create a URI to the thumbnail container + Uri uri = new Uri("https://" + _storageConfig.AccountName + "/.blob.core.windows.net/"); + // Create storagecredentials object by reading the values from the configuration (appsettings.json) - StorageCredentials storageCredentials = new StorageCredentials(_storageConfig.AccountName, _storageConfig.AccountKey); + //StorageSharedKeyCredential storageCredentials = new StorageSharedKeyCredential(_storageConfig.AccountName, _storageConfig.AccountKey); // Create cloudstorage account by passing the storagecredentials - CloudStorageAccount storageAccount = new CloudStorageAccount(storageCredentials, true); + //CloudStorageAccount storageAccount = new CloudStorageAccount(storageCredentials, true); + BlobServiceClient blobServiceClient = new BlobServiceClient(uri); // Create blob client - CloudBlobClient blobClient = storageAccount.CreateCloudBlobClient(); + //BlobClient blobClient = new BlobClient(uri, storageCredentials); // Get reference to the container - CloudBlobContainer container = blobClient.GetContainerReference(_storageConfig.ThumbnailContainer); + //CloudBlobContainer container = blobClient.GetContainerReference(_storageConfig.ThumbnailContainer); + BlobContainerClient container = await blobServiceClient.CreateBlobContainerAsync(_storageConfig.ThumbnailContainer); - BlobContinuationToken continuationToken = null; + //BlobContinuationToken continuationToken = null; - BlobResultSegment resultSegment = null; + //BlobResultSegment resultSegment = null; //Call ListBlobsSegmentedAsync and enumerate the result segment returned, while the continuation token is non-null. //When the continuation token is null, the last page has been returned and execution can exit the loop. - do + //do + //{ + // //This overload allows control of the page size. You can return all remaining results by passing null for the maxResults parameter, + // //or by calling a different overload. + // resultSegment = await container.ListBlobsSegmentedAsync("", true, BlobListingDetails.All, 10, continuationToken, null, null); + + // foreach (var blobItem in resultSegment.Results) + // { + // thumbnailUrls.Add(blobItem.StorageUri.PrimaryUri.ToString()); + // } + + // //Get the continuation token. + // continuationToken = resultSegment.ContinuationToken; + //} + //while (continuationToken != null); + + foreach (BlobItem blobItem in container.GetBlobs()) { - //This overload allows control of the page size. You can return all remaining results by passing null for the maxResults parameter, - //or by calling a different overload. - resultSegment = await container.ListBlobsSegmentedAsync("", true, BlobListingDetails.All, 10, continuationToken, null, null); - - foreach (var blobItem in resultSegment.Results) - { - thumbnailUrls.Add(blobItem.StorageUri.PrimaryUri.ToString()); - } - - //Get the continuation token. - continuationToken = resultSegment.ContinuationToken; + string thumbnailUrl = "https://" + _storageConfig.AccountName + "/.blob.core.windows.net/" + _storageConfig.ThumbnailContainer + "/" + blobItem.Name; + thumbnailUrls.Add(thumbnailUrl); } - while (continuationToken != null); - return await Task.FromResult(thumbnailUrls); } } diff --git a/ImageResizeWebApp/ImageResizeWebApp/ImageResizeWebApp.csproj b/ImageResizeWebApp/ImageResizeWebApp/ImageResizeWebApp.csproj index a646f46..0a193a0 100644 --- a/ImageResizeWebApp/ImageResizeWebApp/ImageResizeWebApp.csproj +++ b/ImageResizeWebApp/ImageResizeWebApp/ImageResizeWebApp.csproj @@ -1,4 +1,4 @@ - + Exe @@ -17,6 +17,8 @@ + + @@ -33,7 +35,6 @@ - From b0b61921918e53646003b01e12b37f0dc80a46a6 Mon Sep 17 00:00:00 2001 From: Mark Hopkins Date: Thu, 23 Jan 2020 09:40:04 -0800 Subject: [PATCH 3/8] Updated to matching packages --- .../ImageResizeWebApp/ImageResizeWebApp.csproj | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/ImageResizeWebApp/ImageResizeWebApp/ImageResizeWebApp.csproj b/ImageResizeWebApp/ImageResizeWebApp/ImageResizeWebApp.csproj index 0a193a0..cf53a42 100644 --- a/ImageResizeWebApp/ImageResizeWebApp/ImageResizeWebApp.csproj +++ b/ImageResizeWebApp/ImageResizeWebApp/ImageResizeWebApp.csproj @@ -5,7 +5,7 @@ - netcoreapp2.2 + netcoreapp2.1 true Latest $(AssetTargetFallback);portable-net45+win8+wp8+wpa81; @@ -13,14 +13,14 @@ exe - + - + From 1130984c77a85b9cb5647e69ed384664a012e81b Mon Sep 17 00:00:00 2001 From: Mark Hopkins Date: Thu, 23 Jan 2020 13:45:10 -0800 Subject: [PATCH 4/8] Corrected package versions and updated code to .NET v12 --- .../Helpers/StorageHelper.cs | 69 +++++-------------- .../ImageResizeWebApp.csproj | 29 +++----- 2 files changed, 27 insertions(+), 71 deletions(-) diff --git a/ImageResizeWebApp/ImageResizeWebApp/Helpers/StorageHelper.cs b/ImageResizeWebApp/ImageResizeWebApp/Helpers/StorageHelper.cs index 85c0c29..aaedda3 100644 --- a/ImageResizeWebApp/ImageResizeWebApp/Helpers/StorageHelper.cs +++ b/ImageResizeWebApp/ImageResizeWebApp/Helpers/StorageHelper.cs @@ -30,27 +30,21 @@ public static bool IsImage(IFormFile file) public static async Task UploadFileToStorage(Stream fileStream, string fileName, AzureStorageConfig _storageConfig) { // Create a URI to the blob - Uri uri = new Uri("https://" + _storageConfig.AccountName + "/.blob.core.windows.net/" + _storageConfig.ImageContainer + "/" + fileName); + Uri blobUri = new Uri("https://" + + _storageConfig.AccountName + + ".blob.core.windows.net/" + + _storageConfig.ImageContainer + + "/" + fileName); - // Create storagecredentials object by reading the values from the configuration (appsettings.json) - StorageSharedKeyCredential storageCredentials = new StorageSharedKeyCredential(_storageConfig.AccountName, _storageConfig.AccountKey); - - // Create cloudstorage account by passing the storagecredentials - //CloudStorageAccount storageAccount = new CloudStorageAccount(storageCredentials, true); - //BlobServiceClient blobServiceClient = new BlobServiceClient(uri); + // Create StorageSharedKeyCredentials object by reading + // the values from the configuration (appsettings.json) + StorageSharedKeyCredential storageCredentials = + new StorageSharedKeyCredential(_storageConfig.AccountName, _storageConfig.AccountKey); // Create the blob client. - BlobClient blobClient = new BlobClient(uri, storageCredentials); - - // Get reference to the blob container by passing the name by reading the value from the configuration (appsettings.json) - //CloudBlobContainer container = blobClient.GetContainerReference(_storageConfig.ImageContainer); - //BlobContainerClient container = await blobServiceClient.CreateBlobContainerAsync(_storageConfig.ImageContainer); - - // Get the reference to the block blob from the container - //CloudBlockBlob blockBlob = container.GetBlockBlobReference(fileName); + BlobClient blobClient = new BlobClient(blobUri, storageCredentials); // Upload the file - //await blockBlob.UploadFromStreamAsync(fileStream); await blobClient.UploadAsync(fileStream); return await Task.FromResult(true); @@ -60,49 +54,18 @@ public static async Task> GetThumbNailUrls(AzureStorageConfig _stor { List thumbnailUrls = new List(); - // Create a URI to the thumbnail container - Uri uri = new Uri("https://" + _storageConfig.AccountName + "/.blob.core.windows.net/"); - - // Create storagecredentials object by reading the values from the configuration (appsettings.json) - //StorageSharedKeyCredential storageCredentials = new StorageSharedKeyCredential(_storageConfig.AccountName, _storageConfig.AccountKey); - - // Create cloudstorage account by passing the storagecredentials - //CloudStorageAccount storageAccount = new CloudStorageAccount(storageCredentials, true); - BlobServiceClient blobServiceClient = new BlobServiceClient(uri); + // Create a URI to the storage account + Uri accountUri = new Uri("https://" + _storageConfig.AccountName + ".blob.core.windows.net/"); - // Create blob client - //BlobClient blobClient = new BlobClient(uri, storageCredentials); + // Create BlobServiceClient from the account URI + BlobServiceClient blobServiceClient = new BlobServiceClient(accountUri); // Get reference to the container - //CloudBlobContainer container = blobClient.GetContainerReference(_storageConfig.ThumbnailContainer); - BlobContainerClient container = await blobServiceClient.CreateBlobContainerAsync(_storageConfig.ThumbnailContainer); - - //BlobContinuationToken continuationToken = null; - - //BlobResultSegment resultSegment = null; - - //Call ListBlobsSegmentedAsync and enumerate the result segment returned, while the continuation token is non-null. - //When the continuation token is null, the last page has been returned and execution can exit the loop. - //do - //{ - // //This overload allows control of the page size. You can return all remaining results by passing null for the maxResults parameter, - // //or by calling a different overload. - // resultSegment = await container.ListBlobsSegmentedAsync("", true, BlobListingDetails.All, 10, continuationToken, null, null); - - // foreach (var blobItem in resultSegment.Results) - // { - // thumbnailUrls.Add(blobItem.StorageUri.PrimaryUri.ToString()); - // } - - // //Get the continuation token. - // continuationToken = resultSegment.ContinuationToken; - //} - //while (continuationToken != null); + BlobContainerClient container = blobServiceClient.GetBlobContainerClient(_storageConfig.ThumbnailContainer); foreach (BlobItem blobItem in container.GetBlobs()) { - string thumbnailUrl = "https://" + _storageConfig.AccountName + "/.blob.core.windows.net/" + _storageConfig.ThumbnailContainer + "/" + blobItem.Name; - thumbnailUrls.Add(thumbnailUrl); + thumbnailUrls.Add(container.Uri + "/" + blobItem.Name); } return await Task.FromResult(thumbnailUrls); diff --git a/ImageResizeWebApp/ImageResizeWebApp/ImageResizeWebApp.csproj b/ImageResizeWebApp/ImageResizeWebApp/ImageResizeWebApp.csproj index cf53a42..364cb53 100644 --- a/ImageResizeWebApp/ImageResizeWebApp/ImageResizeWebApp.csproj +++ b/ImageResizeWebApp/ImageResizeWebApp/ImageResizeWebApp.csproj @@ -5,12 +5,11 @@ - netcoreapp2.1 + netcoreapp2.0 true Latest $(AssetTargetFallback);portable-net45+win8+wp8+wpa81; false - exe @@ -19,22 +18,16 @@ - - - - - - all - runtime; build; native; contentfiles; analyzers; buildtransitive - - - - all - runtime; build; native; contentfiles; analyzers; buildtransitive - - - - + + + + + + + + + + From 6f9d25003927b96a993c898f1c3e85fa31a5fe52 Mon Sep 17 00:00:00 2001 From: Mark Hopkins Date: Thu, 23 Jan 2020 14:03:41 -0800 Subject: [PATCH 5/8] Formatting --- ImageResizeWebApp/ImageResizeWebApp/Helpers/StorageHelper.cs | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/ImageResizeWebApp/ImageResizeWebApp/Helpers/StorageHelper.cs b/ImageResizeWebApp/ImageResizeWebApp/Helpers/StorageHelper.cs index aaedda3..fef9081 100644 --- a/ImageResizeWebApp/ImageResizeWebApp/Helpers/StorageHelper.cs +++ b/ImageResizeWebApp/ImageResizeWebApp/Helpers/StorageHelper.cs @@ -27,7 +27,8 @@ public static bool IsImage(IFormFile file) return formats.Any(item => file.FileName.EndsWith(item, StringComparison.OrdinalIgnoreCase)); } - public static async Task UploadFileToStorage(Stream fileStream, string fileName, AzureStorageConfig _storageConfig) + public static async Task UploadFileToStorage(Stream fileStream, string fileName, + AzureStorageConfig _storageConfig) { // Create a URI to the blob Uri blobUri = new Uri("https://" + From a3f1d05a75475473ff9dcb75c73fa7afcc7b8df9 Mon Sep 17 00:00:00 2001 From: Mark Hopkins Date: Thu, 23 Jan 2020 14:08:57 -0800 Subject: [PATCH 6/8] Removed unused member --- ImageResizeWebApp/ImageResizeWebApp/Models/AzureStorageConfig.cs | 1 - 1 file changed, 1 deletion(-) diff --git a/ImageResizeWebApp/ImageResizeWebApp/Models/AzureStorageConfig.cs b/ImageResizeWebApp/ImageResizeWebApp/Models/AzureStorageConfig.cs index a12f694..12192e4 100644 --- a/ImageResizeWebApp/ImageResizeWebApp/Models/AzureStorageConfig.cs +++ b/ImageResizeWebApp/ImageResizeWebApp/Models/AzureStorageConfig.cs @@ -4,7 +4,6 @@ public class AzureStorageConfig { public string AccountName { get; set; } public string AccountKey { get; set; } - public string QueueName { get; set; } public string ImageContainer { get; set; } public string ThumbnailContainer { get; set; } } From a7744be3ce2a20f4f76be9487f86548bcfcb814b Mon Sep 17 00:00:00 2001 From: Mark Hopkins Date: Thu, 27 Feb 2020 13:27:26 -0800 Subject: [PATCH 7/8] Updates to v12 --- .../Controllers/ImagesController.cs | 23 +++---------------- .../Helpers/StorageHelper.cs | 20 ++++++++++------ 2 files changed, 16 insertions(+), 27 deletions(-) diff --git a/ImageResizeWebApp/ImageResizeWebApp/Controllers/ImagesController.cs b/ImageResizeWebApp/ImageResizeWebApp/Controllers/ImagesController.cs index bc4ca3f..f835357 100644 --- a/ImageResizeWebApp/ImageResizeWebApp/Controllers/ImagesController.cs +++ b/ImageResizeWebApp/ImageResizeWebApp/Controllers/ImagesController.cs @@ -29,17 +29,13 @@ public async Task Upload(ICollection files) try { - if (files.Count == 0) - return BadRequest("No files received from the upload"); if (storageConfig.AccountKey == string.Empty || storageConfig.AccountName == string.Empty) - return BadRequest("sorry, can't retrieve your azure storage details from appsettings.js, make sure that you add azure storage details there"); if (storageConfig.ImageContainer == string.Empty) - return BadRequest("Please provide a name for your image container in the azure blob storage"); foreach (var formFile in files) @@ -63,18 +59,12 @@ public async Task Upload(ICollection files) if (isUploaded) { if (storageConfig.ThumbnailContainer != string.Empty) - return new AcceptedAtActionResult("GetThumbNails", "Images", null, null); - else - return new AcceptedResult(); } else - return BadRequest("Look like the image couldnt upload to the storage"); - - } catch (Exception ex) { @@ -86,28 +76,21 @@ public async Task Upload(ICollection files) [HttpGet("thumbnails")] public async Task GetThumbNails() { - try { if (storageConfig.AccountKey == string.Empty || storageConfig.AccountName == string.Empty) - - return BadRequest("sorry, can't retrieve your azure storage details from appsettings.js, make sure that you add azure storage details there"); + return BadRequest("Sorry, can't retrieve your Azure storage details from appsettings.js, make sure that you add Azure storage details there."); if (storageConfig.ImageContainer == string.Empty) - - return BadRequest("Please provide a name for your image container in the azure blob storage"); + return BadRequest("Please provide a name for your image container in Azure blob storage."); List thumbnailUrls = await StorageHelper.GetThumbNailUrls(storageConfig); - - return new ObjectResult(thumbnailUrls); - + return new ObjectResult(thumbnailUrls); } catch (Exception ex) { return BadRequest(ex.Message); } - } - } } \ No newline at end of file diff --git a/ImageResizeWebApp/ImageResizeWebApp/Helpers/StorageHelper.cs b/ImageResizeWebApp/ImageResizeWebApp/Helpers/StorageHelper.cs index fef9081..81cd759 100644 --- a/ImageResizeWebApp/ImageResizeWebApp/Helpers/StorageHelper.cs +++ b/ImageResizeWebApp/ImageResizeWebApp/Helpers/StorageHelper.cs @@ -1,7 +1,9 @@ using ImageResizeWebApp.Models; using Microsoft.AspNetCore.Http; +using Azure; using Azure.Storage; using Azure.Storage.Blobs; +using Azure.Storage.Sas; using System; using System.Collections.Generic; @@ -9,6 +11,7 @@ using System.Linq; using System.Threading.Tasks; using Azure.Storage.Blobs.Models; +using System.Diagnostics; namespace ImageResizeWebApp.Helpers { @@ -31,15 +34,15 @@ public static async Task UploadFileToStorage(Stream fileStream, string fil AzureStorageConfig _storageConfig) { // Create a URI to the blob - Uri blobUri = new Uri("https://" + - _storageConfig.AccountName + - ".blob.core.windows.net/" + - _storageConfig.ImageContainer + + Uri blobUri = new Uri("https://" + + _storageConfig.AccountName + + ".blob.core.windows.net/" + + _storageConfig.ImageContainer + "/" + fileName); // Create StorageSharedKeyCredentials object by reading // the values from the configuration (appsettings.json) - StorageSharedKeyCredential storageCredentials = + StorageSharedKeyCredential storageCredentials = new StorageSharedKeyCredential(_storageConfig.AccountName, _storageConfig.AccountKey); // Create the blob client. @@ -64,9 +67,12 @@ public static async Task> GetThumbNailUrls(AzureStorageConfig _stor // Get reference to the container BlobContainerClient container = blobServiceClient.GetBlobContainerClient(_storageConfig.ThumbnailContainer); - foreach (BlobItem blobItem in container.GetBlobs()) + if (container.Exists()) { - thumbnailUrls.Add(container.Uri + "/" + blobItem.Name); + foreach (BlobItem blobItem in container.GetBlobs()) + { + thumbnailUrls.Add(container.Uri + "/" + blobItem.Name); + } } return await Task.FromResult(thumbnailUrls); From 4f97c5b9b47a2aeb3c83b002ed4ef7bc6f6b2333 Mon Sep 17 00:00:00 2001 From: Mark Hopkins Date: Thu, 27 Feb 2020 13:41:17 -0800 Subject: [PATCH 8/8] Sorted and removed unnecessary assemblies --- .../ImageResizeWebApp/Helpers/StorageHelper.cs | 12 ++++-------- 1 file changed, 4 insertions(+), 8 deletions(-) diff --git a/ImageResizeWebApp/ImageResizeWebApp/Helpers/StorageHelper.cs b/ImageResizeWebApp/ImageResizeWebApp/Helpers/StorageHelper.cs index 81cd759..be4efbc 100644 --- a/ImageResizeWebApp/ImageResizeWebApp/Helpers/StorageHelper.cs +++ b/ImageResizeWebApp/ImageResizeWebApp/Helpers/StorageHelper.cs @@ -1,17 +1,13 @@ -using ImageResizeWebApp.Models; -using Microsoft.AspNetCore.Http; -using Azure; -using Azure.Storage; +using Azure.Storage; using Azure.Storage.Blobs; -using Azure.Storage.Sas; - +using Azure.Storage.Blobs.Models; +using ImageResizeWebApp.Models; +using Microsoft.AspNetCore.Http; using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Threading.Tasks; -using Azure.Storage.Blobs.Models; -using System.Diagnostics; namespace ImageResizeWebApp.Helpers {