ByteShelfClient 2.2.0

There is a newer version of this package available.
See the version list below for details.
dotnet add package ByteShelfClient --version 2.2.0
                    
NuGet\Install-Package ByteShelfClient -Version 2.2.0
                    
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="ByteShelfClient" Version="2.2.0" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="ByteShelfClient" Version="2.2.0" />
                    
Directory.Packages.props
<PackageReference Include="ByteShelfClient" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add ByteShelfClient --version 2.2.0
                    
#r "nuget: ByteShelfClient, 2.2.0"
                    
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#:package ByteShelfClient@2.2.0
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=ByteShelfClient&version=2.2.0
                    
Install as a Cake Addin
#tool nuget:?package=ByteShelfClient&version=2.2.0
                    
Install as a Cake Tool

ByteShelfClient

The client for ByteShelf, a self-hosted file store. One interface over a server's shelves, files and users, an HTTP implementation, and an in-memory one so an app's tests need no running server. Targets net8.0 and is AOT-compatible.

Use

HttpClient http = new HttpClient { BaseAddress = new Uri("https://files.example.com/") };

// A program uses one shelf with a key an admin created for it.
IByteShelfClient client = new ByteShelfHttpClient(http, new ShelfKeyCredentials(secret));
IShelfClient receipts = client.Shelf("receipts");

// Typed application/pdf by its extension; UploadOptions.ContentType says otherwise.
ShelfFile stored = await receipts.UploadAsync("/2026/march.pdf", fileStream);
await using Stream content = await receipts.OpenReadAsync(stored.Id);

A person signs in instead, and the session renews itself:

using UserSession session = await UserSession.SignInAsync(http, email, password);
session.Renewed += (_, _) => Save(session.Tokens);   // to resume later with UserSession.Resume(http, saved)
IByteShelfClient client = new ByteShelfHttpClient(http, session);
// ...
await session.SignOutAsync();   // ends it on the server; disposing alone leaves it resumable

IByteShelfClient lists and creates shelves and, with a UserSession, the signed-in user and (for admins) the other users; a shelf key cannot reach users. Admins also drive replication through it: GetReplicationAsync (the replicas and how far behind each is, or the primary followed), CreatePairingCodeAsync, FollowPrimaryAsync, RemoveReplicaAsync and PromoteAsync. IShelfClient works on one shelf: list a directory or its whole tree, search, find duplicates (FindDuplicatesAsync), read what the server read from a file's contents (GetFileMetadataAsync), upload, open (part of a file with OpenReadRangeAsync), describe (many files in one request with SetDescriptionsAsync), share by a link, make an upload link, encrypt directories and swap a file's contents in place (SwapContentAsync), create directories, move (many entries in one request with MoveAsync; a batch applies each item on its own, in order, and answers each one's result), delete, the trash (ListDeletionAsync and OpenDeletedReadAsync read what a deletion took without restoring it, on a replica too; the listing also says what the deletion took and the encrypted folder its files were stored in), and for admins the shelf's settings, grants and keys. ByteShelfClient.Encryption encrypts and decrypts with a password, on the client only. Files and directories are named by path or by id; both convert to an EntryReference.

Uploads

The server needs a file's size before it accepts any bytes. A stream that can seek is sent from its position to its end; any other stream is first copied to a temporary file. Files up to ByteShelfHttpClientOptions.SingleRequestMaxBytes (32 MiB) go in one request, larger ones as a resumable upload in chunks of ChunkBytes (8 MiB). A file with a description always goes as a resumable upload, since a single request has no body but the contents to carry it. A chunk interrupted on the way (the connection broke, the HttpClient's timeout ran out, the server failed with a 5xx, or it holds a different count than the chunk assumed) is sent on from the offset the server reports, up to three times in a row. Any other failure, or a cancellation, aborts the upload, so its reservation against the quota is released at once; the abort gets 10 seconds, and the failure it follows is the one thrown. HttpClient's default timeout of 100 seconds applies to each request; a program that sends large chunks over a slow line can raise it.

Errors

