Path: redb-route/redb.Route.S3/S3ConnectionFactory.cs
Snapshot: redb-route, development snapshot 2026-12-06. This is the development state of the public mirrors; it may differ from the published packages.
License: Apache-2.0. Repository: https://github.com/redbase-app/redb-route
Page: https://redbase.app/src/dev-2026-12-06/redb-route/redb.Route.S3/S3ConnectionFactory.cs
----
using Amazon;
using Amazon.Runtime;
using Amazon.S3;
using redb.Route.Core;
namespace redb.Route.S3;
///
/// Connection factory for S3/MinIO. Register via DI or in the route registry
/// and reference by name in endpoint URIs (connectionFactory=myFactory).
///
/// Supports AWS S3, MinIO, Dell ECS, Ceph, and other S3-compatible stores.
///
///
public sealed class S3ConnectionFactory
{
///
/// Custom service URL for S3-compatible stores (MinIO, Dell ECS, Ceph).
/// Example: http://localhost:9000.
///
public string ServiceUrl { get; set; } = "";
/// AWS region. (default: "us-east-1")
public string Region { get; set; } = "us-east-1";
/// AWS access key ID.
[Sensitive]
public string AccessKey { get; set; } = "";
/// AWS secret access key.
[Sensitive]
public string SecretKey { get; set; } = "";
/// AWS session token (for temporary STS credentials).
[Sensitive]
public string SessionToken { get; set; } = "";
/// AWS named profile from ~/.aws/credentials.
public string ProfileName { get; set; } = "";
///
/// Use path-style URLs (required for MinIO and most S3-compatible stores).
/// (default: false)
///
public bool ForcePathStyle { get; set; }
///
/// Use the default AWS credentials provider chain.
/// (default: false)
///
public bool UseDefaultCredentialsProvider { get; set; }
/// Trust all SSL certificates. (default: false)
public bool TrustAllCertificates { get; set; }
// ── Timeouts ──
/// Connection timeout in milliseconds. (default: 30000)
public int ConnectionTimeout { get; set; } = 30_000;
/// Socket/read timeout in milliseconds. (default: 60000)
public int SocketTimeout { get; set; } = 60_000;
/// Maximum concurrent connections. (default: 50)
public int MaxConnections { get; set; } = 50;
/// Number of retry attempts. (default: 3)
public int RetryCount { get; set; } = 3;
/// Retry mode: "standard" or "adaptive". (default: "standard")
public string RetryMode { get; set; } = "standard";
// ── Proxy ──
/// HTTP proxy host.
public string ProxyHost { get; set; } = "";
/// HTTP proxy port.
public int ProxyPort { get; set; }
///
/// Creates an and an client
/// from this factory's settings.
///
public IAmazonS3 Build()
{
var config = new AmazonS3Config { ForcePathStyle = ForcePathStyle };
// ServiceURL and RegionEndpoint are mutually exclusive, and in SDK v4 setting one
// CLEARS the other — the old "set region, then null it out for ServiceUrl" dance
// wiped ServiceURL and Build() with a custom URL always threw
// "No RegionEndpoint or ServiceURL configured".
if (!string.IsNullOrEmpty(ServiceUrl))
{
config.ServiceURL = ServiceUrl;
config.AuthenticationRegion = Region;
}
else
{
config.RegionEndpoint = RegionEndpoint.GetBySystemName(Region);
}
S3ClientConfigSupport.Apply(config,
ConnectionTimeout, SocketTimeout, MaxConnections,
RetryCount, RetryMode, TrustAllCertificates);
if (!string.IsNullOrEmpty(ProxyHost) && ProxyPort > 0)
{
config.ProxyHost = ProxyHost;
config.ProxyPort = ProxyPort;
}
var credentials = ResolveCredentials();
return new AmazonS3Client(credentials, config);
}
///
/// Creates an from this factory's settings
/// without building the client.
///
internal AmazonS3Config BuildConfig()
{
var config = new AmazonS3Config { ForcePathStyle = ForcePathStyle };
S3ClientConfigSupport.Apply(config,
ConnectionTimeout, SocketTimeout, MaxConnections,
RetryCount, RetryMode, TrustAllCertificates);
if (!string.IsNullOrEmpty(ServiceUrl))
{
config.ServiceURL = ServiceUrl;
config.AuthenticationRegion = Region;
}
else
{
config.RegionEndpoint = RegionEndpoint.GetBySystemName(Region);
}
if (!string.IsNullOrEmpty(ProxyHost) && ProxyPort > 0)
{
config.ProxyHost = ProxyHost;
config.ProxyPort = ProxyPort;
}
return config;
}
private AWSCredentials ResolveCredentials()
{
if (UseDefaultCredentialsProvider)
// SDK v4: the non-deprecated default-chain resolver (env -> profile -> IMDS etc.)
return Amazon.Runtime.Credentials.DefaultAWSCredentialsIdentityResolver.GetCredentials();
if (!string.IsNullOrEmpty(ProfileName))
{
var chain = new Amazon.Runtime.CredentialManagement.CredentialProfileStoreChain();
if (chain.TryGetAWSCredentials(ProfileName, out var profileCreds))
return profileCreds;
throw new InvalidOperationException($"AWS profile '{ProfileName}' not found.");
}
if (!string.IsNullOrEmpty(SessionToken))
return new SessionAWSCredentials(AccessKey, SecretKey, SessionToken);
return new BasicAWSCredentials(AccessKey, SecretKey);
}
}