Refactored service to internalise the CDN
continuous-integration/drone/pr Build is passing

This commit is contained in:
Khwezi Mngoma
2026-05-19 11:34:00 +02:00
parent f245bc94e1
commit 52d204e286
10 changed files with 188 additions and 46 deletions
+45 -18
View File
@@ -1,6 +1,6 @@
using Amazon.Runtime;
using LiteCharms.Features.S3;
using LiteCharms.Features.S3.Configuration;
using LiteCharms.Features.S3;
using LiteCharms.Features.S3.Abstractions;
using static LiteCharms.Features.S3.Constants;
namespace LiteCharms.Features.Extensions;
@@ -8,23 +8,50 @@ public static class S3
{
public static IServiceCollection AddGarageS3(this IServiceCollection services, IConfiguration configuration)
{
var optionsSection = configuration.GetSection(nameof(S3Settings));
services.Configure<S3Settings>(optionsSection);
var options = optionsSection.Get<S3Settings>()
?? throw new InvalidOperationException("S3 configuration section is missing.");
var credentials = new BasicAWSCredentials(options.AccessKey, options.SecretKey);
var s3Config = new AmazonS3Config
if (configuration.GetSection(BookshopBucketName) is not null)
{
ServiceURL = options.ServiceUrl,
AuthenticationRegion = options.Region,
ForcePathStyle = true,
};
services.AddKeyedSingleton<IAmazonS3, AmazonS3Client>(BookshopBucketName, (provider, client) =>
new AmazonS3Client(new BasicAWSCredentials(configuration.GetSection($"{BookshopS3SettingsSection}:AccessKey").Value,
configuration.GetSection($"{BookshopS3SettingsSection}:SecretKey").Value),
new AmazonS3Config
{
ServiceURL = configuration.GetSection($"{BookshopS3SettingsSection}:ServiceUrl").Value,
AuthenticationRegion = configuration.GetSection($"{BookshopS3SettingsSection}:Region").Value,
ForcePathStyle = true,
}));
services.AddSingleton<IAmazonS3>(new AmazonS3Client(credentials, s3Config));
services.AddScoped<S3Service>();
services.AddKeyedScoped<IS3Service, BookstoreS3Service>(BookshopBucketName);
}
if (configuration.GetSection(BookshopInvoicesBucketName) is not null)
{
services.AddKeyedSingleton<IAmazonS3, AmazonS3Client>(BookshopInvoicesBucketName, (provider, client) =>
new AmazonS3Client(new BasicAWSCredentials(configuration.GetSection($"{BookshopInvoicesBucketName}:AccessKey").Value,
configuration.GetSection($"{BookshopInvoicesBucketName}:SecretKey").Value),
new AmazonS3Config
{
ServiceURL = configuration.GetSection($"{BookshopInvoicesBucketName}:ServiceUrl").Value,
AuthenticationRegion = configuration.GetSection($"{BookshopInvoicesBucketName}:Region").Value,
ForcePathStyle = true,
}));
services.AddKeyedScoped<IS3Service, BookstoreInvoicesS3Service>(BookshopInvoicesBucketName);
}
if (configuration.GetSection(BookshopQuotesBucketName) is not null)
{
services.AddKeyedSingleton<IAmazonS3, AmazonS3Client>(BookshopQuotesBucketName, (provider, client) =>
new AmazonS3Client(new BasicAWSCredentials(configuration.GetSection($"{BookshopQuotesBucketName}:AccessKey").Value,
configuration.GetSection($"{BookshopQuotesBucketName}:SecretKey").Value),
new AmazonS3Config
{
ServiceURL = configuration.GetSection($"{BookshopQuotesBucketName}:ServiceUrl").Value,
AuthenticationRegion = configuration.GetSection($"{BookshopQuotesBucketName}:Region").Value,
ForcePathStyle = true,
}));
services.AddKeyedScoped<IS3Service, BookstoreQuotesS3Service>(BookshopQuotesBucketName);
}
return services;
}