Skip to content

Commit

Permalink
Merge pull request #31 from mhopkins-msft/master
Browse files Browse the repository at this point in the history
Updated to Azure Storage v12 libraries
  • Loading branch information
mhopkins-msft authored Mar 5, 2020
2 parents 3553fb6 + 4f97c5b commit 892a64f
Show file tree
Hide file tree
Showing 7 changed files with 43 additions and 107 deletions.
Original file line number Diff line number Diff line change
@@ -1,9 +1,5 @@
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using System.Diagnostics;

namespace ImageResizeWebApp.Controllers
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,19 +1,12 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using ImageResizeWebApp.Helpers;
using ImageResizeWebApp.Models;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Options;

using System;
using System.Collections.Generic;
using System.IO;
using Microsoft.Azure;
using Microsoft.WindowsAzure.Storage;
using Microsoft.WindowsAzure.Storage.Blob;
using Microsoft.WindowsAzure.Storage.Auth;
using System.Net.Http;
using Microsoft.AspNetCore.Http;
using ImageResizeWebApp.Helpers;
using System.Threading.Tasks;

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

try
{

if (files.Count == 0)

return BadRequest("No files received from the upload");

if (storageConfig.AccountKey == string.Empty || storageConfig.AccountName == string.Empty)

return BadRequest("sorry, can't retrieve your azure storage details from appsettings.js, make sure that you add azure storage details there");

if (storageConfig.ImageContainer == string.Empty)

return BadRequest("Please provide a name for your image container in the azure blob storage");

foreach (var formFile in files)
Expand All @@ -70,18 +59,12 @@ public async Task<IActionResult> Upload(ICollection<IFormFile> files)
if (isUploaded)
{
if (storageConfig.ThumbnailContainer != string.Empty)

return new AcceptedAtActionResult("GetThumbNails", "Images", null, null);

else

return new AcceptedResult();
}
else

return BadRequest("Look like the image couldnt upload to the storage");


}
catch (Exception ex)
{
Expand All @@ -93,28 +76,21 @@ public async Task<IActionResult> Upload(ICollection<IFormFile> files)
[HttpGet("thumbnails")]
public async Task<IActionResult> GetThumbNails()
{

try
{
if (storageConfig.AccountKey == string.Empty || storageConfig.AccountName == string.Empty)

return BadRequest("sorry, can't retrieve your azure storage details from appsettings.js, make sure that you add azure storage details there");
return BadRequest("Sorry, can't retrieve your Azure storage details from appsettings.js, make sure that you add Azure storage details there.");

if (storageConfig.ImageContainer == string.Empty)

return BadRequest("Please provide a name for your image container in the azure blob storage");
return BadRequest("Please provide a name for your image container in Azure blob storage.");

List<string> thumbnailUrls = await StorageHelper.GetThumbNailUrls(storageConfig);

return new ObjectResult(thumbnailUrls);

return new ObjectResult(thumbnailUrls);
}
catch (Exception ex)
{
return BadRequest(ex.Message);
}

}

}
}
72 changes: 27 additions & 45 deletions ImageResizeWebApp/ImageResizeWebApp/Helpers/StorageHelper.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
using ImageResizeWebApp.Models;
using Azure.Storage;
using Azure.Storage.Blobs;
using Azure.Storage.Blobs.Models;
using ImageResizeWebApp.Models;
using Microsoft.AspNetCore.Http;
using Microsoft.WindowsAzure.Storage;
using Microsoft.WindowsAzure.Storage.Auth;
using Microsoft.WindowsAzure.Storage.Blob;

using System;
using System.Collections.Generic;
using System.IO;
Expand All @@ -27,25 +26,26 @@ public static bool IsImage(IFormFile file)
return formats.Any(item => file.FileName.EndsWith(item, StringComparison.OrdinalIgnoreCase));
}

