Skip to content

Commit 892a64f

Browse files
Merge pull request #31 from mhopkins-msft/master
Updated to Azure Storage v12 libraries
2 parents 3553fb6 + 4f97c5b commit 892a64f

File tree

7 files changed

+43
-107
lines changed

7 files changed

+43
-107
lines changed

ImageResizeWebApp/ImageResizeWebApp/Controllers/HomeController.cs

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,5 @@
1-
using System;
2-
using System.Collections.Generic;
3-
using System.Diagnostics;
4-
using System.Linq;
5-
using System.Threading.Tasks;
61
using Microsoft.AspNetCore.Mvc;
2+
using System.Diagnostics;
73

84
namespace ImageResizeWebApp.Controllers
95
{

ImageResizeWebApp/ImageResizeWebApp/Controllers/ImagesController.cs

Lines changed: 9 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,12 @@
1-
using System;
2-
using System.Collections.Generic;
3-
using System.Linq;
4-
using System.Threading.Tasks;
5-
using Microsoft.AspNetCore.Mvc;
1+
using ImageResizeWebApp.Helpers;
62
using ImageResizeWebApp.Models;
3+
using Microsoft.AspNetCore.Http;
4+
using Microsoft.AspNetCore.Mvc;
75
using Microsoft.Extensions.Options;
8-
6+
using System;
7+
using System.Collections.Generic;
98
using System.IO;
10-
using Microsoft.Azure;
11-
using Microsoft.WindowsAzure.Storage;
12-
using Microsoft.WindowsAzure.Storage.Blob;
13-
using Microsoft.WindowsAzure.Storage.Auth;
14-
using System.Net.Http;
15-
using Microsoft.AspNetCore.Http;
16-
using ImageResizeWebApp.Helpers;
9+
using System.Threading.Tasks;
1710

1811
namespace ImageResizeWebApp.Controllers
1912
{
@@ -36,17 +29,13 @@ public async Task<IActionResult> Upload(ICollection<IFormFile> files)
3629

3730
try
3831
{
39-
4032
if (files.Count == 0)
41-
4233
return BadRequest("No files received from the upload");
4334

4435
if (storageConfig.AccountKey == string.Empty || storageConfig.AccountName == string.Empty)
45-
4636
return BadRequest("sorry, can't retrieve your azure storage details from appsettings.js, make sure that you add azure storage details there");
4737

4838
if (storageConfig.ImageContainer == string.Empty)
49-
5039
return BadRequest("Please provide a name for your image container in the azure blob storage");
5140

5241
foreach (var formFile in files)
@@ -70,18 +59,12 @@ public async Task<IActionResult> Upload(ICollection<IFormFile> files)
7059
if (isUploaded)
7160
{
7261
if (storageConfig.ThumbnailContainer != string.Empty)
73-
7462
return new AcceptedAtActionResult("GetThumbNails", "Images", null, null);
75-
7663
else
77-
7864
return new AcceptedResult();
7965
}
8066
else
81-
8267
return BadRequest("Look like the image couldnt upload to the storage");
83-
84-
8568
}
8669
catch (Exception ex)
8770
{
@@ -93,28 +76,21 @@ public async Task<IActionResult> Upload(ICollection<IFormFile> files)
9376
[HttpGet("thumbnails")]
9477
public async Task<IActionResult> GetThumbNails()
9578
{
96-
9779
try
9880
{
9981
if (storageConfig.AccountKey == string.Empty || storageConfig.AccountName == string.Empty)
100-
101-
return BadRequest("sorry, can't retrieve your azure storage details from appsettings.js, make sure that you add azure storage details there");
82+
return BadRequest("Sorry, can't retrieve your Azure storage details from appsettings.js, make sure that you add Azure storage details there.");
10283

10384
if (storageConfig.ImageContainer == string.Empty)
104-
105-
return BadRequest("Please provide a name for your image container in the azure blob storage");
85+
return BadRequest("Please provide a name for your image container in Azure blob storage.");
10686

10787
List<string> thumbnailUrls = await StorageHelper.GetThumbNailUrls(storageConfig);
108-
109-
return new ObjectResult(thumbnailUrls);
110-
88+
return new ObjectResult(thumbnailUrls);
11189
}
11290
catch (Exception ex)
11391
{
11492
return BadRequest(ex.Message);
11593
}
116-
11794
}
118-
11995
}
12096
}

ImageResizeWebApp/ImageResizeWebApp/Helpers/StorageHelper.cs

Lines changed: 27 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
1-
using ImageResizeWebApp.Models;
1+
using Azure.Storage;
2+
using Azure.Storage.Blobs;
3+
using Azure.Storage.Blobs.Models;
4+
using ImageResizeWebApp.Models;
25
using Microsoft.AspNetCore.Http;
3-
using Microsoft.WindowsAzure.Storage;
4-
using Microsoft.WindowsAzure.Storage.Auth;
5-
using Microsoft.WindowsAzure.Storage.Blob;
6-
76
using System;
87
using System.Collections.Generic;
98
using System.IO;
@@ -27,25 +26,26 @@ public static bool IsImage(IFormFile file)
2726
return formats.Any(item => file.FileName.EndsWith(item, StringComparison.OrdinalIgnoreCase));
2827
}
2928

