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>
|
||||
<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.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.OpenApi" Version="10.0.0" />
|
||||
</ItemGroup>
|
||||
|
||||
@@ -1,4 +1,7 @@
|
||||
using Azure.Data.Tables;
|
||||
using Azure.Storage.Blobs;
|
||||
using CsvHelper;
|
||||
using CsvHelper.Configuration;
|
||||
using Microsoft.AspNetCore.Authentication.JwtBearer;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using Microsoft.Data.SqlClient;
|
||||
@@ -6,6 +9,8 @@ using MongoDB.Driver;
|
||||
using OnlineShop.ApiService;
|
||||
using OnlineShop.ApiService.Model;
|
||||
using OnlineShop.ServiceDefaults.Dtos;
|
||||
using System.Globalization;
|
||||
using System.Text;
|
||||
|
||||
var builder = WebApplication.CreateBuilder(args);
|
||||
|
||||
@@ -229,6 +234,52 @@ using (var scope = app.Services.CreateScope())
|
||||
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();
|
||||
@@ -306,6 +357,37 @@ app.MapGet("/product-metadata",
|
||||
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.Run();
|
||||
|
||||
@@ -19,12 +19,19 @@ var sqldb = sql.AddDatabase("sqldb");
|
||||
var mongo = builder.AddMongoDB("mongo").WithLifetime(ContainerLifetime.Persistent);
|
||||
var mongodb = mongo.AddDatabase("mongodb");
|
||||
|
||||
var tables = builder.AddAzureStorage("storage")
|
||||
.RunAsEmulator()
|
||||
var storage = builder.AddAzureStorage("storage")
|
||||
.RunAsEmulator();
|
||||
|
||||
var tables = storage
|
||||
.AddTables("tables");
|
||||
|
||||
var blobs = storage
|
||||
.AddBlobContainer("blobs");
|
||||
|
||||
var apiService = builder.AddProject<Projects.OnlineShop_ApiService>("apiservice")
|
||||
.WithHttpHealthCheck("/health")
|
||||
.WithReference(blobs)
|
||||
.WaitFor(blobs)
|
||||
.WithReference(tables)
|
||||
.WaitFor(tables)
|
||||
.WithReference(mongodb)
|
||||
|
||||
Reference in New Issue
Block a user