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:
@@ -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);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user