Skip to content

Commit 0561553

Browse files
committed
Updated to v12
1 parent 1ece46e commit 0561553

File tree

2 files changed

+48
-31
lines changed

2 files changed

+48
-31
lines changed

ImageResizeWebApp/ImageResizeWebApp/Helpers/StorageHelper.cs

Lines changed: 45 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
using ImageResizeWebApp.Models;
22
using Microsoft.AspNetCore.Http;
3-
using Microsoft.WindowsAzure.Storage;
4-
using Microsoft.WindowsAzure.Storage.Auth;
5-
using Microsoft.WindowsAzure.Storage.Blob;
3+
using Azure.Storage;
4+
using Azure.Storage.Blobs;
65

76
using System;
87
using System.Collections.Generic;
98
using System.IO;
109
using System.Linq;
1110
using System.Threading.Tasks;
11+
using Azure.Storage.Blobs.Models;
1212

1313
namespace ImageResizeWebApp.Helpers
1414
{
@@ -29,23 +29,29 @@ public static bool IsImage(IFormFile file)
2929

3030
public static async Task<bool> UploadFileToStorage(Stream fileStream, string fileName, AzureStorageConfig _storageConfig)
3131
{
32+
// Create a URI to the blob
33+
Uri uri = new Uri("https://" + _storageConfig.AccountName + "/.blob.core.windows.net/" + _storageConfig.ImageContainer + "/" + fileName);
34+
3235
// Create storagecredentials object by reading the values from the configuration (appsettings.json)
33-
StorageCredentials storageCredentials = new StorageCredentials(_storageConfig.AccountName, _storageConfig.AccountKey);
36+
StorageSharedKeyCredential storageCredentials = new StorageSharedKeyCredential(_storageConfig.AccountName, _storageConfig.AccountKey);
3437

3538
// Create cloudstorage account by passing the storagecredentials
36-
CloudStorageAccount storageAccount = new CloudStorageAccount(storageCredentials, true);
39+
//CloudStorageAccount storageAccount = new CloudStorageAccount(storageCredentials, true);
40+
//BlobServiceClient blobServiceClient = new BlobServiceClient(uri);
3741

3842
// Create the blob client.
39-
CloudBlobClient blobClient = storageAccount.CreateCloudBlobClient();
43+
BlobClient blobClient = new BlobClient(uri, storageCredentials);
4044

4145
// Get reference to the blob container by passing the name by reading the value from the configuration (appsettings.json)
42-
CloudBlobContainer container = blobClient.GetContainerReference(_storageConfig.ImageContainer);
46+
//CloudBlobContainer container = blobClient.GetContainerReference(_storageConfig.ImageContainer);
47+
//BlobContainerClient container = await blobServiceClient.CreateBlobContainerAsync(_storageConfig.ImageContainer);
4348

4449
// Get the reference to the block blob from the container
45-
CloudBlockBlob blockBlob = container.GetBlockBlobReference(fileName);
50+
//CloudBlockBlob blockBlob = container.GetBlockBlobReference(fileName);
4651

4752
// Upload the file
48-
await blockBlob.UploadFromStreamAsync(fileStream);
53+
//await blockBlob.UploadFromStreamAsync(fileStream);
54+
await blobClient.UploadAsync(fileStream);
4955

5056
return await Task.FromResult(true);
5157
}
@@ -54,41 +60,51 @@ public static async Task<List<string>> GetThumbNailUrls(AzureStorageConfig _stor
5460
{
5561
List<string> thumbnailUrls = new List<string>();
5662

63+
// Create a URI to the thumbnail container
64+
Uri uri = new Uri("https://" + _storageConfig.AccountName + "/.blob.core.windows.net/");
65+
5766
// Create storagecredentials object by reading the values from the configuration (appsettings.json)
58-
StorageCredentials storageCredentials = new StorageCredentials(_storageConfig.AccountName, _storageConfig.AccountKey);
67+
//StorageSharedKeyCredential storageCredentials = new StorageSharedKeyCredential(_storageConfig.AccountName, _storageConfig.AccountKey);
5968

6069
// Create cloudstorage account by passing the storagecredentials
61-
CloudStorageAccount storageAccount = new CloudStorageAccount(storageCredentials, true);
70+
//CloudStorageAccount storageAccount = new CloudStorageAccount(storageCredentials, true);
71+
BlobServiceClient blobServiceClient = new BlobServiceClient(uri);
6272

6373
// Create blob client
64-
CloudBlobClient blobClient = storageAccount.CreateCloudBlobClient();
74+
//BlobClient blobClient = new BlobClient(uri, storageCredentials);
6575

6676
// Get reference to the container
67-
CloudBlobContainer container = blobClient.GetContainerReference(_storageConfig.ThumbnailContainer);
77+
//CloudBlobContainer container = blobClient.GetContainerReference(_storageConfig.ThumbnailContainer);
78+
BlobContainerClient container = await blobServiceClient.CreateBlobContainerAsync(_storageConfig.ThumbnailContainer);
6879

69-
BlobContinuationToken continuationToken = null;
80+
//BlobContinuationToken continuationToken = null;
7081

71-
BlobResultSegment resultSegment = null;
82+
//BlobResultSegment resultSegment = null;
7283

7384
//Call ListBlobsSegmentedAsync and enumerate the result segment returned, while the continuation token is non-null.
7485
//When the continuation token is null, the last page has been returned and execution can exit the loop.
75-
do
86+
//do
87+
//{
88+
// //This overload allows control of the page size. You can return all remaining results by passing null for the maxResults parameter,
89+
// //or by calling a different overload.
90+
// resultSegment = await container.ListBlobsSegmentedAsync("", true, BlobListingDetails.All, 10, continuationToken, null, null);
91+
92+
// foreach (var blobItem in resultSegment.Results)
93+
// {
94+
// thumbnailUrls.Add(blobItem.StorageUri.PrimaryUri.ToString());
95+
// }
96+
97+
// //Get the continuation token.
98+
// continuationToken = resultSegment.ContinuationToken;
99+
//}
100+
//while (continuationToken != null);
101+
102+
foreach (BlobItem blobItem in container.GetBlobs())
76103
{
77-
//This overload allows control of the page size. You can return all remaining results by passing null for the maxResults parameter,
78-
//or by calling a different overload.
79-
resultSegment = await container.ListBlobsSegmentedAsync("", true, BlobListingDetails.All, 10, continuationToken, null, null);
80-
81-
foreach (var blobItem in resultSegment.Results)
82-
{
83-
thumbnailUrls.Add(blobItem.StorageUri.PrimaryUri.ToString());
84-
}
85-
86-
//Get the continuation token.
87-
continuationToken = resultSegment.ContinuationToken;
104+
string thumbnailUrl = "https://" + _storageConfig.AccountName + "/.blob.core.windows.net/" + _storageConfig.ThumbnailContainer + "/" + blobItem.Name;
105+
thumbnailUrls.Add(thumbnailUrl);
88106
}
89107

90-
while (continuationToken != null);
91-
92108
return await Task.FromResult(thumbnailUrls);
93109
}
94110
}

ImageResizeWebApp/ImageResizeWebApp/ImageResizeWebApp.csproj

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
<Project Sdk="Microsoft.NET.Sdk.Web">
1+
<Project Sdk="Microsoft.NET.Sdk.Web">
22

33
<PropertyGroup Label="Configuration" Condition="'$(Configuration)|$(Platform)'=='Debug|AnyCPU'">
44
<OutputType>Exe</OutputType>
@@ -17,6 +17,8 @@
1717
</PropertyGroup>
1818

1919
<ItemGroup>
20+
<PackageReference Include="Azure.Storage.Blobs" Version="12.2.0" />
21+
<PackageReference Include="Azure.Storage.Common" Version="12.1.1" />
2022
<PackageReference Include="Microsoft.AspNetCore" Version="2.2.0" />
2123
<PackageReference Include="Microsoft.AspNetCore.All" Version="2.2.8" />
2224
<PackageReference Include="Microsoft.AspNetCore.Mvc" Version="2.2.0" />
@@ -33,7 +35,6 @@
3335
<PackageReference Include="Microsoft.Extensions.Logging.Debug" Version="3.1.1" />
3436
<PackageReference Include="Microsoft.VisualStudio.Web.BrowserLink" Version="2.2.0" />
3537
<PackageReference Include="Microsoft.VisualStudio.Web.CodeGeneration.Design" Version="3.1.0" />
36-
<PackageReference Include="WindowsAzure.Storage" Version="9.3.3" />
3738
</ItemGroup>
3839

3940
<ItemGroup>

0 commit comments

Comments
 (0)