diff --git a/AppWithAzureBlobStorage/OnlineShop/OnlineShop.ApiService/OnlineShop.ApiService.csproj b/AppWithAzureBlobStorage/OnlineShop/OnlineShop.ApiService/OnlineShop.ApiService.csproj
index 10f224f..1b0046c 100644
--- a/AppWithAzureBlobStorage/OnlineShop/OnlineShop.ApiService/OnlineShop.ApiService.csproj
+++ b/AppWithAzureBlobStorage/OnlineShop/OnlineShop.ApiService/OnlineShop.ApiService.csproj
@@ -12,8 +12,10 @@
+
+
diff --git a/AppWithAzureBlobStorage/OnlineShop/OnlineShop.ApiService/Program.cs b/AppWithAzureBlobStorage/OnlineShop/OnlineShop.ApiService/Program.cs
index c2f01f8..fa4fde9 100644
--- a/AppWithAzureBlobStorage/OnlineShop/OnlineShop.ApiService/Program.cs
+++ b/AppWithAzureBlobStorage/OnlineShop/OnlineShop.ApiService/Program.cs
@@ -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();
+
+ 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();
+
+ 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();
+ }
+
+ List 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()
+ .ToList();
+ }
+
+ return productSpecs.ToArray();
+});
+
app.MapDefaultEndpoints();
app.Run();
diff --git a/AppWithAzureBlobStorage/OnlineShop/OnlineShop.AppHost/AppHost.cs b/AppWithAzureBlobStorage/OnlineShop/OnlineShop.AppHost/AppHost.cs
index 6bc7462..73b656e 100644
--- a/AppWithAzureBlobStorage/OnlineShop/OnlineShop.AppHost/AppHost.cs
+++ b/AppWithAzureBlobStorage/OnlineShop/OnlineShop.AppHost/AppHost.cs
@@ -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("apiservice")
.WithHttpHealthCheck("/health")
+ .WithReference(blobs)
+ .WaitFor(blobs)
.WithReference(tables)
.WaitFor(tables)
.WithReference(mongodb)