30-
public static async Task<bool> UploadFileToStorage(Stream fileStream, string fileName, AzureStorageConfig _storageConfig)
29+
public static async Task<bool> UploadFileToStorage(Stream fileStream, string fileName,
30+
AzureStorageConfig _storageConfig)
3131
{
32-
// Create storagecredentials object by reading the values from the configuration (appsettings.json)
33-
StorageCredentials storageCredentials = new StorageCredentials(_storageConfig.AccountName, _storageConfig.AccountKey);
34-
35-
// Create cloudstorage account by passing the storagecredentials
36-
CloudStorageAccount storageAccount = new CloudStorageAccount(storageCredentials, true);
32+
// Create a URI to the blob
33+
Uri blobUri = new Uri("https://" +
34+
_storageConfig.AccountName +
35+
".blob.core.windows.net/" +
36+
_storageConfig.ImageContainer +
37+
"/" + fileName);
38+
39+
// Create StorageSharedKeyCredentials object by reading
40+
// the values from the configuration (appsettings.json)
41+
StorageSharedKeyCredential storageCredentials =
42+
new StorageSharedKeyCredential(_storageConfig.AccountName, _storageConfig.AccountKey);
3743

3844
// Create the blob client.
39-
CloudBlobClient blobClient = storageAccount.CreateCloudBlobClient();
40-
41-
// 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);
43-
44-
// Get the reference to the block blob from the container
45-
CloudBlockBlob blockBlob = container.GetBlockBlobReference(fileName);
45+
BlobClient blobClient = new BlobClient(blobUri, storageCredentials);
4646

4747
// Upload the file
48-
await blockBlob.UploadFromStreamAsync(fileStream);
48+
await blobClient.UploadAsync(fileStream);
4949

5050
return await Task.FromResult(true);
5151
}
@@ -54,41 +54,23 @@ public static async Task<List<string>> GetThumbNailUrls(AzureStorageConfig _stor
5454
{
5555
List<string> thumbnailUrls = new List<string>();
5656

57-
// Create storagecredentials object by reading the values from the configuration (appsettings.json)
58-
StorageCredentials storageCredentials = new StorageCredentials(_storageConfig.AccountName, _storageConfig.AccountKey);
59-
60-
// Create cloudstorage account by passing the storagecredentials
61-
CloudStorageAccount storageAccount = new CloudStorageAccount(storageCredentials, true);
57+
// Create a URI to the storage account
58+
Uri accountUri = new Uri("https://" + _storageConfig.AccountName + ".blob.core.windows.net/");
6259

63-
// Create blob client
64-
CloudBlobClient blobClient = storageAccount.CreateCloudBlobClient();
60+
// Create BlobServiceClient from the account URI
61+
BlobServiceClient blobServiceClient = new BlobServiceClient(accountUri);
6562

6663
// Get reference to the container
67-
CloudBlobContainer container = blobClient.GetContainerReference(_storageConfig.ThumbnailContainer);
64+
BlobContainerClient container = blobServiceClient.GetBlobContainerClient(_storageConfig.ThumbnailContainer);
6865

69-
BlobContinuationToken continuationToken = null;
70-
71-
BlobResultSegment resultSegment = null;
72-
73-
//Call ListBlobsSegmentedAsync and enumerate the result segment returned, while the continuation token is non-null.
74-
//When the continuation token is null, the last page has been returned and execution can exit the loop.
75-
do
66+
if (container.Exists())
7667
{
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)
68+
foreach (BlobItem blobItem in container.GetBlobs())
8269
{
83-
thumbnailUrls.Add(blobItem.StorageUri.PrimaryUri.ToString());
70+
thumbnailUrls.Add(container.Uri + "/" + blobItem.Name);
8471
}
85-
86-
//Get the continuation token.
87-
continuationToken = resultSegment.ContinuationToken;
8872
}
8973

90-
while (continuationToken != null);
91-
9274
return await Task.FromResult(thumbnailUrls);
9375
}
9476
}

