Table of Contents

Class R2ClientFactory

Namespace
Cloudflare.NET.R2
Assembly
Cloudflare.NET.R2.dll

Factory that creates and caches IR2Client instances. Supports named configurations for multi-account scenarios and jurisdiction-specific clients for accessing buckets in different geographic regions.

public sealed class R2ClientFactory : IR2ClientFactory, IDisposable
Inheritance
R2ClientFactory
Implements
Inherited Members

Remarks

This factory caches created clients to leverage the thread-safe, singleton-friendly nature of the AWS S3 client. Clients are cached by a composite key of (name, jurisdiction).

Named clients are registered using AddCloudflareR2Client(IServiceCollection, string, Action<R2Settings>) .

Constructors

R2ClientFactory(IOptionsMonitor<R2Settings>, IOptionsMonitor<CloudflareApiOptions>, ILoggerFactory)

Initializes a new instance of the R2ClientFactory class.

public R2ClientFactory(IOptionsMonitor<R2Settings> r2OptionsMonitor, IOptionsMonitor<CloudflareApiOptions> cloudflareOptionsMonitor, ILoggerFactory loggerFactory)

Parameters

r2OptionsMonitor IOptionsMonitor<R2Settings>

The options monitor for named R2 settings.

cloudflareOptionsMonitor IOptionsMonitor<CloudflareApiOptions>

The options monitor for named Cloudflare API options.

loggerFactory ILoggerFactory

The logger factory.

Methods

Dispose()

Disposes all cached R2 clients and their underlying S3 clients.

public void Dispose()

GetClient(R2Jurisdiction)

Gets an IR2Client for the specified jurisdiction using the default (unnamed) credentials.

public IR2Client GetClient(R2Jurisdiction jurisdiction)

Parameters

jurisdiction R2Jurisdiction

The target jurisdiction for R2 operations.

Returns

IR2Client

A cached IR2Client instance configured for the specified jurisdiction.

Examples

// Access buckets in different jurisdictions with the same credentials
var globalClient = factory.GetClient(R2Jurisdiction.Default);
var euClient = factory.GetClient(R2Jurisdiction.EuropeanUnion);
var fedRampClient = factory.GetClient(R2Jurisdiction.FedRamp);

// Upload to EU-jurisdictional bucket
await euClient.UploadAsync("my-eu-bucket", "file.txt", stream);

Remarks

This method uses the credentials from the default (unnamed) R2 configuration registered via AddCloudflareR2Client(IServiceCollection, Action<R2Settings>) .

The same R2 credentials work across all jurisdictions within an account; only the S3 endpoint differs. Clients are cached by jurisdiction and reused for subsequent calls.

Exceptions

CloudflareR2ConfigurationException

Thrown when the default R2 configuration is missing or invalid.

GetClient(string)

Gets or creates a named IR2Client instance configured with the specified named options.

public IR2Client GetClient(string name)

Parameters

name string

The name of the client configuration to use. Must match the name used during registration with AddCloudflareR2Client(IServiceCollection, string, Action<R2Settings>) .

Returns

IR2Client

A cached IR2Client instance configured with the named options.

Examples

// Registration
services.AddCloudflareR2Client("primary", options => {
    options.AccessKeyId = "primary-key";
    options.SecretAccessKey = "primary-secret";
});
services.AddCloudflareR2Client("backup", options => {
    options.AccessKeyId = "backup-key";
    options.SecretAccessKey = "backup-secret";
});

// Usage via factory
public class MyService(IR2ClientFactory factory)
{
    public async Task DoSomething()
    {
        var primaryClient = factory.GetClient("primary");
        var backupClient = factory.GetClient("backup");
        // ...
    }
}

Remarks

The client uses the jurisdiction specified in the named configuration's Jurisdiction. To access a different jurisdiction with the same credentials, use GetClient(string, R2Jurisdiction).

Clients are cached by (name, configured-jurisdiction) and reused for subsequent calls.

Exceptions

ArgumentException

Thrown when name is null or whitespace.

CloudflareR2ConfigurationException

Thrown when the named configuration is missing or invalid.

GetClient(string, R2Jurisdiction)

Gets an IR2Client for the specified jurisdiction using named credentials.

public IR2Client GetClient(string name, R2Jurisdiction jurisdiction)

Parameters

name string

The name of the client configuration providing credentials. Must match the name used during registration.

jurisdiction R2Jurisdiction

The target jurisdiction for R2 operations.

Returns

IR2Client

A cached IR2Client instance configured with the named credentials and specified jurisdiction.

Examples

// Production account has buckets in both default and EU jurisdictions
var prodDefault = factory.GetClient("production", R2Jurisdiction.Default);
var prodEu = factory.GetClient("production", R2Jurisdiction.EuropeanUnion);

// DR account uses FedRAMP jurisdiction
var drFedRamp = factory.GetClient("disaster-recovery", R2Jurisdiction.FedRamp);

Remarks

Use this method when you have multiple accounts (each with different credentials) and need to access buckets in specific jurisdictions within those accounts.

Clients are cached by (name, jurisdiction) tuple and reused for subsequent calls.

Exceptions

ArgumentException

Thrown when name is null or whitespace.

CloudflareR2ConfigurationException

Thrown when the named configuration is missing or invalid.