Switch CDN to S3-compatible Ceph RadosGW (AWSSDK.S3)
Replace custom HttpCdnClient (proprietary upload API) with S3CdnClient backed by AWSSDK.S3 using ForcePathStyle for Ceph RadosGW compatibility. CdnOptions gains EndpointUrl/AccessKey/SecretKey/BucketName; removes UploadBaseUrl/ApiKey. K8s deployments and secrets template updated to match new config keys. Generated with [Claude Code](https://claude.ai/code) via [Happy](https://happy.engineering) Co-Authored-By: Claude <noreply@anthropic.com> Co-Authored-By: Happy <yesreply@happy.engineering>
This commit is contained in:
@@ -37,6 +37,10 @@
|
||||
<!-- Identity with EF Core: user/role store backed by a DbContext -->
|
||||
<PackageVersion Include="Microsoft.AspNetCore.Identity.EntityFrameworkCore" Version="8.0.0" />
|
||||
|
||||
<!-- ── AWS SDK ────────────────────────────────────────────────────────── -->
|
||||
<!-- S3-compatible object storage client (also works with Ceph RadosGW) -->
|
||||
<PackageVersion Include="AWSSDK.S3" Version="3.7.414.0" />
|
||||
|
||||
<!-- ── Logging ────────────────────────────────────────────────────────── -->
|
||||
<!-- Serilog: structured logging library (replaces Microsoft's default logger) -->
|
||||
<PackageVersion Include="Serilog.AspNetCore" Version="8.0.0" />
|
||||
|
||||
@@ -1,14 +1,16 @@
|
||||
namespace SOR.Cdn;
|
||||
|
||||
/// <summary>
|
||||
/// Configuration for the CDN client.
|
||||
/// Configuration for the S3-compatible CDN client (Ceph RadosGW).
|
||||
///
|
||||
/// appsettings.json section: "Cdn"
|
||||
///
|
||||
/// In production, inject via k8s secret as environment variables:
|
||||
/// Cdn__PublicBaseUrl → public URL prefix served to clients (e.g. https://cdn.sor.gg)
|
||||
/// Cdn__UploadBaseUrl → internal upload endpoint (e.g. https://upload.sor.gg)
|
||||
/// Cdn__ApiKey → API key for upload/delete operations
|
||||
/// Cdn__PublicBaseUrl → public URL prefix served to clients (e.g. https://cdn.aeternalisgames.com)
|
||||
/// Cdn__EndpointUrl → S3 endpoint URL (e.g. http://172.10.1.60:7480)
|
||||
/// Cdn__AccessKey → S3 access key ID
|
||||
/// Cdn__SecretKey → S3 secret access key
|
||||
/// Cdn__BucketName → S3 bucket name (e.g. sor-assets)
|
||||
/// </summary>
|
||||
public sealed class CdnOptions
|
||||
{
|
||||
@@ -16,19 +18,26 @@ public sealed class CdnOptions
|
||||
|
||||
/// <summary>
|
||||
/// Public base URL for serving CDN assets.
|
||||
/// Returned URLs are <see cref="PublicBaseUrl"/>/{path}.
|
||||
/// Example: https://cdn.sor.gg
|
||||
/// Returned URLs are <see cref="PublicBaseUrl"/>/{folder}/{fileName}.
|
||||
/// Example: https://cdn.aeternalisgames.com
|
||||
/// </summary>
|
||||
public string PublicBaseUrl { get; set; } = string.Empty;
|
||||
|
||||
/// <summary>
|
||||
/// Base URL of the CDN upload service.
|
||||
/// Example: https://upload.sor.gg
|
||||
/// S3-compatible endpoint URL (Ceph RadosGW).
|
||||
/// Example: http://172.10.1.60:7480
|
||||
/// </summary>
|
||||
public string UploadBaseUrl { get; set; } = string.Empty;
|
||||
public string EndpointUrl { get; set; } = string.Empty;
|
||||
|
||||
/// <summary>S3 access key ID.</summary>
|
||||
public string AccessKey { get; set; } = string.Empty;
|
||||
|
||||
/// <summary>S3 secret access key.</summary>
|
||||
public string SecretKey { get; set; } = string.Empty;
|
||||
|
||||
/// <summary>
|
||||
/// API key sent as the X-Api-Key request header on all write operations.
|
||||
/// S3 bucket name where assets are stored.
|
||||
/// Example: sor-assets
|
||||
/// </summary>
|
||||
public string ApiKey { get; set; } = string.Empty;
|
||||
public string BucketName { get; set; } = string.Empty;
|
||||
}
|
||||
|
||||
@@ -1,35 +1,28 @@
|
||||
namespace SOR.Cdn;
|
||||
|
||||
using System.Net.Http.Json;
|
||||
using Amazon.S3;
|
||||
using Amazon.S3.Model;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using Microsoft.Extensions.Options;
|
||||
|
||||
/// <summary>
|
||||
/// HTTP-based CDN client.
|
||||
/// S3-compatible CDN client backed by Ceph RadosGW.
|
||||
///
|
||||
/// Expected CDN server API:
|
||||
/// Uploads objects with public-read ACL; the public URL is built as:
|
||||
/// {PublicBaseUrl}/{folder}/{fileName}
|
||||
///
|
||||
/// POST {UploadBaseUrl}/upload?folder={folder}
|
||||
/// Header: X-Api-Key: {ApiKey}
|
||||
/// Body: multipart/form-data, field name "file"
|
||||
/// Returns: 200 { "path": "cards/abc/art.webp", "url": "https://cdn.sor.gg/cards/abc/art.webp" }
|
||||
///
|
||||
/// DELETE {UploadBaseUrl}/files/{path}
|
||||
/// Header: X-Api-Key: {ApiKey}
|
||||
/// Returns: 200 or 404 (both treated as success — idempotent)
|
||||
/// Requires ForcePathStyle=true for Ceph (virtual-hosted bucket style is not supported
|
||||
/// without explicit DNS configuration).
|
||||
/// </summary>
|
||||
public sealed class HttpCdnClient : ICdnClient
|
||||
public sealed class S3CdnClient : ICdnClient
|
||||
{
|
||||
private readonly HttpClient _http;
|
||||
private readonly IAmazonS3 _s3;
|
||||
private readonly CdnOptions _options;
|
||||
private readonly ILogger<HttpCdnClient> _logger;
|
||||
private readonly ILogger<S3CdnClient> _logger;
|
||||
|
||||
public HttpCdnClient(
|
||||
HttpClient http,
|
||||
IOptions<CdnOptions> options,
|
||||
ILogger<HttpCdnClient> logger)
|
||||
public S3CdnClient(IAmazonS3 s3, IOptions<CdnOptions> options, ILogger<S3CdnClient> logger)
|
||||
{
|
||||
_http = http;
|
||||
_s3 = s3;
|
||||
_options = options.Value;
|
||||
_logger = logger;
|
||||
}
|
||||
@@ -41,60 +34,39 @@ public sealed class HttpCdnClient : ICdnClient
|
||||
Stream data,
|
||||
CancellationToken ct = default)
|
||||
{
|
||||
var uploadUrl = $"{_options.UploadBaseUrl.TrimEnd('/')}/upload" +
|
||||
$"?folder={Uri.EscapeDataString(folder)}";
|
||||
var key = $"{folder.Trim('/')}/{fileName}";
|
||||
|
||||
using var multipart = new MultipartFormDataContent();
|
||||
var fileContent = new StreamContent(data);
|
||||
fileContent.Headers.ContentType =
|
||||
new System.Net.Http.Headers.MediaTypeHeaderValue(contentType);
|
||||
multipart.Add(fileContent, "file", fileName);
|
||||
|
||||
using var request = new HttpRequestMessage(HttpMethod.Post, uploadUrl)
|
||||
{
|
||||
Content = multipart,
|
||||
};
|
||||
request.Headers.Add("X-Api-Key", _options.ApiKey);
|
||||
|
||||
HttpResponseMessage response;
|
||||
try
|
||||
{
|
||||
response = await _http.SendAsync(request, ct);
|
||||
var request = new PutObjectRequest
|
||||
{
|
||||
BucketName = _options.BucketName,
|
||||
Key = key,
|
||||
InputStream = data,
|
||||
ContentType = contentType,
|
||||
CannedACL = S3CannedACL.PublicRead,
|
||||
DisableDefaultChecksumValidation = true,
|
||||
};
|
||||
|
||||
await _s3.PutObjectAsync(request, ct);
|
||||
|
||||
var publicUrl = $"{_options.PublicBaseUrl.TrimEnd('/')}/{key}";
|
||||
_logger.LogInformation("CDN upload succeeded: {Url}", publicUrl);
|
||||
return new CdnUploadResult(true, key, publicUrl, null);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.LogError(ex, "CDN upload request failed for {Folder}/{FileName}", folder, fileName);
|
||||
_logger.LogError(ex, "CDN upload failed for {Folder}/{FileName}", folder, fileName);
|
||||
return new CdnUploadResult(false, null, null, ex.Message);
|
||||
}
|
||||
|
||||
if (!response.IsSuccessStatusCode)
|
||||
{
|
||||
var body = await response.Content.ReadAsStringAsync(ct);
|
||||
_logger.LogWarning("CDN upload HTTP {Status} for {Folder}/{FileName}: {Body}",
|
||||
(int)response.StatusCode, folder, fileName, body);
|
||||
return new CdnUploadResult(false, null, null, $"HTTP {(int)response.StatusCode}");
|
||||
}
|
||||
|
||||
var json = await response.Content.ReadFromJsonAsync<CdnUploadResponse>(ct);
|
||||
if (json is null)
|
||||
return new CdnUploadResult(false, null, null, "Empty or invalid response from CDN");
|
||||
|
||||
_logger.LogInformation("CDN upload succeeded: {Url}", json.Url);
|
||||
return new CdnUploadResult(true, json.Path, json.Url, null);
|
||||
}
|
||||
|
||||
public async Task<bool> DeleteAsync(string path, CancellationToken ct = default)
|
||||
{
|
||||
var deleteUrl = $"{_options.UploadBaseUrl.TrimEnd('/')}/files/{Uri.EscapeDataString(path)}";
|
||||
|
||||
using var request = new HttpRequestMessage(HttpMethod.Delete, deleteUrl);
|
||||
request.Headers.Add("X-Api-Key", _options.ApiKey);
|
||||
|
||||
try
|
||||
{
|
||||
var response = await _http.SendAsync(request, ct);
|
||||
// 200 = deleted, 404 = already gone — both are fine.
|
||||
return response.IsSuccessStatusCode || response.StatusCode == System.Net.HttpStatusCode.NotFound;
|
||||
await _s3.DeleteObjectAsync(_options.BucketName, path, ct);
|
||||
return true;
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
@@ -102,7 +74,4 @@ public sealed class HttpCdnClient : ICdnClient
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
// Response shape from the CDN upload endpoint.
|
||||
private sealed record CdnUploadResponse(string Path, string Url);
|
||||
}
|
||||
|
||||
@@ -4,7 +4,11 @@
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<!-- ASP.NET Core framework: IOptions<T>, ILogger<T>, IHttpClientFactory, etc. -->
|
||||
<!-- ASP.NET Core framework: IOptions<T>, ILogger<T>, etc. -->
|
||||
<FrameworkReference Include="Microsoft.AspNetCore.App" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="AWSSDK.S3" />
|
||||
</ItemGroup>
|
||||
</Project>
|
||||
|
||||
@@ -1,16 +1,18 @@
|
||||
namespace SOR.Cdn;
|
||||
|
||||
using Amazon.S3;
|
||||
using Microsoft.Extensions.Configuration;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using Microsoft.Extensions.Options;
|
||||
|
||||
public static class ServiceRegistration
|
||||
{
|
||||
/// <summary>
|
||||
/// Registers <see cref="ICdnClient"/> (backed by <see cref="HttpCdnClient"/>) and reads
|
||||
/// Registers <see cref="ICdnClient"/> (backed by <see cref="S3CdnClient"/>) and reads
|
||||
/// configuration from the "Cdn" section.
|
||||
///
|
||||
/// Production: override via k8s secret env vars:
|
||||
/// Cdn__PublicBaseUrl, Cdn__UploadBaseUrl, Cdn__ApiKey
|
||||
/// Cdn__PublicBaseUrl, Cdn__EndpointUrl, Cdn__AccessKey, Cdn__SecretKey, Cdn__BucketName
|
||||
/// </summary>
|
||||
public static IServiceCollection AddCdnClient(
|
||||
this IServiceCollection services,
|
||||
@@ -18,8 +20,21 @@ public static class ServiceRegistration
|
||||
{
|
||||
services.Configure<CdnOptions>(configuration.GetSection(CdnOptions.Section));
|
||||
|
||||
// Typed HttpClient — IHttpClientFactory manages connection pooling + lifetime.
|
||||
services.AddHttpClient<ICdnClient, HttpCdnClient>();
|
||||
// Build AmazonS3Client from CdnOptions; registered as Singleton because
|
||||
// AmazonS3Client manages its own connection pool.
|
||||
services.AddSingleton<IAmazonS3>(sp =>
|
||||
{
|
||||
var opts = sp.GetRequiredService<IOptions<CdnOptions>>().Value;
|
||||
var config = new AmazonS3Config
|
||||
{
|
||||
ServiceURL = opts.EndpointUrl,
|
||||
ForcePathStyle = true, // required for Ceph RadosGW
|
||||
AuthenticationRegion = "us-east-1",
|
||||
};
|
||||
return new AmazonS3Client(opts.AccessKey, opts.SecretKey, config);
|
||||
});
|
||||
|
||||
services.AddSingleton<ICdnClient, S3CdnClient>();
|
||||
|
||||
return services;
|
||||
}
|
||||
|
||||
@@ -37,22 +37,32 @@ spec:
|
||||
name: sor-app-secrets
|
||||
key: ConnectionStrings__Default
|
||||
|
||||
# CDN — needed for asset uploads in the admin portal.
|
||||
# CDN (Ceph RadosGW S3-compatible) — needed for asset uploads in the admin portal.
|
||||
- name: Cdn__PublicBaseUrl
|
||||
valueFrom:
|
||||
secretKeyRef:
|
||||
name: sor-app-secrets
|
||||
key: Cdn__PublicBaseUrl
|
||||
- name: Cdn__UploadBaseUrl
|
||||
- name: Cdn__EndpointUrl
|
||||
valueFrom:
|
||||
secretKeyRef:
|
||||
name: sor-app-secrets
|
||||
key: Cdn__UploadBaseUrl
|
||||
- name: Cdn__ApiKey
|
||||
key: Cdn__EndpointUrl
|
||||
- name: Cdn__AccessKey
|
||||
valueFrom:
|
||||
secretKeyRef:
|
||||
name: sor-app-secrets
|
||||
key: Cdn__ApiKey
|
||||
key: Cdn__AccessKey
|
||||
- name: Cdn__SecretKey
|
||||
valueFrom:
|
||||
secretKeyRef:
|
||||
name: sor-app-secrets
|
||||
key: Cdn__SecretKey
|
||||
- name: Cdn__BucketName
|
||||
valueFrom:
|
||||
secretKeyRef:
|
||||
name: sor-app-secrets
|
||||
key: Cdn__BucketName
|
||||
|
||||
# ASP.NET Core Data Protection — persists Blazor auth cookies across restarts.
|
||||
# TODO: generate a strong random value and store in sor-app-secrets.
|
||||
|
||||
@@ -48,22 +48,32 @@ spec:
|
||||
name: redis-credentials
|
||||
key: connection-string
|
||||
|
||||
# CDN
|
||||
# CDN (Ceph RadosGW S3-compatible)
|
||||
- name: Cdn__PublicBaseUrl
|
||||
valueFrom:
|
||||
secretKeyRef:
|
||||
name: sor-app-secrets
|
||||
key: Cdn__PublicBaseUrl
|
||||
- name: Cdn__UploadBaseUrl
|
||||
- name: Cdn__EndpointUrl
|
||||
valueFrom:
|
||||
secretKeyRef:
|
||||
name: sor-app-secrets
|
||||
key: Cdn__UploadBaseUrl
|
||||
- name: Cdn__ApiKey
|
||||
key: Cdn__EndpointUrl
|
||||
- name: Cdn__AccessKey
|
||||
valueFrom:
|
||||
secretKeyRef:
|
||||
name: sor-app-secrets
|
||||
key: Cdn__ApiKey
|
||||
key: Cdn__AccessKey
|
||||
- name: Cdn__SecretKey
|
||||
valueFrom:
|
||||
secretKeyRef:
|
||||
name: sor-app-secrets
|
||||
key: Cdn__SecretKey
|
||||
- name: Cdn__BucketName
|
||||
valueFrom:
|
||||
secretKeyRef:
|
||||
name: sor-app-secrets
|
||||
key: Cdn__BucketName
|
||||
|
||||
# ── Health checks ──────────────────────────────────────────────────
|
||||
livenessProbe:
|
||||
|
||||
@@ -33,14 +33,16 @@ In your Gitea repository → Settings → Secrets, add:
|
||||
|
||||
## Secrets Reference
|
||||
|
||||
| Secret Name | Key | Used By | Notes |
|
||||
|--------------------|----------------------------|-----------------|-------------------------------------------|
|
||||
| sor-app-secrets | ConnectionStrings__Default | host + admin | Postgres connection string |
|
||||
| sor-app-secrets | Cdn__PublicBaseUrl | host + admin | e.g. https://cdn.aeternalisgames.com |
|
||||
| sor-app-secrets | Cdn__UploadBaseUrl | host + admin | CDN upload endpoint |
|
||||
| sor-app-secrets | Cdn__ApiKey | host + admin | CDN API key |
|
||||
| redis-credentials | connection-string | host only | **Already exists** in stone-server ns |
|
||||
| harbor-credentials | .dockerconfigjson | both pods | Harbor image pull secret |
|
||||
| Secret Name | Key | Used By | Notes |
|
||||
|--------------------|----------------------------|-----------------|-------------------------------------------------|
|
||||
| sor-app-secrets | ConnectionStrings__Default | host + admin | Postgres connection string |
|
||||
| sor-app-secrets | Cdn__PublicBaseUrl | host + admin | e.g. https://cdn.aeternalisgames.com |
|
||||
| sor-app-secrets | Cdn__EndpointUrl | host + admin | Ceph RadosGW endpoint, e.g. http://172.10.1.60:7480 |
|
||||
| sor-app-secrets | Cdn__AccessKey | host + admin | S3 access key ID |
|
||||
| sor-app-secrets | Cdn__SecretKey | host + admin | S3 secret access key |
|
||||
| sor-app-secrets | Cdn__BucketName | host + admin | S3 bucket name, e.g. sor-assets |
|
||||
| redis-credentials | connection-string | host only | **Already exists** in stone-server ns |
|
||||
| harbor-credentials | .dockerconfigjson | both pods | Harbor image pull secret |
|
||||
|
||||
---
|
||||
|
||||
|
||||
@@ -9,7 +9,11 @@ stringData:
|
||||
# Format: Host=HOST;Port=5432;Database=SOR;Username=USER;Password=PASSWORD;
|
||||
ConnectionStrings__Default: "Host=172.10.1.127;Port=5432;Database=SOR;Username=SOR-DBA;Password=CHANGEME"
|
||||
|
||||
# CDN — public URL served to clients, upload endpoint, and API key.
|
||||
# CDN (Ceph RadosGW S3-compatible).
|
||||
# PublicBaseUrl: the public-facing URL prefix returned to clients for asset links.
|
||||
# EndpointUrl: internal S3 endpoint (e.g. http://172.10.1.60:7480).
|
||||
Cdn__PublicBaseUrl: "https://CHANGEME"
|
||||
Cdn__UploadBaseUrl: "https://CHANGEME"
|
||||
Cdn__ApiKey: "CHANGEME"
|
||||
Cdn__EndpointUrl: "http://CHANGEME:7480"
|
||||
Cdn__AccessKey: "CHANGEME"
|
||||
Cdn__SecretKey: "CHANGEME"
|
||||
Cdn__BucketName: "sor-assets"
|
||||
|
||||
Reference in New Issue
Block a user