SoftwareDriven.Persistence.S3
3.5.1
dotnet add package SoftwareDriven.Persistence.S3 --version 3.5.1
NuGet\Install-Package SoftwareDriven.Persistence.S3 -Version 3.5.1
<PackageReference Include="SoftwareDriven.Persistence.S3" Version="3.5.1" />
<PackageVersion Include="SoftwareDriven.Persistence.S3" Version="3.5.1" />
<PackageReference Include="SoftwareDriven.Persistence.S3" />
paket add SoftwareDriven.Persistence.S3 --version 3.5.1
#r "nuget: SoftwareDriven.Persistence.S3, 3.5.1"
#:package SoftwareDriven.Persistence.S3@3.5.1
#addin nuget:?package=SoftwareDriven.Persistence.S3&version=3.5.1
#tool nuget:?package=SoftwareDriven.Persistence.S3&version=3.5.1
SoftwareDriven.Persistence.S3
S3-compatible object storage provider for SoftwareDriven.Persistence. Works with AWS S3 and S3-compatible servers such as MinIO. Targets net10.0.
Installation
dotnet add package SoftwareDriven.Persistence.S3
Setup
// Register services
services.AddS3();
services.AddSingleton<S3RepositoryProvider<MyModel>>();
// Connect against AWS S3
var manager = serviceProvider.GetRequiredService<S3Manager>();
await manager.Connect("accessKey", "secretKey", "eu-central-1", "my-bucket");
S3-compatible endpoint (e.g. MinIO)
await manager.ConnectToEndpoint("http://localhost:9000", "minioadmin", "minioadmin", "my-bucket");
A MinIO instance for local development or for running the tests of this repository:
docker run -d --name sd-persistence-minio -p 9000:9000 -e MINIO_ROOT_USER=minioadmin -e MINIO_ROOT_PASSWORD=minioadmin quay.io/minio/minio:latest server /data
The endpoint, the credentials and the bucket the tests use are configured in the S3 section of
SoftwareDriven.Persistence.Test/appsettings.json.
From the configuration
var options = configuration.GetSection("Persistence:Blobs").Get<S3Options>()!;
await s3Manager.Connect(options); // ServiceUrl or Region, AccessKey, SecretKey, Bucket, ForcePathStyle
With a custom endpoint, checksums are sent only where the operation requires them (ChecksumsWhenRequired, default
true): the default aws-chunked uploads of the AWS SDK with trailing checksum are stored including the chunk framing
by servers not supporting them (e.g. SeaweedFS 3.80), silently corrupting the content.
Timeouts, retries and outages
S3Options.Timeout (default 15 s, up to the response headers; a download body is not limited, an upload body is),
MaxErrorRetry (default 2) and RetryMode (default Standard) set the client of Connect(S3Options),
Connect(...) and ConnectToEndpoint(...) (before 3.5.1: SDK defaults, 100 s and more retries).
S3Manager.CreateConfig(options) gives the configuration for a client of one's own.
Uploads (S3FileProvider.Store, StoreFile; a single PutObject, no multipart upload) wait for
S3Options.UploadTimeout instead (default 10 min, the body included: S3 answers a PUT only after the whole body).
Connect(S3Options) sets up a second client for them (S3Manager.UploadClient) when it differs from Timeout, so a
short timeout for reading does not cut off a large upload; S3Manager.CreateUploadConfig(options) gives its
configuration. Connect(client, uploadClient, bucket) takes both clients pre-configured.
With ThrowOnUnavailable (default false) S3FileProvider.OpenRead, GetInfo, LoadFile and FileExists throw
BlobStoreUnavailableException on a timeout, a 5xx/408/429, a network error or an abort not requested by the
caller; a missing object stays null or false. Without it OpenRead, GetInfo and LoadFile return null, as before.
S3Manager.CheckAvailability(timeout) sends HeadBucket and returns an S3ProbeResult (Available,
StatusCode, Error, Duration) instead of throwing, e.g. for a health check or to trip the circuit of a
FailoverBlobProvider.
Pre-configured client
await manager.Connect(myAmazonS3Client, "my-bucket");
Multi-instance (keyed DI)
services.AddS3("InstanceA");
services.AddS3("InstanceB");
var managerA = serviceProvider.GetRequiredKeyedService<S3Manager>("InstanceA");
var managerB = serviceProvider.GetRequiredKeyedService<S3Manager>("InstanceB");
Usage
var provider = serviceProvider.GetRequiredService<S3RepositoryProvider<MyModel>>();
// CRUD
await provider.CreateOrUpdate(new MyModel { Title = "Hello" });
var item = await provider.FindById("some-id");
var results = await provider.FindByExpression(x => x.Title == "Hello");
await provider.Delete("some-id");
// Files
await provider.StoreFile("logo.png", bytes);
var data = await provider.LoadFile("logo.png");
// Pagination
var page = await provider.GetAll(new PaginationOptions<MyModel>
{
PageSize = 10,
PageNumber = 0
});
Storage layout
Each model type is stored as a single JSON object in the bucket under
{BasePath}/{TableName}.json (default BasePath is data). Files stored via
IFileProvider are kept as individual objects under {BasePath}/files/{FileFolder}/{fileId}.
S3 as file backend of other providers
S3FileProvider implements IFileProvider on its own and can serve as the file backend of any other
persistence provider, so that the models stay in their database while the files go to a bucket:
services.AddMongoDb();
services.AddSingleton<MongoDbRepositoryProvider<MyModel>>();
services.AddS3("Files"); // the S3Manager holding the bucket
services.AddS3FileStorage(managerKey: "Files"); // files of all providers go to that bucket
Restrict the file backend to the providers of one service key by passing the instanceKey:
services.AddS3FileStorage("InstanceA", managerKey: "Files");
S3FileProvider implements IBlobProvider: files are streamed, byte ranges are requested from the server, the
content type is kept and the files of a folder can be listed. Streams that cannot seek are buffered for the upload.
Alternatively the provider of a single instance can be exchanged at runtime:
provider.FileProvider = new S3FileProvider(s3Manager, logger) { BasePath = "data" };
Queries are executed in memory after loading the collection object. This keeps the full
IPersistenceProvider<T> contract (expression queries, projections, element match) available,
but is best suited for small to medium collections. Read-modify-write on a collection is not
atomic across concurrent writers.
Features
SupportsTotalCount: trueSupportsPagingState: false (offset-based pagination)DefaultIdSource:GuidSourceSupportsMaintenance: false (a deleted object is gone; there is nothing to compact)- Full
IPersistenceProvider<T>support including nested collection projections and element match queries
License
MIT
| Product | Versions Compatible and additional computed target framework versions. |
|---|---|
| .NET | net10.0 is compatible. net10.0-android was computed. net10.0-browser was computed. net10.0-ios was computed. net10.0-maccatalyst was computed. net10.0-macos was computed. net10.0-tvos was computed. net10.0-windows was computed. |
-
net10.0
- AWSSDK.S3 (>= 4.0.100.1)
- SoftwareDriven.Persistence (>= 3.5.1)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.