SharpS3

NuGet NuGet downloads Docs License Coverage Status

A transport-agnostic .NET library for building Amazon S3 compatible clients, servers, and storage backends.

Features

Installation

Install the library via NuGet:

dotnet add package SharpS3

Getting Started

1. S3 Client Quick Start

Initialize SharpS3Client and perform standard S3 operations:

using SharpS3;
using SharpS3.Models;

// 1. Initialize client
var options = new SharpS3ClientOptions
{
    Endpoint = new Uri("http://localhost:8000"),
    AccessKey = "test-access-key",
    SecretKey = "test-secret-key",
    ForcePathStyle = true
};

using ISharpS3Client client = new SharpS3Client(options);

// 2. Create a bucket
await client.CreateBucketAsync("my-bucket");

// 3. Upload an object
await client.PutObjectAsync("my-bucket", "documents/sample.txt", "Hello S3 from SharpS3!");

// 4. Download an object
using var response = await client.GetObjectAsync("my-bucket", "documents/sample.txt");
string content = await response.ReadAsStringAsync();
Console.WriteLine($"Downloaded: {content}");

// 5. Generate a presigned URL (valid for 1 hour)
Uri presignedUrl = client.GeneratePresignedUrl("my-bucket", "documents/sample.txt", HttpMethod.Get, TimeSpan.FromHours(1));
Console.WriteLine($"Presigned URL: {presignedUrl}");

2. S3 Server & Protocol Engine

SharpS3 is transport-agnostic and decoupled from any web server. Receive and dispatch requests in any framework using ISharpS3Server and S3RequestDispatcher:

using SharpS3;
using SharpS3.Dispatch;
using SharpS3.Storage;

// 1. Initialize SharpS3 protocol server and storage backend
ISharpS3Server s3Server = new SharpS3Server();
IS3StorageBackend backend = new FileSystemStorageBackend(@"C:\Data\S3Storage");
var dispatcher = new S3RequestDispatcher(backend);

// 2. Parse incoming HTTP request metadata and body stream
var s3Request = await s3Server.ReceiveRequestAsync(
    httpMethod: "GET",
    path: "/my-bucket/documents/sample.txt",
    queryParameters: queryDictionary,
    headers: headerDictionary,
    bodyStream: requestStream);

// 3. Dispatch to storage backend and obtain response stream
using var s3Response = await dispatcher.DispatchAsync(s3Request);
Console.WriteLine($"Status: {s3Response.StatusCode}, Content-Type: {s3Response.ContentType}");

3. Using Official AWS SDK (AWSSDK.S3)

Any standard AWS SDK client can connect directly to your SharpS3 server:

using Amazon.S3;
using Amazon.S3.Model;

var s3Config = new AmazonS3Config
{
    ServiceURL = "http://localhost:8000",
    ForcePathStyle = true,
    UseHttp = true
};

using var awsClient = new AmazonS3Client("test-key", "test-secret", s3Config);

// List buckets using AWS SDK
var response = await awsClient.ListBucketsAsync();
foreach (var b in response.Buckets)
{
    Console.WriteLine($"Bucket: {b.BucketName}");
}

Storage Backends

SharpS3 separates the S3 wire protocol from persistence through the IS3StorageBackend interface:

Backend Implementation Best Used For
In-Memory InMemoryStorageBackend Unit testing, integration tests, mock services, ephemeral cache
File System FileSystemStorageBackend Local S3 storage, edge appliances, NAS/SAN storage, on-prem S3
Custom IS3StorageBackend Azure Blob, Google Cloud Storage, SQLite, or database backends

Custom Storage Backend Example

Implement IS3StorageBackend to back S3 requests with any custom store:

public class CustomStorageBackend : IS3StorageBackend
{
    public Task<ListAllMyBucketsResult> ListBucketsAsync(IS3Request request, CancellationToken ct = default)
    {
        // Custom bucket listing logic...
    }

    public Task<S3ObjectPayload> ReadObjectAsync(string bucket, string key, IS3Request request, CancellationToken ct = default)
    {
        // Custom stream streaming logic...
    }

    // Implement remaining bucket and object operations...
}

Contributing

Contributions are welcome! Please feel free to submit issues, pull requests, or feature proposals.

License

SharpS3 is provided as-is under the MIT license.