public static async Task<bool> UploadFileToStorage(Stream fileStream, string fileName, AzureStorageConfig _storageConfig)
public static async Task<bool> UploadFileToStorage(Stream fileStream, string fileName,
AzureStorageConfig _storageConfig)
{
// Create storagecredentials object by reading the values from the configuration (appsettings.json)
StorageCredentials storageCredentials = new StorageCredentials(_storageConfig.AccountName, _storageConfig.AccountKey);

// Create cloudstorage account by passing the storagecredentials
CloudStorageAccount storageAccount = new CloudStorageAccount(storageCredentials, true);
// Create a URI to the blob
Uri blobUri = new Uri("https://" +
_storageConfig.AccountName +
".blob.core.windows.net/" +
_storageConfig.ImageContainer +
"/" + fileName);

// Create StorageSharedKeyCredentials object by reading
// the values from the configuration (appsettings.json)
StorageSharedKeyCredential storageCredentials =
new StorageSharedKeyCredential(_storageConfig.AccountName, _storageConfig.AccountKey);

// Create the blob client.
CloudBlobClient blobClient = storageAccount.CreateCloudBlobClient();

// Get reference to the blob container by passing the name by reading the value from the configuration (appsettings.json)
CloudBlobContainer container = blobClient.GetContainerReference(_storageConfig.ImageContainer);

// Get the reference to the block blob from the container
CloudBlockBlob blockBlob = container.GetBlockBlobReference(fileName);
BlobClient blobClient = new BlobClient(blobUri, storageCredentials);

// Upload the file
await blockBlob.UploadFromStreamAsync(fileStream);
await blobClient.UploadAsync(fileStream);

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

// Create storagecredentials object by reading the values from the configuration (appsettings.json)
StorageCredentials storageCredentials = new StorageCredentials(_storageConfig.AccountName, _storageConfig.AccountKey);

// Create cloudstorage account by passing the storagecredentials
CloudStorageAccount storageAccount = new CloudStorageAccount(storageCredentials, true);
// Create a URI to the storage account
Uri accountUri = new Uri("https://" + _storageConfig.AccountName + ".blob.core.windows.net/");

// Create blob client
CloudBlobClient blobClient = storageAccount.CreateCloudBlobClient();
// Create BlobServiceClient from the account URI
BlobServiceClient blobServiceClient = new BlobServiceClient(accountUri);

// Get reference to the container
CloudBlobContainer container = blobClient.GetContainerReference(_storageConfig.ThumbnailContainer);
BlobContainerClient container = blobServiceClient.GetBlobContainerClient(_storageConfig.ThumbnailContainer);

BlobContinuationToken continuationToken = null;

BlobResultSegment resultSegment = null;

//Call ListBlobsSegmentedAsync and enumerate the result segment returned, while the continuation token is non-null.
//When the continuation token is null, the last page has been returned and execution can exit the loop.
do
if (container.Exists())
{
//This overload allows control of the page size. You can return all remaining results by passing null for the maxResults parameter,
//or by calling a different overload.
resultSegment = await container.ListBlobsSegmentedAsync("", true, BlobListingDetails.All, 10, continuationToken, null, null);

foreach (var blobItem in resultSegment.Results)
foreach (BlobItem blobItem in container.GetBlobs())
{
thumbnailUrls.Add(blobItem.StorageUri.PrimaryUri.ToString());
thumbnailUrls.Add(container.Uri + "/" + blobItem.Name);
}

//Get the continuation token.
continuationToken = resultSegment.ContinuationToken;
}

while (continuationToken != null);

return await Task.FromResult(thumbnailUrls);
}
}
Expand Down
8 changes: 4 additions & 4 deletions ImageResizeWebApp/ImageResizeWebApp/ImageResizeWebApp.csproj
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<Project Sdk="Microsoft.NET.Sdk.Web">
<Project Sdk="Microsoft.NET.Sdk.Web">

<PropertyGroup Label="Configuration" Condition="'$(Configuration)|$(Platform)'=='Debug|AnyCPU'">
<OutputType>Exe</OutputType>
Expand All @@ -10,13 +10,14 @@
<TypeScriptToolsVersion>Latest</TypeScriptToolsVersion>
<AssetTargetFallback>$(AssetTargetFallback);portable-net45+win8+wp8+wpa81;</AssetTargetFallback>
<IsPackable>false</IsPackable>
<!-- <PackageTargetFallback>portable-net45+win8</PackageTargetFallback> -->
<ApplicationIcon />
<OutputTypeEx>exe</OutputTypeEx>
<StartupObject />
<StartupObject></StartupObject>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Azure.Storage.Blobs" Version="12.2.0" />
<PackageReference Include="Azure.Storage.Common" Version="12.1.1" />
<PackageReference Include="Microsoft.AspNetCore" Version="2.0.0" />
<PackageReference Include="Microsoft.AspNetCore.All" Version="2.0.0" />
<PackageReference Include="Microsoft.AspNetCore.Mvc" Version="2.0.0" />
Expand All @@ -27,7 +28,6 @@
<PackageReference Include="Microsoft.Extensions.Logging.Debug" Version="2.0.0" />
<PackageReference Include="Microsoft.VisualStudio.Web.BrowserLink" Version="2.0.0" />
<PackageReference Include="Microsoft.VisualStudio.Web.CodeGeneration.Design" Version="2.0.0" />
<PackageReference Include="WindowsAzure.Storage" Version="8.2.1" />
</ItemGroup>

<ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,9 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace ImageResizeWebApp.Models
namespace ImageResizeWebApp.Models
{
public class AzureStorageConfig
{
public string AccountName { get; set; }
public string AccountKey { get; set; }
public string QueueName { get; set; }
public string ImageContainer { get; set; }
public string ThumbnailContainer { get; set; }
}
Expand Down
7 changes: 0 additions & 7 deletions ImageResizeWebApp/ImageResizeWebApp/Program.cs
Original file line number Diff line number Diff line change
@@ -1,12 +1,5 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Logging;

namespace ImageResizeWebApp
{
Expand Down
7 changes: 1 addition & 6 deletions ImageResizeWebApp/ImageResizeWebApp/Startup.cs
Original file line number Diff line number Diff line change
@@ -1,13 +1,8 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using ImageResizeWebApp.Models;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.SpaServices.Webpack;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using ImageResizeWebApp.Models;

namespace ImageResizeWebApp
{
Expand Down

0 comments on commit 892a64f

Please sign in to comment.