Core.BluePrint.Packages/Core.Blueprint.Storage/Configuration/RegisterBlueprint.cs
2025-06-09 00:39:20 -06:00

52 lines
2.1 KiB
C#

using Azure.Identity;
using Core.Blueprint.Storage.Contracts;
using Core.Blueprint.Storage.Provider;
using Microsoft.Extensions.Azure;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
namespace Core.Blueprint.Storage.Configuration
{
public static class RegisterBlueprint
{
public static IServiceCollection AddBlobStorage(this IServiceCollection services, IConfiguration configuration)
{
var blobConnection = configuration.GetConnectionString("BlobStorage");
if (string.IsNullOrWhiteSpace(blobConnection))
throw new ArgumentException("The BlobStorage configuration section is missing or empty.");
var environment = Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT") ?? string.Empty;
services.AddAzureClients(cfg =>
{
if (environment == "Local")
{
var accountKey = configuration.GetSection("BlobStorage:AccountKey").Value;
var accountName = configuration.GetSection("BlobStorage:AccountName").Value;
if(string.IsNullOrEmpty(accountKey) && string.IsNullOrEmpty(accountName))
throw new ArgumentException("The BlobStorage configuration section is missing or empty.");
cfg.AddBlobServiceClient(configuration.GetConnectionString("BlobStorage"));
}
else
{
var chainedCredentials = new ChainedTokenCredential(
new ManagedIdentityCredential(),
new SharedTokenCacheCredential(),
new VisualStudioCredential(),
new VisualStudioCodeCredential()
);
cfg.AddBlobServiceClient(new Uri(blobConnection))
.WithCredential(chainedCredentials);
}
});
services.AddScoped<IBlobStorageProvider, BlobStorageProvider>();
return services;
}
}
}