Skip to content

Commit 69d6791

Browse files
LuyunmtTianyun-G
andauthored
Change storage blob track1 to track2 (#17)
* change storage track1 to track2 * Update packages version * Update code according comments Co-authored-by: Tianyun Gao <v-tianga@microsoft.com>
1 parent d16b7ae commit 69d6791

File tree

5 files changed

+123
-49
lines changed

5 files changed

+123
-49
lines changed

WebApp-Storage-DotNet/Controllers/HomeController.cs

Lines changed: 22 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -15,15 +15,13 @@ namespace WebApp_Storage_DotNet.Controllers
1515
{
1616
using System;
1717
using System.Collections.Generic;
18-
using System.Web.Mvc;
19-
using System.Web;
20-
using System.Threading.Tasks;
21-
using System.IO;
22-
using Microsoft.WindowsAzure;
23-
using Microsoft.WindowsAzure.Storage;
24-
using Microsoft.WindowsAzure.Storage.Blob;
25-
using Microsoft.Azure;
2618
using System.Configuration;
19+
using System.IO;
20+
using System.Threading.Tasks;
21+
using System.Web;
22+
using System.Web.Mvc;
23+
using Azure.Storage.Blobs;
24+
using Azure.Storage.Blobs.Models;
2725

2826
/// <summary>
2927
/// Azure Blob Storage Photo Gallery - Demonstrates how to use the Blob Storage service.
@@ -45,9 +43,8 @@ namespace WebApp_Storage_DotNet.Controllers
4543

4644
public class HomeController : Controller
4745
{
48-
static CloudBlobClient blobClient;
4946
const string blobContainerName = "webappstoragedotnet-imagecontainer";
50-
static CloudBlobContainer blobContainer;
47+
static BlobContainerClient blobContainer;
5148

5249
/// <summary>
5350
/// Task<ActionResult> Index()
@@ -63,25 +60,22 @@ public async Task<ActionResult> Index()
6360
{
6461
// Retrieve storage account information from connection string
6562
// How to create a storage connection string - http://msdn.microsoft.com/en-us/library/azure/ee758697.aspx
66-
CloudStorageAccount storageAccount = CloudStorageAccount.Parse(ConfigurationManager.AppSettings["StorageConnectionString"].ToString());
63+
BlobServiceClient blobServiceClient = new BlobServiceClient(ConfigurationManager.AppSettings["StorageConnectionString"].ToString());
6764

68-
// Create a blob client for interacting with the blob service.
69-
blobClient = storageAccount.CreateCloudBlobClient();
70-
blobContainer = blobClient.GetContainerReference(blobContainerName);
71-
await blobContainer.CreateIfNotExistsAsync();
65+
blobContainer = blobServiceClient.GetBlobContainerClient(blobContainerName);
66+
await blobContainer.CreateIfNotExistsAsync(PublicAccessType.Blob);
7267

7368
// 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
7469
// access to the resource. See the documentation links at the top for more information on SAS. The second approach is to set permissions
7570
// 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
7671
// using: https://[InsertYourStorageAccountNameHere].blob.core.windows.net/webappstoragedotnet-imagecontainer/FileName
77-
await blobContainer.SetPermissionsAsync(new BlobContainerPermissions { PublicAccess = BlobContainerPublicAccessType.Blob });
7872

79-
// Gets all Cloud Block Blobs in the blobContainerName and passes them to teh view
73+
// Gets all Block Blobs in the blobContainerName and passes them to the view
8074
List<Uri> allBlobs = new List<Uri>();
81-
foreach (IListBlobItem blob in blobContainer.ListBlobs())
75+
foreach (BlobItem blob in blobContainer.GetBlobs())
8276
{
83-
if (blob.GetType() == typeof(CloudBlockBlob))
84-
allBlobs.Add(blob.Uri);
77+
if (blob.Properties.BlobType == BlobType.Block)
78+
allBlobs.Add(blobContainer.GetBlobClient(blob.Name).Uri);
8579
}
8680

8781
return View(allBlobs);
@@ -91,7 +85,7 @@ public async Task<ActionResult> Index()
9185
ViewData["message"] = ex.Message;
9286
ViewData["trace"] = ex.StackTrace;
9387
return View("Error");
94-
}
88+
}
9589
}
9690

9791
/// <summary>
@@ -111,8 +105,8 @@ public async Task<ActionResult> UploadAsync()
111105
{
112106
for (int i = 0; i < fileCount; i++)
113107
{
114-
CloudBlockBlob blob = blobContainer.GetBlockBlobReference(GetRandomBlobName(files[i].FileName));
115-
await blob.UploadFromFileAsync(files[i].FileName, FileMode.Open);
108+
BlobClient blob = blobContainer.GetBlobClient(GetRandomBlobName(files[i].FileName));
109+
await blob.UploadAsync(files[i].FileName);
116110
}
117111
}
118112
return RedirectToAction("Index");
@@ -122,7 +116,7 @@ public async Task<ActionResult> UploadAsync()
122116
ViewData["message"] = ex.Message;
123117
ViewData["trace"] = ex.StackTrace;
124118
return View("Error");
125-
}
119+
}
126120
}
127121

