using Minio;
using Minio.DataModel.Args;
namespace Clarity.Server.Extensions;
public static class MinioExtensions
{
///
/// Idempotently provisions MinIO buckets on application startup.
///
public static async Task ProvisionBucketsAsync(this WebApplication app, params string[] bucketNames)
{
// Skip entirely if Minio isn't configured (e.g. tenant containers without Minio env var)
var connStr = app.Configuration.GetConnectionString("minio");
if (string.IsNullOrWhiteSpace(connStr)) return;
using var scope = app.Services.CreateScope();
var minioClient = scope.ServiceProvider.GetRequiredService();
var logger = scope.ServiceProvider.GetRequiredService>();
foreach (var bucket in bucketNames)
{
try
{
var exists = await minioClient.BucketExistsAsync(new BucketExistsArgs().WithBucket(bucket));
if (!exists)
{
await minioClient.MakeBucketAsync(new MakeBucketArgs().WithBucket(bucket));
logger.LogInformation("🚀 Project Clarity: Successfully provisioned MinIO bucket '{Bucket}'", bucket);
}
}
catch (Exception ex)
{
logger.LogError(ex, "❌ Project Clarity: Failed to provision MinIO bucket '{Bucket}'", bucket);
}
}
}
}