From fe7a5abb39583759bfacfed2c54f5b34b06d97ab Mon Sep 17 00:00:00 2001 From: Luyunmt <2534523917@qq.com> Date: Wed, 26 Aug 2020 10:42:33 +0800 Subject: [PATCH 1/3] change storage track1 to track2 --- .../Controllers/HomeController.cs | 39 ++++++------- WebApp-Storage-DotNet/Global.asax.cs | 3 - WebApp-Storage-DotNet/Web.config | 35 ++++++++++-- .../WebApp-Storage-DotNet.csproj | 57 +++++++++++++++---- WebApp-Storage-DotNet/packages.config | 15 ++++- 5 files changed, 107 insertions(+), 42 deletions(-) diff --git a/WebApp-Storage-DotNet/Controllers/HomeController.cs b/WebApp-Storage-DotNet/Controllers/HomeController.cs index 9dd9ec5..ccd709a 100644 --- a/WebApp-Storage-DotNet/Controllers/HomeController.cs +++ b/WebApp-Storage-DotNet/Controllers/HomeController.cs @@ -19,11 +19,10 @@ 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; + using Azure.Storage.Blobs; + using Azure.Storage.Blobs.Models; + using Azure.Storage.Blobs.Specialized; /// /// Azure Blob Storage Photo Gallery - Demonstrates how to use the Blob Storage service. @@ -45,9 +44,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 +61,24 @@ 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); + blobContainer = blobServiceClient.GetBlobContainerClient(blobContainerName); await blobContainer.CreateIfNotExistsAsync(); // 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 }); + await blobContainer.SetAccessPolicyAsync(PublicAccessType.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); + string a = blobContainer.Uri.ToString(); + if (blob.Properties.BlobType == BlobType.Block) + allBlobs.Add(new Uri(blobContainer.Uri.ToString()+"/"+blob.Name)); } return View(allBlobs); @@ -111,8 +108,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"); @@ -138,7 +135,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 +158,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..4ffd047 100644 --- a/WebApp-Storage-DotNet/Web.config +++ b/WebApp-Storage-DotNet/Web.config @@ -7,15 +7,22 @@ - + - - + + @@ -43,6 +50,26 @@ + + + + + + + + + + + + + + + + + + + + @@ -51,4 +78,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..4209393 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.4.1\lib\netstandard2.0\Azure.Core.dll + + + ..\packages\Azure.Storage.Blobs.12.5.1\lib\netstandard2.0\Azure.Storage.Blobs.dll + + + ..\packages\Azure.Storage.Common.12.5.1\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,52 @@ 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.0\lib\netstandard2.0\System.Buffers.dll + + + + ..\packages\System.Diagnostics.DiagnosticSource.4.6.0\lib\net46\System.Diagnostics.DiagnosticSource.dll + + + ..\packages\System.Memory.4.5.3\lib\netstandard2.0\System.Memory.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.6.0\lib\netstandard2.0\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 +163,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..c9f0c92 100644 --- a/WebApp-Storage-DotNet/packages.config +++ b/WebApp-Storage-DotNet/packages.config @@ -1,6 +1,9 @@  + + + @@ -8,7 +11,7 @@ - + @@ -19,7 +22,15 @@ + + + + + + + + + - \ No newline at end of file From 44a2a899e8c322f7f8ac60da94d4f3e2cdec044d Mon Sep 17 00:00:00 2001 From: Tianyun Gao Date: Tue, 31 Aug 2021 11:12:09 +0800 Subject: [PATCH 2/3] Update packages version --- WebApp-Storage-DotNet/Web.config | 8 +++++++ .../WebApp-Storage-DotNet.csproj | 23 +++++++++++-------- WebApp-Storage-DotNet/packages.config | 13 ++++++----- 3 files changed, 28 insertions(+), 16 deletions(-) diff --git a/WebApp-Storage-DotNet/Web.config b/WebApp-Storage-DotNet/Web.config index 4ffd047..4ff6479 100644 --- a/WebApp-Storage-DotNet/Web.config +++ b/WebApp-Storage-DotNet/Web.config @@ -70,6 +70,14 @@ + + + + + + + + diff --git a/WebApp-Storage-DotNet/WebApp-Storage-DotNet.csproj b/WebApp-Storage-DotNet/WebApp-Storage-DotNet.csproj index 4209393..f672879 100644 --- a/WebApp-Storage-DotNet/WebApp-Storage-DotNet.csproj +++ b/WebApp-Storage-DotNet/WebApp-Storage-DotNet.csproj @@ -46,14 +46,14 @@ 4 - - ..\packages\Azure.Core.1.4.1\lib\netstandard2.0\Azure.Core.dll + + ..\packages\Azure.Core.1.18.0\lib\net461\Azure.Core.dll - - ..\packages\Azure.Storage.Blobs.12.5.1\lib\netstandard2.0\Azure.Storage.Blobs.dll + + ..\packages\Azure.Storage.Blobs.12.9.1\lib\netstandard2.0\Azure.Storage.Blobs.dll - - ..\packages\Azure.Storage.Common.12.5.1\lib\netstandard2.0\Azure.Storage.Common.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 @@ -82,7 +82,7 @@ - ..\packages\System.Buffers.4.5.0\lib\netstandard2.0\System.Buffers.dll + ..\packages\System.Buffers.4.5.1\lib\net461\System.Buffers.dll @@ -91,7 +91,10 @@ - ..\packages\System.Memory.4.5.3\lib\netstandard2.0\System.Memory.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 @@ -104,8 +107,8 @@ ..\packages\System.Spatial.5.6.4\lib\net40\System.Spatial.dll True - - ..\packages\System.Text.Encodings.Web.4.6.0\lib\netstandard2.0\System.Text.Encodings.Web.dll + + ..\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 diff --git a/WebApp-Storage-DotNet/packages.config b/WebApp-Storage-DotNet/packages.config index c9f0c92..c373e95 100644 --- a/WebApp-Storage-DotNet/packages.config +++ b/WebApp-Storage-DotNet/packages.config @@ -1,9 +1,9 @@  - - - + + + @@ -22,13 +22,14 @@ - + - + + - + From 622a14aa875fb890275c091a4e264ccbadca3234 Mon Sep 17 00:00:00 2001 From: Tianyun Gao Date: Thu, 2 Sep 2021 14:32:32 +0800 Subject: [PATCH 3/3] Update code according comments --- .../Controllers/HomeController.cs | 19 ++++++++----------- 1 file changed, 8 insertions(+), 11 deletions(-) diff --git a/WebApp-Storage-DotNet/Controllers/HomeController.cs b/WebApp-Storage-DotNet/Controllers/HomeController.cs index ccd709a..9e7f6b1 100644 --- a/WebApp-Storage-DotNet/Controllers/HomeController.cs +++ b/WebApp-Storage-DotNet/Controllers/HomeController.cs @@ -15,14 +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 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; - using Azure.Storage.Blobs.Specialized; /// /// Azure Blob Storage Photo Gallery - Demonstrates how to use the Blob Storage service. @@ -64,21 +63,19 @@ public async Task Index() BlobServiceClient blobServiceClient = new BlobServiceClient(ConfigurationManager.AppSettings["StorageConnectionString"].ToString()); blobContainer = blobServiceClient.GetBlobContainerClient(blobContainerName); - await blobContainer.CreateIfNotExistsAsync(); + 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.SetAccessPolicyAsync(PublicAccessType.Blob); // Gets all Block Blobs in the blobContainerName and passes them to the view List allBlobs = new List(); foreach (BlobItem blob in blobContainer.GetBlobs()) { - string a = blobContainer.Uri.ToString(); if (blob.Properties.BlobType == BlobType.Block) - allBlobs.Add(new Uri(blobContainer.Uri.ToString()+"/"+blob.Name)); + allBlobs.Add(blobContainer.GetBlobClient(blob.Name).Uri); } return View(allBlobs); @@ -88,7 +85,7 @@ public async Task Index() ViewData["message"] = ex.Message; ViewData["trace"] = ex.StackTrace; return View("Error"); - } + } } /// @@ -119,7 +116,7 @@ public async Task UploadAsync() ViewData["message"] = ex.Message; ViewData["trace"] = ex.StackTrace; return View("Error"); - } + } } ///