Skip to content

Commit

Permalink
Add connection string-based implementation of Azure Blob Storage
Browse files Browse the repository at this point in the history
  • Loading branch information
mderriey committed Feb 19, 2021
1 parent 420f3fa commit c32e1f6
Show file tree
Hide file tree
Showing 8 changed files with 93 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Azure.Storage.Blobs" Version="12.8.0" />
<PackageReference Include="Microsoft.ApplicationInsights.AspNetCore" Version="2.16.0" />
</ItemGroup>

Expand Down
25 changes: 21 additions & 4 deletions src/AzureIdentityLivestream.Web/Pages/Index.cshtml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,24 @@
ViewData["Title"] = "Home page";
}

<div class="text-center">
<h1 class="display-4">Welcome</h1>
<p>Learn about <a href="https://docs.microsoft.com/aspnet/core">building Web apps with ASP.NET Core</a>.</p>
</div>
<h2>People from <code>@Model.PersonProviderTypeName</code></h2>

<table class="table">
<thead>
<tr>
<th>First name</th>
<th>Last name</th>
<th>Age</th>
</tr>
</thead>
<tbody>
@foreach (var person in Model.People)
{
<tr>
<td>@person.FirstName</td>
<td>@person.LastName</td>
<td>@person.Age</td>
</tr>
}
</tbody>
</table>
21 changes: 11 additions & 10 deletions src/AzureIdentityLivestream.Web/Pages/Index.cshtml.cs
Original file line number Diff line number Diff line change
@@ -1,25 +1,26 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Collections.Generic;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using AzureIdentityLivestream.Web.Services;
using Microsoft.AspNetCore.Mvc.RazorPages;
using Microsoft.Extensions.Logging;

namespace AzureIdentityLivestream.Web.Pages
{
public class IndexModel : PageModel
{
private readonly ILogger<IndexModel> _logger;
private readonly IPersonProvider _personProvider;

public IndexModel(ILogger<IndexModel> logger)
public IndexModel(IPersonProvider personProvider)
{
_logger = logger;
_personProvider = personProvider;
}

public void OnGet()
public async Task OnGet()
{

People = await _personProvider.GetPeople();
PersonProviderTypeName = _personProvider.GetType().Name;
}

public IEnumerable<Person> People { get; set; }
public string PersonProviderTypeName { get; set; }
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
using System.Collections.Generic;
using System.IO;
using System.Text.Json;
using System.Threading.Tasks;
using Azure.Storage.Blobs;

namespace AzureIdentityLivestream.Web.Services.AzureBlobStorage
{
public class AzureBlobStoragePersonProvider : IPersonProvider
{
private readonly BlobServiceClient _blobServiceClient;

public AzureBlobStoragePersonProvider(BlobServiceClient blobServiceClient)
{
_blobServiceClient = blobServiceClient;
}

public async Task<IEnumerable<Person>> GetPeople()
{
List<Person> people = new();

var containerClient = _blobServiceClient.GetBlobContainerClient("people");
var blobs = containerClient.GetBlobsAsync();
await foreach (var blob in blobs)
{
var blobClient = containerClient.GetBlobClient(blob.Name);

await using var personStream = new MemoryStream();
var blobDownloadInfo = await blobClient.DownloadAsync();
var person = await JsonSerializer.DeserializeAsync<Person>(blobDownloadInfo.Value.Content);

people.Add(person);
}

return people;
}
}
}
10 changes: 10 additions & 0 deletions src/AzureIdentityLivestream.Web/Services/IPersonProvider.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
using System.Collections.Generic;
using System.Threading.Tasks;

namespace AzureIdentityLivestream.Web.Services
{
public interface IPersonProvider
{
Task<IEnumerable<Person>> GetPeople();
}
}
4 changes: 4 additions & 0 deletions src/AzureIdentityLivestream.Web/Services/Person.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
namespace AzureIdentityLivestream.Web.Services
{
public record Person(string FirstName, string LastName, int Age);
}
7 changes: 7 additions & 0 deletions src/AzureIdentityLivestream.Web/Startup.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
using Azure.Storage.Blobs;
using AzureIdentityLivestream.Web.Services;
using AzureIdentityLivestream.Web.Services.AzureBlobStorage;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Configuration;
Expand All @@ -18,6 +21,10 @@ public Startup(IConfiguration configuration)
public void ConfigureServices(IServiceCollection services)
{
services.AddApplicationInsightsTelemetry();

services.AddSingleton(_ => new BlobServiceClient(Configuration.GetValue<string>("StorageConnectionString")));
services.AddSingleton<IPersonProvider, AzureBlobStoragePersonProvider>();

services.AddRazorPages();
}

Expand Down
1 change: 1 addition & 0 deletions src/AzureIdentityLivestream.Web/appsettings.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
{
"StorageConnectionString": "UseDevelopmentStorage=true",
"Logging": {
"LogLevel": {
"Default": "Information",
Expand Down

0 comments on commit c32e1f6

Please sign in to comment.