Skip to content

Storage adapters

The bucket port, the S3 adapter, and what a custom adapter must guarantee.

A bucket is a configured storage adapter plus an access policy. The server never talks to a provider directly, so the same definition can run on S3, in memory, or on a backend you write.

A StorageBucket is created for one bucket name and exposes:

  • put and get for server-side objects;
  • createUploadUrl and createDownloadUrl for presigned requests;
  • head and list for metadata and pagination;
  • remove and copy for lifecycle work;
  • getPublicUrl for a public bucket.

The bucket name is not passed per call. The adapter instance owns it.

Missing objects answer null from get and head, not an error. Failures use a small vocabulary: not_found, forbidden, checksum_mismatch, bucket_missing, unavailable, unknown. Adapters translate their provider’s errors into that vocabulary, and the original error stays reachable through cause.

import { S3StorageBucket, createS3Client } from "@softzweig/suitcase-s3";
const bucket = await S3StorageBucket.create({
name: "suitcase-uploads",
s3: createS3Client({ region: "eu-central-1" }),
});
const { url, method, headers } = await bucket.createUploadUrl({
key: "assets/a.png",
expiresInSeconds: 600,
contentType: "image/png",
contentLength: 3,
checksum: { hashAlgorithm: "sha256", hashHex },
});
await fetch(url, { method, headers, body });

Every returned header is part of the signature. Headers you do not pass in are never signed, so the URL carries no obligation the client cannot fulfil.

The core contract allows sha256 and sha512. The S3 adapter maps them to ChecksumSHA256 and ChecksumSHA512 and rejects anything else at compile time when a new algorithm is added without a mapping.

import { BucketAccessPolicy } from "@softzweig/suitcase-server/storage";
const config = {
access: BucketAccessPolicy.PRIVATE,
storage: bucket,
};

PUBLIC lets reads use getPublicUrl and skips signing. PRIVATE keeps reads behind a signed URL. The policy belongs to the config, not to a kind, so put public content in its own bucket.

The upload bucket also chooses who removes the source object after finalize has copied it into the kind’s bucket. The default is UploadCleanupPolicy.DELETE_AFTER_FINALIZE, which queues uploads.purge:

import {
BucketAccessPolicy,
UploadCleanupPolicy,
type UploadBucketConfig,
} from "@softzweig/suitcase-server/storage";
const upload: UploadBucketConfig = {
access: BucketAccessPolicy.PRIVATE,
cleanupUploads: UploadCleanupPolicy.BUCKET_LIFECYCLE, // opt out of the purge job
storage: uploadBucket,
};

Omit cleanupUploads, or set it to UploadCleanupPolicy.DELETE_AFTER_FINALIZE, to have the server queue uploads.purge after finalize. Set UploadCleanupPolicy.BUCKET_LIFECYCLE only when the bucket has a verified lifecycle rule that expires the upload key prefix. The server then skips the purge job and leaves cleanup to the provider. Lifecycle expiry is asynchronous and provider-specific, so DELETE_AFTER_FINALIZE remains the safer default.

Private S3 reads can reuse signed URLs instead of signing one per resolve. See Cached download URLs for the wrapper, edge caching, and cost tradeoffs.

createMemoryBucketConfig is for development and tests. It is a real bucket implementation in one process: nothing is durable, other instances cannot see its objects, and memory is the only limit. It is useful for a local server or a test that needs real storage behavior without infrastructure.

Keep the provider’s specifics inside the package. An adapter should:

  • own its bucket name and never ask callers for it;
  • translate provider errors into StorageError and keep the cause;
  • return null for absence rather than throwing;
  • echo exactly the headers a presigned request needs;
  • treat checksum as an obligation when the provider supports it;
  • return an opaque cursor from list, not a provider-specific token.

The service layer authorizes and validates. The adapter stores bytes and reports what the provider did.