128122
/// <summary>
@@ -138,7 +132,7 @@ public async Task<ActionResult> DeleteImage(string name)
138132
Uri uri = new Uri(name);
139133
string filename = Path.GetFileName(uri.LocalPath);
140134

141-
var blob = blobContainer.GetBlockBlobReference(filename);
135+
var blob = blobContainer.GetBlobClient(filename);
142136
await blob.DeleteIfExistsAsync();
143137

144138
return RedirectToAction("Index");
@@ -161,11 +155,11 @@ public async Task<ActionResult> DeleteAll()
161155
{
162156
try
163157
{
164-
foreach (var blob in blobContainer.ListBlobs())
158+
foreach (var blob in blobContainer.GetBlobs())
165159
{
166-
if (blob.GetType() == typeof(CloudBlockBlob))
160+
if (blob.Properties.BlobType == BlobType.Block)
167161
{
168-
await ((CloudBlockBlob)blob).DeleteIfExistsAsync();
162+
await blobContainer.DeleteBlobIfExistsAsync(blob.Name);
169163
}
170164
}
171165

WebApp-Storage-DotNet/Global.asax.cs

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,6 @@
55
using System.Web.Mvc;
66
using System.Web.Optimization;
77
using System.Web.Routing;
8-
using Microsoft.WindowsAzure;
9-
using Microsoft.WindowsAzure.Storage;
10-
using Microsoft.WindowsAzure.Storage.Blob;
118

129
namespace WebApp_Storage_DotNet
1310
{

WebApp-Storage-DotNet/Web.config

Lines changed: 39 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,22 @@
77
<appSettings>
88
<!-- Insert your storage account name and key in the line below. -->
99
<add key="StorageConnectionString" value="UseDevelopmentStorage=true" />
10-
1110
<add key="webpages:Version" value="3.0.0.0" />
1211
<add key="webpages:Enabled" value="false" />
1312
<add key="ClientValidationEnabled" value="true" />
1413
<add key="UnobtrusiveJavaScriptEnabled" value="true" />
1514
</appSettings>
15+
<!--
16+
For a description of web.config changes see http://go.microsoft.com/fwlink/?LinkId=235367.
17+
18+
The following attributes can be set on the <httpRuntime> tag.
19+
<system.Web>
20+
<httpRuntime targetFramework="4.8" />
21+
</system.Web>
22+
-->
1623
<system.web>
17-
<compilation debug="true" targetFramework="4.5.2" />
18-
<httpRuntime targetFramework="4.5.2" maxRequestLength="2100000000" executionTimeout="12000000"/>
24+
<compilation debug="true" targetFramework="4.8" />
25+
<httpRuntime targetFramework="4.5.2" maxRequestLength="2100000000" executionTimeout="12000000" />
1926
</system.web>
2027
<runtime>
2128
<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
@@ -43,6 +50,34 @@
4350
<assemblyIdentity name="System.Web.Mvc" publicKeyToken="31bf3856ad364e35" />
4451
<bindingRedirect oldVersion="1.0.0.0-5.2.3.0" newVersion="5.2.3.0" />
4552
</dependentAssembly>
53+
<dependentAssembly>
54+
<assemblyIdentity name="System.Numerics.Vectors" publicKeyToken="b03f5f7f11d50a3a" culture="neutral" />
55+
<bindingRedirect oldVersion="0.0.0.0-4.1.4.0" newVersion="4.1.4.0" />
56+
</dependentAssembly>
57+
<dependentAssembly>
58+
<assemblyIdentity name="System.Runtime.CompilerServices.Unsafe" publicKeyToken="b03f5f7f11d50a3a" culture="neutral" />
59+
<bindingRedirect oldVersion="0.0.0.0-4.0.5.0" newVersion="4.0.5.0" />
60+
</dependentAssembly>
61+
<dependentAssembly>
62+
<assemblyIdentity name="System.Buffers" publicKeyToken="cc7b13ffcd2ddd51" culture="neutral" />
63+
<bindingRedirect oldVersion="0.0.0.0-4.0.3.0" newVersion="4.0.3.0" />
64+
</dependentAssembly>
65+
<dependentAssembly>
66+
<assemblyIdentity name="System.Memory" publicKeyToken="cc7b13ffcd2ddd51" culture="neutral" />
67+
<bindingRedirect oldVersion="0.0.0.0-4.0.1.1" newVersion="4.0.1.1" />
68+
</dependentAssembly>
69+
<dependentAssembly>
70+
<assemblyIdentity name="System.ValueTuple" publicKeyToken="cc7b13ffcd2ddd51" culture="neutral" />
71+
<bindingRedirect oldVersion="0.0.0.0-4.0.3.0" newVersion="4.0.3.0" />
72+
</dependentAssembly>
73+
<dependentAssembly>
74+
<assemblyIdentity name="System.Text.Encodings.Web" publicKeyToken="cc7b13ffcd2ddd51" culture="neutral" />
75+
<bindingRedirect oldVersion="0.0.0.0-4.0.5.1" newVersion="4.0.5.1" />
76+
</dependentAssembly>
77+
<dependentAssembly>
78+
<assemblyIdentity name="Azure.Core" publicKeyToken="92742159e12e44c8" culture="neutral" />
79+
<bindingRedirect oldVersion="0.0.0.0-1.18.0.0" newVersion="1.18.0.0" />
80+
</dependentAssembly>
4681
</assemblyBinding>
4782
</runtime>
4883
<system.codedom>
@@ -51,4 +86,4 @@
5186
<compiler language="vb;vbs;visualbasic;vbscript" extension=".vb" type="Microsoft.CodeDom.Providers.DotNetCompilerPlatform.VBCodeProvider, Microsoft.CodeDom.Providers.DotNetCompilerPlatform, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" warningLevel="4" compilerOptions="/langversion:14 /nowarn:41008 /define:_MYTYPE=\&quot;Web\&quot; /optionInfer+" />
5287
</compilers>
5388
</system.codedom>
54-
</configuration>
89+
</configuration>

WebApp-Storage-DotNet/WebApp-Storage-DotNet.csproj

Lines changed: 48 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
<AppDesignerFolder>Properties</AppDesignerFolder>
1616
<RootNamespace>WebApp_Storage_DotNet</RootNamespace>
1717
<AssemblyName>WebApp-Storage-DotNet</AssemblyName>
18-
<TargetFrameworkVersion>v4.5.2</TargetFrameworkVersion>
18+
<TargetFrameworkVersion>v4.8</TargetFrameworkVersion>
1919
<MvcBuildViews>false</MvcBuildViews>
2020
<UseIISExpress>true</UseIISExpress>
2121
<IISExpressSSLPort />
@@ -25,6 +25,8 @@
2525
<UseGlobalApplicationHostFile />
2626
<NuGetPackageImportStamp>
2727
</NuGetPackageImportStamp>
28+
<Use64BitIISExpress />
29+
<TargetFrameworkProfile />
2830
</PropertyGroup>
2931
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
3032
<DebugSymbols>true</DebugSymbols>
@@ -44,9 +46,17 @@
4446
<WarningLevel>4</WarningLevel>
4547
</PropertyGroup>
4648
<ItemGroup>
47-
<Reference Include="Microsoft.Azure.KeyVault.Core, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
48-
<HintPath>..\packages\Microsoft.Azure.KeyVault.Core.1.0.0\lib\net40\Microsoft.Azure.KeyVault.Core.dll</HintPath>
49-
<Private>True</Private>
49+
<Reference Include="Azure.Core, Version=1.18.0.0, Culture=neutral, PublicKeyToken=92742159e12e44c8, processorArchitecture=MSIL">
50+
<HintPath>..\packages\Azure.Core.1.18.0\lib\net461\Azure.Core.dll</HintPath>
51+
</Reference>
52+
<Reference Include="Azure.Storage.Blobs, Version=12.9.1.0, Culture=neutral, PublicKeyToken=92742159e12e44c8, processorArchitecture=MSIL">
53+
<HintPath>..\packages\Azure.Storage.Blobs.12.9.1\lib\netstandard2.0\Azure.Storage.Blobs.dll</HintPath>
54+
</Reference>
55+
<Reference Include="Azure.Storage.Common, Version=12.8.0.0, Culture=neutral, PublicKeyToken=92742159e12e44c8, processorArchitecture=MSIL">
56+
<HintPath>..\packages\Azure.Storage.Common.12.8.0\lib\netstandard2.0\Azure.Storage.Common.dll</HintPath>
57+
</Reference>
58+
<Reference Include="Microsoft.Bcl.AsyncInterfaces, Version=1.0.0.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51, processorArchitecture=MSIL">
59+
<HintPath>..\packages\Microsoft.Bcl.AsyncInterfaces.1.0.0\lib\net461\Microsoft.Bcl.AsyncInterfaces.dll</HintPath>
5060
</Reference>
5161
<Reference Include="Microsoft.CodeDom.Providers.DotNetCompilerPlatform, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
5262
<HintPath>..\packages\Microsoft.CodeDom.Providers.DotNetCompilerPlatform.1.0.0\lib\net45\Microsoft.CodeDom.Providers.DotNetCompilerPlatform.dll</HintPath>
@@ -66,30 +76,55 @@
6676
<Private>True</Private>
6777
</Reference>
6878
<Reference Include="Microsoft.WindowsAzure.Configuration, Version=2.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL" />
69-
<Reference Include="Microsoft.WindowsAzure.Storage, Version=6.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
70-
<HintPath>..\packages\WindowsAzure.Storage.6.0.0\lib\net40\Microsoft.WindowsAzure.Storage.dll</HintPath>
71-
<Private>True</Private>
72-
</Reference>
7379
<Reference Include="Newtonsoft.Json, Version=6.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed, processorArchitecture=MSIL">
7480
<HintPath>..\packages\Newtonsoft.Json.6.0.8\lib\net45\Newtonsoft.Json.dll</HintPath>
7581
<Private>True</Private>
7682
</Reference>
7783
<Reference Include="System" />
84+
<Reference Include="System.Buffers, Version=4.0.3.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51, processorArchitecture=MSIL">
85+
<HintPath>..\packages\System.Buffers.4.5.1\lib\net461\System.Buffers.dll</HintPath>
86+
</Reference>
7887
<Reference Include="System.Data" />
88+
<Reference Include="System.Data.DataSetExtensions" />
89+
<Reference Include="System.Diagnostics.DiagnosticSource, Version=4.0.4.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51, processorArchitecture=MSIL">
90+
<HintPath>..\packages\System.Diagnostics.DiagnosticSource.4.6.0\lib\net46\System.Diagnostics.DiagnosticSource.dll</HintPath>
91+
</Reference>
7992
<Reference Include="System.Drawing" />
93+
<Reference Include="System.Memory, Version=4.0.1.1, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51, processorArchitecture=MSIL">
94+
<HintPath>..\packages\System.Memory.4.5.4\lib\net461\System.Memory.dll</HintPath>
95+
</Reference>
96+
<Reference Include="System.Memory.Data, Version=1.0.2.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51, processorArchitecture=MSIL">
97+
<HintPath>..\packages\System.Memory.Data.1.0.2\lib\net461\System.Memory.Data.dll</HintPath>
98+
</Reference>
99+
<Reference Include="System.Numerics" />
100+
<Reference Include="System.Numerics.Vectors, Version=4.1.4.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL">
101+
<HintPath>..\packages\System.Numerics.Vectors.4.5.0\lib\net46\System.Numerics.Vectors.dll</HintPath>
102+
</Reference>
103+
<Reference Include="System.Runtime.CompilerServices.Unsafe, Version=4.0.5.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL">
104+
<HintPath>..\packages\System.Runtime.CompilerServices.Unsafe.4.6.0\lib\netstandard2.0\System.Runtime.CompilerServices.Unsafe.dll</HintPath>
105+
</Reference>
80106
<Reference Include="System.Spatial, Version=5.6.4.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
81107
<HintPath>..\packages\System.Spatial.5.6.4\lib\net40\System.Spatial.dll</HintPath>
82108
<Private>True</Private>
83109
</Reference>
110+
<Reference Include="System.Text.Encodings.Web, Version=4.0.5.1, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51, processorArchitecture=MSIL">
111+
<HintPath>..\packages\System.Text.Encodings.Web.4.7.2\lib\net461\System.Text.Encodings.Web.dll</HintPath>
112+
</Reference>
113+
<Reference Include="System.Text.Json, Version=4.0.0.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51, processorArchitecture=MSIL">
114+
<HintPath>..\packages\System.Text.Json.4.6.0\lib\net461\System.Text.Json.dll</HintPath>
115+
</Reference>
116+
<Reference Include="System.Threading.Tasks.Extensions, Version=4.2.0.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51, processorArchitecture=MSIL">
117+
<HintPath>..\packages\System.Threading.Tasks.Extensions.4.5.2\lib\netstandard2.0\System.Threading.Tasks.Extensions.dll</HintPath>
118+
</Reference>
119+
<Reference Include="System.ValueTuple, Version=4.0.3.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51, processorArchitecture=MSIL">
120+
<HintPath>..\packages\System.ValueTuple.4.5.0\lib\net47\System.ValueTuple.dll</HintPath>
121+
</Reference>
84122
<Reference Include="System.Web.DynamicData" />
85123
<Reference Include="System.Web.Entity" />
86124
<Reference Include="System.Web.ApplicationServices" />
87125
<Reference Include="System.ComponentModel.DataAnnotations" />
88-
<Reference Include="System.Core" />
89-
<Reference Include="System.Data.DataSetExtensions" />
90-
<Reference Include="System.Xml.Linq" />
91-
<Reference Include="System.Web" />
92126
<Reference Include="System.Web.Extensions" />
127+
<Reference Include="System.Web" />
93128
<Reference Include="System.Web.Abstractions" />
94129
<Reference Include="System.Web.Routing" />
95130
<Reference Include="System.Xml" />
@@ -131,6 +166,7 @@
131166
<Private>True</Private>
132167
<HintPath>..\packages\Microsoft.AspNet.WebPages.3.2.3\lib\net45\System.Web.WebPages.Razor.dll</HintPath>
133168
</Reference>
169+
<Reference Include="System.Xml.Linq" />
134170
<Reference Include="WebGrease">
135171
<Private>True</Private>
136172
<HintPath>..\packages\WebGrease.1.5.2\lib\WebGrease.dll</HintPath>

0 commit comments

Comments
 (0)