diff --git a/WebApp-Storage-DotNet/Controllers/HomeController.cs b/WebApp-Storage-DotNet/Controllers/HomeController.cs index 9dd9ec5..9e7f6b1 100644 --- a/WebApp-Storage-DotNet/Controllers/HomeController.cs +++ b/WebApp-Storage-DotNet/Controllers/HomeController.cs @@ -15,15 +15,13 @@ namespace WebApp_Storage_DotNet.Controllers { using System; using System.Collections.Generic; - using System.Web.Mvc; - 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; + using System.IO; + using System.Threading.Tasks; + using System.Web; + using System.Web.Mvc; + using Azure.Storage.Blobs; + using Azure.Storage.Blobs.Models; /// /// Azure Blob Storage Photo Gallery - Demonstrates how to use the Blob Storage service. @@ -45,9 +43,8 @@ namespace WebApp_Storage_DotNet.Controllers public class HomeController : Controller { - static CloudBlobClient blobClient; const string blobContainerName = "webappstoragedotnet-imagecontainer"; - static CloudBlobContainer blobContainer; + static BlobContainerClient blobContainer; /// /// Task Index() @@ -63,25 +60,22 @@ 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()); + BlobServiceClient blobServiceClient = new BlobServiceClient(ConfigurationManager.AppSettings["StorageConnectionString"].ToString()); - // Create a blob client for interacting with the blob service. - blobClient = storageAccount.CreateCloudBlobClient(); - blobContainer = blobClient.GetContainerReference(blobContainerName); - await blobContainer.CreateIfNotExistsAsync(); + blobContainer = blobServiceClient.GetBlobContainerClient(blobContainerName); + await blobContainer.CreateIfNotExistsAsync(PublicAccessType.Blob); // To view the uploaded blob in a browser, you have two options. The first option is to use a Shared Access Signature (SAS) token to delegate // access to the resource. See the documentation links at the top for more information on SAS. The second approach is to set permissions // to allow public access to blobs in this container. Comment the line below to not use this approach and to use SAS. Then you can view the image // using: https://[InsertYourStorageAccountNameHere].blob.core.windows.net/webappstoragedotnet-imagecontainer/FileName - await blobContainer.SetPermissionsAsync(new BlobContainerPermissions { PublicAccess = BlobContainerPublicAccessType.Blob }); - // Gets all Cloud Block Blobs in the blobContainerName and passes them to teh view + // Gets all Block Blobs in the blobContainerName and passes them to the view List allBlobs = new List(); - foreach (IListBlobItem blob in blobContainer.ListBlobs()) + foreach (BlobItem blob in blobContainer.GetBlobs()) { - if (blob.GetType() == typeof(CloudBlockBlob)) - allBlobs.Add(blob.Uri); + if (blob.Properties.BlobType == BlobType.Block) + allBlobs.Add(blobContainer.GetBlobClient(blob.Name).Uri); } return View(allBlobs); @@ -91,7 +85,7 @@ public async Task Index() ViewData["message"] = ex.Message; ViewData["trace"] = ex.StackTrace; return View("Error"); - } + } } /// @@ -111,8 +105,8 @@ 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); + BlobClient blob = blobContainer.GetBlobClient(GetRandomBlobName(files[i].FileName)); + await blob.UploadAsync(files[i].FileName); } } return RedirectToAction("Index"); @@ -122,7 +116,7 @@ public async Task UploadAsync() ViewData["message"] = ex.Message; ViewData["trace"] = ex.StackTrace; return View("Error"); - } + } } /// @@ -138,7 +132,7 @@ public async Task DeleteImage(string name) Uri uri = new Uri(name); string filename = Path.GetFileName(uri.LocalPath); - var blob = blobContainer.GetBlockBlobReference(filename); + var blob = blobContainer.GetBlobClient(filename); await blob.DeleteIfExistsAsync(); return RedirectToAction("Index"); @@ -161,11 +155,11 @@ public async Task DeleteAll() { try { - foreach (var blob in blobContainer.ListBlobs()) + foreach (var blob in blobContainer.GetBlobs()) { - if (blob.GetType() == typeof(CloudBlockBlob)) + if (blob.Properties.BlobType == BlobType.Block) { - await ((CloudBlockBlob)blob).DeleteIfExistsAsync(); + await blobContainer.DeleteBlobIfExistsAsync(blob.Name); } } diff --git a/WebApp-Storage-DotNet/Global.asax.cs b/WebApp-Storage-DotNet/Global.asax.cs index 4750a35..e224720 100644 --- a/WebApp-Storage-DotNet/Global.asax.cs +++ b/WebApp-Storage-DotNet/Global.asax.cs @@ -5,9 +5,6 @@ using System.Web.Mvc; using System.Web.Optimization; using System.Web.Routing; -using Microsoft.WindowsAzure; -using Microsoft.WindowsAzure.Storage; -using Microsoft.WindowsAzure.Storage.Blob; namespace WebApp_Storage_DotNet { diff --git a/WebApp-Storage-DotNet/Web.config b/WebApp-Storage-DotNet/Web.config index fc3fecd..4ff6479 100644 --- a/WebApp-Storage-DotNet/Web.config +++ b/WebApp-Storage-DotNet/Web.config @@ -7,15 +7,22 @@ - + - - + + @@ -43,6 +50,34 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + @@ -51,4 +86,4 @@ - + \ No newline at end of file diff --git a/WebApp-Storage-DotNet/WebApp-Storage-DotNet.csproj b/WebApp-Storage-DotNet/WebApp-Storage-DotNet.csproj index f522f50..f672879 100644 --- a/WebApp-Storage-DotNet/WebApp-Storage-DotNet.csproj +++ b/WebApp-Storage-DotNet/WebApp-Storage-DotNet.csproj @@ -15,7 +15,7 @@ Properties WebApp_Storage_DotNet WebApp-Storage-DotNet - v4.5.2 + v4.8 false true @@ -25,6 +25,8 @@ + + true @@ -44,9 +46,17 @@ 4 - - ..\packages\Microsoft.Azure.KeyVault.Core.1.0.0\lib\net40\Microsoft.Azure.KeyVault.Core.dll - True + + ..\packages\Azure.Core.1.18.0\lib\net461\Azure.Core.dll + + + ..\packages\Azure.Storage.Blobs.12.9.1\lib\netstandard2.0\Azure.Storage.Blobs.dll + + + ..\packages\Azure.Storage.Common.12.8.0\lib\netstandard2.0\Azure.Storage.Common.dll + + + ..\packages\Microsoft.Bcl.AsyncInterfaces.1.0.0\lib\net461\Microsoft.Bcl.AsyncInterfaces.dll ..\packages\Microsoft.CodeDom.Providers.DotNetCompilerPlatform.1.0.0\lib\net45\Microsoft.CodeDom.Providers.DotNetCompilerPlatform.dll @@ -66,30 +76,55 @@ True - - ..\packages\WindowsAzure.Storage.6.0.0\lib\net40\Microsoft.WindowsAzure.Storage.dll - True - ..\packages\Newtonsoft.Json.6.0.8\lib\net45\Newtonsoft.Json.dll True + + ..\packages\System.Buffers.4.5.1\lib\net461\System.Buffers.dll + + + + ..\packages\System.Diagnostics.DiagnosticSource.4.6.0\lib\net46\System.Diagnostics.DiagnosticSource.dll + + + ..\packages\System.Memory.4.5.4\lib\net461\System.Memory.dll + + + ..\packages\System.Memory.Data.1.0.2\lib\net461\System.Memory.Data.dll + + + + ..\packages\System.Numerics.Vectors.4.5.0\lib\net46\System.Numerics.Vectors.dll + + + ..\packages\System.Runtime.CompilerServices.Unsafe.4.6.0\lib\netstandard2.0\System.Runtime.CompilerServices.Unsafe.dll + ..\packages\System.Spatial.5.6.4\lib\net40\System.Spatial.dll True + + ..\packages\System.Text.Encodings.Web.4.7.2\lib\net461\System.Text.Encodings.Web.dll + + + ..\packages\System.Text.Json.4.6.0\lib\net461\System.Text.Json.dll + + + ..\packages\System.Threading.Tasks.Extensions.4.5.2\lib\netstandard2.0\System.Threading.Tasks.Extensions.dll + + + ..\packages\System.ValueTuple.4.5.0\lib\net47\System.ValueTuple.dll + - - - - + @@ -131,6 +166,7 @@ True ..\packages\Microsoft.AspNet.WebPages.3.2.3\lib\net45\System.Web.WebPages.Razor.dll + True ..\packages\WebGrease.1.5.2\lib\WebGrease.dll diff --git a/WebApp-Storage-DotNet/packages.config b/WebApp-Storage-DotNet/packages.config index 751db9b..c373e95 100644 --- a/WebApp-Storage-DotNet/packages.config +++ b/WebApp-Storage-DotNet/packages.config @@ -1,6 +1,9 @@  + + + @@ -8,7 +11,7 @@ - + @@ -19,7 +22,16 @@ + + + + + + + + + + - \ No newline at end of file