mirror of
https://github.com/fiodarsazanavets/aspire-13-examples.git
synced 2026-06-20 12:23:14 +00:00
Add blob storage examples
This commit is contained in:
@@ -12,8 +12,10 @@
|
|||||||
|
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<PackageReference Include="Aspire.Azure.Data.Tables" Version="13.1.0" />
|
<PackageReference Include="Aspire.Azure.Data.Tables" Version="13.1.0" />
|
||||||
|
<PackageReference Include="Aspire.Azure.Storage.Blobs" Version="13.1.0" />
|
||||||
<PackageReference Include="Aspire.Microsoft.Data.SqlClient" Version="13.1.0" />
|
<PackageReference Include="Aspire.Microsoft.Data.SqlClient" Version="13.1.0" />
|
||||||
<PackageReference Include="Aspire.MongoDB.Driver" Version="13.1.0" />
|
<PackageReference Include="Aspire.MongoDB.Driver" Version="13.1.0" />
|
||||||
|
<PackageReference Include="CsvHelper" Version="33.1.0" />
|
||||||
<PackageReference Include="Microsoft.AspNetCore.Authentication.JwtBearer" Version="10.0.0" />
|
<PackageReference Include="Microsoft.AspNetCore.Authentication.JwtBearer" Version="10.0.0" />
|
||||||
<PackageReference Include="Microsoft.AspNetCore.OpenApi" Version="10.0.0" />
|
<PackageReference Include="Microsoft.AspNetCore.OpenApi" Version="10.0.0" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
|
|||||||
@@ -1,4 +1,7 @@
|
|||||||
using Azure.Data.Tables;
|
using Azure.Data.Tables;
|
||||||
|
using Azure.Storage.Blobs;
|
||||||
|
using CsvHelper;
|
||||||
|
using CsvHelper.Configuration;
|
||||||
using Microsoft.AspNetCore.Authentication.JwtBearer;
|
using Microsoft.AspNetCore.Authentication.JwtBearer;
|
||||||
using Microsoft.AspNetCore.Mvc;
|
using Microsoft.AspNetCore.Mvc;
|
||||||
using Microsoft.Data.SqlClient;
|
using Microsoft.Data.SqlClient;
|
||||||
@@ -6,6 +9,8 @@ using MongoDB.Driver;
|
|||||||
using OnlineShop.ApiService;
|
using OnlineShop.ApiService;
|
||||||
using OnlineShop.ApiService.Model;
|
using OnlineShop.ApiService.Model;
|
||||||
using OnlineShop.ServiceDefaults.Dtos;
|
using OnlineShop.ServiceDefaults.Dtos;
|
||||||
|
using System.Globalization;
|
||||||
|
using System.Text;
|
||||||
|
|
||||||
var builder = WebApplication.CreateBuilder(args);
|
var builder = WebApplication.CreateBuilder(args);
|
||||||
|
|
||||||
@@ -229,6 +234,52 @@ using (var scope = app.Services.CreateScope())
|
|||||||
await tableClient.AddEntityAsync(entity);
|
await tableClient.AddEntityAsync(entity);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
var blobServiceClient =
|
||||||
|
scope.ServiceProvider
|
||||||
|
.GetRequiredService<BlobServiceClient>();
|
||||||
|
|
||||||
|
var containerClient = blobServiceClient
|
||||||
|
.GetBlobContainerClient("products");
|
||||||
|
|
||||||
|
// Create the container if it doesn't exist
|
||||||
|
await containerClient.CreateIfNotExistsAsync();
|
||||||
|
|
||||||
|
var blobClient = containerClient
|
||||||
|
.GetBlobClient("products-specs.csv");
|
||||||
|
|
||||||
|
// Check if the blob already exists
|
||||||
|
if (await blobClient.ExistsAsync())
|
||||||
|
{
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
var productSpecs = new List<ProductSpecCsvRow>();
|
||||||
|
|
||||||
|
foreach (var productId in Enumerable.Range(1, 10))
|
||||||
|
{
|
||||||
|
productSpecs.Add(new ProductSpecCsvRow
|
||||||
|
{
|
||||||
|
ProductId = productId,
|
||||||
|
ReviewsEnabled = true,
|
||||||
|
Featured = productId % 2 == 0,
|
||||||
|
MaxReviewsPerUser = 1,
|
||||||
|
|
||||||
|
Category = productId % 2 == 0 ? "Laptop" : "Peripheral",
|
||||||
|
WarrantyMonths = productId % 2 == 0 ? 24 : 12
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
using (var memoryStream = new MemoryStream())
|
||||||
|
using (var writer = new StreamWriter(memoryStream, Encoding.UTF8, leaveOpen: true))
|
||||||
|
using (var csv = new CsvWriter(writer, new CsvConfiguration(CultureInfo.InvariantCulture)))
|
||||||
|
{
|
||||||
|
csv.WriteRecords(productSpecs);
|
||||||
|
writer.Flush();
|
||||||
|
|
||||||
|
memoryStream.Position = 0;
|
||||||
|
await blobClient.UploadAsync(memoryStream, overwrite: true);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
app.UseExceptionHandler();
|
app.UseExceptionHandler();
|
||||||
@@ -306,6 +357,37 @@ app.MapGet("/product-metadata",
|
|||||||
return metadata.ToArray();
|
return metadata.ToArray();
|
||||||
});
|
});
|
||||||
|
|
||||||
|
app.MapGet("/product-specs", async (
|
||||||
|
BlobServiceClient blobServiceClient) =>
|
||||||
|
{
|
||||||
|
var containerClient = blobServiceClient
|
||||||
|
.GetBlobContainerClient("products");
|
||||||
|
|
||||||
|
var blobClient = containerClient
|
||||||
|
.GetBlobClient("product-specs.csv");
|
||||||
|
|
||||||
|
if (!await blobClient.ExistsAsync())
|
||||||
|
{
|
||||||
|
return Array.Empty<ProductSpecCsvRow>();
|
||||||
|
}
|
||||||
|
|
||||||
|
List<ProductSpecCsvRow> productSpecs;
|
||||||
|
|
||||||
|
// Download the CSV from the blob
|
||||||
|
var downloadResponse = await blobClient.DownloadAsync();
|
||||||
|
|
||||||
|
using (var stream = downloadResponse.Value.Content)
|
||||||
|
using (var reader = new StreamReader(stream))
|
||||||
|
using (var csv = new CsvReader(reader, CultureInfo.InvariantCulture))
|
||||||
|
{
|
||||||
|
productSpecs = csv
|
||||||
|
.GetRecords<ProductSpecCsvRow>()
|
||||||
|
.ToList();
|
||||||
|
}
|
||||||
|
|
||||||
|
return productSpecs.ToArray();
|
||||||
|
});
|
||||||
|
|
||||||
app.MapDefaultEndpoints();
|
app.MapDefaultEndpoints();
|
||||||
|
|
||||||
app.Run();
|
app.Run();
|
||||||
|
|||||||
@@ -19,12 +19,19 @@ var sqldb = sql.AddDatabase("sqldb");
|
|||||||
var mongo = builder.AddMongoDB("mongo").WithLifetime(ContainerLifetime.Persistent);
|
var mongo = builder.AddMongoDB("mongo").WithLifetime(ContainerLifetime.Persistent);
|
||||||
var mongodb = mongo.AddDatabase("mongodb");
|
var mongodb = mongo.AddDatabase("mongodb");
|
||||||
|
|
||||||
var tables = builder.AddAzureStorage("storage")
|
var storage = builder.AddAzureStorage("storage")
|
||||||
.RunAsEmulator()
|
.RunAsEmulator();
|
||||||
|
|
||||||
|
var tables = storage
|
||||||
.AddTables("tables");
|
.AddTables("tables");
|
||||||
|
|
||||||
|
var blobs = storage
|
||||||
|
.AddBlobContainer("blobs");
|
||||||
|
|
||||||
var apiService = builder.AddProject<Projects.OnlineShop_ApiService>("apiservice")
|
var apiService = builder.AddProject<Projects.OnlineShop_ApiService>("apiservice")
|
||||||
.WithHttpHealthCheck("/health")
|
.WithHttpHealthCheck("/health")
|
||||||
|
.WithReference(blobs)
|
||||||
|
.WaitFor(blobs)
|
||||||
.WithReference(tables)
|
.WithReference(tables)
|
||||||
.WaitFor(tables)
|
.WaitFor(tables)
|
||||||
.WithReference(mongodb)
|
.WithReference(mongodb)
|
||||||
|
|||||||
Reference in New Issue
Block a user