A shelf name, path, content type, description, search, tree query or link lifetime the rules in ByteShelfCommon refuse throws ArgumentException before anything is sent. Anything the server refuses throws ByteShelfException, whose Code is one of ErrorCodes: not_found, conflict, permission_denied, quota_exceeded, range_not_satisfiable and so on. Two codes come from the client rather than the server: a ranged read refuses a file whose contents changed with content_changed, one of ErrorCodes, and a successful answer that is not ByteShelf's (its body, or a ranged read's headers) throws ByteShelfException.InvalidResponse (invalid_response). A failed answer without a code in its body takes its code from its status. IsAccessRefused (unauthenticated, invalid_credentials or permission_denied: the server refused who is asking, or a password or key they gave, and the same credentials get the same answer), IsNotFound and IsInvalidResponse sort the codes a program usually acts on. A request that never reached the server throws what HttpClient throws.

Sessions

A UserSession belongs to the server its HttpClient points at. It renews its access token a minute before it runs out, and once more if the server challenges it. A refresh token works once, so renewals run one at a time, a renewal already sent finishes even when the request that started it is cancelled, and Renewed reports each new token. A renewal the server refuses throws its error: unauthenticated when the session has ended (sign in again), anything else leaves the session as it was. A streamed upload cannot be sent twice, so it relies on the renewal ahead of time. Dispose a session when done with it; the HttpClient stays the caller's.

UserSession.Resume(http, tokens) picks up kept Tokens, using the access token while it works. Processes that share one session open it with UserSession.OpenSharedAsync(http, store) instead: the ISharedSessionStore is locked for every renewal, which first takes up tokens another process renewed to and is then saved there, so no two renew with the same refresh token. Signing a shared session out also happens with the store locked, with the tokens it keeps then, and clears it. The byteshelf CLI keeps its session that way, in its config file. Once signed out, a session's requests fail with unauthenticated without being sent.

Downloads

OpenReadAsync hashes the bytes as they are read, and reading to the end throws IOException if they do not match the file's SHA-256, so a file read whole is the file that was stored.

OpenReadRangeAsync reads part of a file, to seek in a video or answer a browser's Range request: the bytes from an offset, for a length or to the end, answered as a ShelfFileRange with the range's Offset and Length, the file's TotalLength and the Sha256 of the contents it came from. Dispose the range to dispose its stream. A range running past the end is cut at the end; one starting at or past the end is refused with range_not_satisfiable, except offset 0 with no length, which answers an empty file as an empty range. Give it the Sha256 read earlier and a file whose contents have changed since is refused with content_changed rather than answering bytes of other contents. Part of a file cannot be checked against its SHA-256, so only a range that turns out to be the whole file is checked. An encrypted file's range is over its stored age bytes, as OpenReadAsync reads them.

CreateDownloadLinkAsync makes a link to one file that anyone holding it can open without signing in, for a day unless a lifetime in whole minutes up to 7 days is given, or until it is revoked. Making one is a read, so any key may. A server with a public URL configured answers the link on it, and the HTTP client keeps it as it is. Otherwise the server answers a path from its root, which the HTTP client makes absolute against the HttpClient's base address, or against ByteShelfHttpClientOptions.LinkBaseAddress for a client that reaches the server at another address than the people the links are for. The in-memory client's links name the file under the reserved byteshelf.invalid domain, which nothing serves.

CreateUploadLinkAsync makes a link that stores one file at a path when anyone holding it PUTs the contents with a Content-Length (curl -T file '<link>'), for 15 minutes unless UploadLinkOptions give a lifetime up to 24 hours, with an optional description and Overwrite. Making one is a store, so a read key cannot, and one with Overwrite needs write access, so an add-only key cannot either. The link acts with the client's access, checked again when it is used. Its URL is made absolute as a download link's is.

Both kinds of link read or store with the access of whoever made them, checked again on every use, and each answers its Id. GetLinksAsync lists the shelf's links that have not run out, without their URLs: those this client made, or every one when it may write on the shelf. RevokeLinkAsync ends one by its id, under the same rule, so no request made with it from then on is served; a replica keeps and revokes its own links. A link meant to be read once is one with a short lifetime, revoked after it was read. The in-memory client keeps its links per shelf and lists every one, as it acts as an admin.

Encryption

