Add Azure Blob startup profile and config sample

This commit is contained in:
Jacob Dubin
2026-05-17 08:01:04 -05:00
parent 478a320581
commit 05efeb2853
12 changed files with 211 additions and 6 deletions

View File

@@ -7,6 +7,18 @@
"ASPNETCORE_ENVIRONMENT": "Development"
},
"applicationUrl": "https://localhost:24604;http://localhost:24605"
},
"Jibo.Cloud.Api.AzureBlob": {
"commandName": "Project",
"launchBrowser": true,
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development",
"OpenJibo__State__Backend": "AzureBlob",
"OpenJibo__State__ConnectionString": "UseDevelopmentStorage=true",
"OpenJibo__PersonalMemory__Backend": "AzureBlob",
"OpenJibo__PersonalMemory__ConnectionString": "UseDevelopmentStorage=true"
},
"applicationUrl": "https://localhost:24604;http://localhost:24605"
}
}
}
}

View File

@@ -0,0 +1,14 @@
{
"OpenJibo": {
"State": {
"Backend": "AzureBlob",
"ConnectionString": "UseDevelopmentStorage=true",
"PersistencePath": "App_Data/cloud-state.json"
},
"PersonalMemory": {
"Backend": "AzureBlob",
"ConnectionString": "UseDevelopmentStorage=true",
"PersistencePath": "App_Data/personal-memory.json"
}
}
}

View File

@@ -58,8 +58,12 @@ public static class ServiceCollectionExtensions
?? Path.Combine(AppContext.BaseDirectory, "App_Data", "personal-memory.json");
var stateBackendKind = ParseBackendKind(configuration?["OpenJibo:State:Backend"]);
var personalMemoryBackendKind = ParseBackendKind(configuration?["OpenJibo:PersonalMemory:Backend"]);
var stateConnectionString = configuration?["OpenJibo:State:ConnectionString"] ?? Environment.GetEnvironmentVariable("OPENJIBO_STATE_SQL_CONNECTION_STRING");
var personalMemoryConnectionString = configuration?["OpenJibo:PersonalMemory:ConnectionString"] ?? Environment.GetEnvironmentVariable("OPENJIBO_PERSONAL_MEMORY_SQL_CONNECTION_STRING");
var stateConnectionString = configuration?["OpenJibo:State:ConnectionString"]
?? Environment.GetEnvironmentVariable("OPENJIBO_STATE_STORAGE_CONNECTION_STRING")
?? Environment.GetEnvironmentVariable("OPENJIBO_STATE_SQL_CONNECTION_STRING");
var personalMemoryConnectionString = configuration?["OpenJibo:PersonalMemory:ConnectionString"]
?? Environment.GetEnvironmentVariable("OPENJIBO_PERSONAL_MEMORY_STORAGE_CONNECTION_STRING")
?? Environment.GetEnvironmentVariable("OPENJIBO_PERSONAL_MEMORY_SQL_CONNECTION_STRING");
services.AddSingleton<IPersistenceSnapshotStoreFactory, PersistenceSnapshotStoreFactory>();
services.AddSingleton<ICloudStateStore>(provider =>
{

View File

@@ -25,6 +25,7 @@
</ItemGroup>
<ItemGroup>
<PackageReference Include="Azure.Storage.Blobs" Version="12.28.0" />
<PackageReference Include="Microsoft.Data.SqlClient" Version="7.0.1" />
</ItemGroup>

View File

@@ -0,0 +1,68 @@
using System.Text.Json;
using Azure.Storage.Blobs;
using Azure.Storage.Blobs.Models;
namespace Jibo.Cloud.Infrastructure.Persistence;
internal sealed class AzureBlobSnapshotStore : ISnapshotStore
{
private static readonly JsonSerializerOptions JsonOptions = new()
{
WriteIndented = true,
PropertyNameCaseInsensitive = true
};
private readonly BlobContainerClient _containerClient;
private readonly string _blobName;
public AzureBlobSnapshotStore(string connectionString, string snapshotName, string containerName = "openjibo-snapshots")
{
if (string.IsNullOrWhiteSpace(connectionString))
{
throw new InvalidOperationException("Azure Blob persistence requires a storage connection string.");
}
if (string.IsNullOrWhiteSpace(snapshotName))
{
throw new ArgumentException("A snapshot name is required for Azure Blob persistence.", nameof(snapshotName));
}
_containerClient = new BlobContainerClient(connectionString, string.IsNullOrWhiteSpace(containerName) ? "openjibo-snapshots" : containerName);
_blobName = $"{snapshotName}.json";
}
public TSnapshot? Load<TSnapshot>() where TSnapshot : class
{
try
{
if (!_containerClient.Exists())
{
return default;
}
var blobClient = _containerClient.GetBlobClient(_blobName);
if (!blobClient.Exists())
{
return default;
}
var content = blobClient.DownloadContent();
var json = content.Value.Content.ToString();
return string.IsNullOrWhiteSpace(json)
? default
: JsonSerializer.Deserialize<TSnapshot>(json, JsonOptions);
}
catch
{
return default;
}
}
public void Save<TSnapshot>(TSnapshot snapshot) where TSnapshot : class
{
_containerClient.CreateIfNotExists(PublicAccessType.None);
var blobClient = _containerClient.GetBlobClient(_blobName);
var json = JsonSerializer.Serialize(snapshot, JsonOptions);
blobClient.Upload(BinaryData.FromString(json), overwrite: true);
}
}

View File

@@ -3,5 +3,6 @@ namespace Jibo.Cloud.Infrastructure.Persistence;
public enum PersistenceBackendKind
{
File,
AzureBlob,
AzureSql
}

View File

@@ -15,6 +15,9 @@ public sealed class PersistenceSnapshotStoreFactory : IPersistenceSnapshotStoreF
return backendKind switch
{
PersistenceBackendKind.File => new JsonFileSnapshotStore(persistencePath, JsonOptions),
PersistenceBackendKind.AzureBlob => new AzureBlobSnapshotStore(
connectionString ?? throw new InvalidOperationException("Azure Blob persistence requires a connection string."),
snapshotName),
PersistenceBackendKind.AzureSql => new AzureSqlSnapshotStore(
connectionString ?? throw new InvalidOperationException("Azure SQL persistence requires a connection string."),
snapshotName),