ImageResizeWebApp/ImageResizeWebApp/ImageResizeWebApp.csproj

Lines changed: 4 additions & 4 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>
@@ -10,13 +10,14 @@
1010
<TypeScriptToolsVersion>Latest</TypeScriptToolsVersion>
1111
<AssetTargetFallback>$(AssetTargetFallback);portable-net45+win8+wp8+wpa81;</AssetTargetFallback>
1212
<IsPackable>false</IsPackable>
13-
<!-- <PackageTargetFallback>portable-net45+win8</PackageTargetFallback> -->
1413
<ApplicationIcon />
1514
<OutputTypeEx>exe</OutputTypeEx>
16-
<StartupObject />
15+
<StartupObject></StartupObject>
1716
</PropertyGroup>
1817

1918
<ItemGroup>
19+
<PackageReference Include="Azure.Storage.Blobs" Version="12.2.0" />
20+
<PackageReference Include="Azure.Storage.Common" Version="12.1.1" />
2021
<PackageReference Include="Microsoft.AspNetCore" Version="2.0.0" />
2122
<PackageReference Include="Microsoft.AspNetCore.All" Version="2.0.0" />
2223
<PackageReference Include="Microsoft.AspNetCore.Mvc" Version="2.0.0" />
@@ -27,7 +28,6 @@
2728
<PackageReference Include="Microsoft.Extensions.Logging.Debug" Version="2.0.0" />
2829
<PackageReference Include="Microsoft.VisualStudio.Web.BrowserLink" Version="2.0.0" />
2930
<PackageReference Include="Microsoft.VisualStudio.Web.CodeGeneration.Design" Version="2.0.0" />
30-
<PackageReference Include="WindowsAzure.Storage" Version="8.2.1" />
3131
</ItemGroup>
3232

3333
<ItemGroup>

ImageResizeWebApp/ImageResizeWebApp/Models/AzureStorageConfig.cs

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,9 @@
1-
using System;
2-
using System.Collections.Generic;
3-
using System.Linq;
4-
using System.Threading.Tasks;
5-
6-
namespace ImageResizeWebApp.Models
1+
namespace ImageResizeWebApp.Models
72
{
83
public class AzureStorageConfig
94
{
105
public string AccountName { get; set; }
116
public string AccountKey { get; set; }
12-
public string QueueName { get; set; }
137
public string ImageContainer { get; set; }
148
public string ThumbnailContainer { get; set; }
159
}

ImageResizeWebApp/ImageResizeWebApp/Program.cs

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,5 @@
1-
using System;
2-
using System.Collections.Generic;
3-
using System.IO;
4-
using System.Linq;
5-
using System.Threading.Tasks;
61
using Microsoft.AspNetCore;
72
using Microsoft.AspNetCore.Hosting;
8-
using Microsoft.Extensions.Configuration;
9-
using Microsoft.Extensions.Logging;
103

114
namespace ImageResizeWebApp
125
{

ImageResizeWebApp/ImageResizeWebApp/Startup.cs

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,8 @@
1-
using System;
2-
using System.Collections.Generic;
3-
using System.Linq;
4-
using System.Threading.Tasks;
1+
using ImageResizeWebApp.Models;
52
using Microsoft.AspNetCore.Builder;
63
using Microsoft.AspNetCore.Hosting;
7-
using Microsoft.AspNetCore.SpaServices.Webpack;
84
using Microsoft.Extensions.Configuration;
95
using Microsoft.Extensions.DependencyInjection;
10-
using ImageResizeWebApp.Models;
116

127
namespace ImageResizeWebApp
138
{

0 commit comments

Comments
 (0)