ByteShelfClient.Encryption encrypts with a password as every ByteShelf client does: an age file (c2sp.org/age) with a scrypt recipient, which age -d opens too. AgeEncryption encrypts and decrypts streams; it follows the specification, passes its official test vectors, reads what typage (age in JavaScript, which the web UI depends on) writes and the other way round, and needs the platform's ChaCha20-Poly1305 (IsSupported). New files are encrypted at work factor 18 (DefaultWorkFactor, age's default), and a file is decrypted up to 22 (MaxWorkFactor, age's cap), which takes 4 GiB of memory; past it the file is refused. A failed decryption throws AgeException, whose Failure is NoMatch for a wrong password (or a file not encrypted with one), Header or Payload for a file that is no age file or is damaged. IsOpenedByAsync tells whether a password opens a file from its header alone. Store an encrypted file with UploadOptions.Encrypted. A directory is encrypted with EncryptDirectoryAsync and a check from FolderPassword.CreateCheckAsync; from then on every file stored under it must be encrypted with its password, which FolderPassword.MatchesAsync tells against the check that GetEncryptionAsync or a listing's EncryptedBy answers. Files already there stay as they are until the client encrypts them in place with SwapContentAsync, which takes the file's record as it was read and stores the encrypted (or decrypted) contents in its place: the file keeps its id, and its old contents are gone at once, from replicas too, never to the trash. A file that changed since it was read is refused with a conflict. RemoveDirectoryEncryptionAsync ends the rule and leaves the files encrypted. The server keeps the rule (Storage), and so does the in-memory client, as far as either can see it: a file stored under the directory must be sent as encrypted and start as an age file. Neither ever sees the password, so the client that stores the file is the one that checks it with MatchesAsync. Neither kind of link reaches an encrypted file: making a download link to one, or an upload link under an encrypted directory or over an encrypted file, throws a conflict.

In tests

InMemoryByteShelfClient keeps everything in memory and follows the server's rules for paths, names, conflicts, quotas, the trash and users. It acts as a signed-in admin and does not model access, so nothing is refused for lack of permission. As over HTTP, a refusal or a cancellation comes through the returned task; only an argument the rules refuse may throw from the call itself. The admin is CurrentUser; pass email and password to choose who it is (a test of ChangePasswordAsync needs the password), a TimeProvider to control timestamps and when the trash is purged, and restoreWindow to change the 30 days.

Replication is modelled by its rules and state only: an in-memory client can follow another in the same process with its pairing code, after which it refuses writes until promoted, but a follower copies no data, and it is never behind.

File metadata (GetFileMetadataAsync: when a photo was taken, a PDF's title and so on) is read only by the server, with libraries this package does not carry. In memory every file's metadata is FileMetadata.None, a search criterion about it (TakenFrom, HasGps, durations, page counts) matches no file, and search text matches no title or camera.

InMemoryByteShelfClient client = new InMemoryByteShelfClient(email: "me@example.com", password: "a-long-password");
await client.CreateShelfAsync("receipts");
await client.Shelf("receipts").SetGrantAsync(client.CurrentUser.Id, ShelfAccessPermission.Read);

The server's test suite runs the same contract tests against both clients.

Product Compatible and additional computed target framework versions.
.NET net8.0 is compatible.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed.  net9.0 was computed.  net9.0-android was computed.  net9.0-browser was computed.  net9.0-ios was computed.  net9.0-maccatalyst was computed.  net9.0-macos was computed.  net9.0-tvos was computed.  net9.0-windows was computed.  net10.0 was computed.  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. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages (1)

Showing the top 1 NuGet packages that depend on ByteShelfClient:

Package Downloads
EasyReasy.ByteShelfProvider

Enables resources from ByteShelf for EasyReasy

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
4.0.1 45 11/26/2026
4.0.0 63 11/25/2026
3.1.0 73 11/20/2026
3.0.0 73 11/20/2026
2.2.0 171 11/20/2026
2.1.0 100 11/19/2026
2.0.0 134 11/15/2026
1.3.0 557 10/12/2025
1.2.2 470 7/14/2025
1.2.1 247 7/13/2025
1.2.0 250 7/13/2025
1.1.2 223 7/12/2025
1.1.1 224 7/11/2025
1.1.0 252 7/3/2025
1.0.0 248 6/18/2025

Adds OpenReadRangeAsync to IShelfClient, in the HTTP client and the in-memory one: it reads part of a file, from an offset for a length or to the end, as a ShelfFileRange carrying the range's offset and length, the file's total length and the SHA-256 of the contents it came from, to seek in a video or answer a browser's Range request. A range starting at or past the end is refused with range_not_satisfiable; given the SHA-256 read earlier, a file whose contents have changed is refused with content_changed. Only a range that is the whole file is checked against its SHA-256. Works with ByteShelf server 2.0.0 and later. IShelfClient gains a member, so an implementation outside ByteShelfClient must add it. See https://github.com/AdamTovatt/byte-shelf/blob/client-v2.2.0/ByteShelfClient/README.md