s3

package
v0.3.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 16, 2026 License: MIT Imports: 19 Imported by: 0

Documentation

Overview

Package s3 is the S3-compatible blob store (AWS S3, MinIO, Ceph RGW and similar). Blobs live at <prefix>sha256/<ab>/<hex>.

Object stores cannot append or rename, so an upload session is shaped around those limits:

  • Bytes accumulate in a pending buffer of at most PartSize. A blob that never fills one part is written with a single PutObject at commit; most package files are that small, and they never pay for a multipart upload.
  • When the buffer fills, the session starts an S3 multipart upload (on the first full part) and uploads the part. S3 requires every part except the last to be at least 5 MiB, which is why appends are buffered rather than uploaded as they arrive: Docker clients PATCH in chunks of any size.
  • The multipart upload ID, the part list, the pending tail and the hash state are saved through a session.Store after every append, so a session resumes in a later request (decision 0027; session.InUploads keeps it in the metadata database).
  • At commit the content key is known. If the client declared the digest up front the multipart upload already targets the content key; otherwise it targets a temporary key and is copied into place, with a multipart copy above S3's 5 GiB single-copy limit.
  • Every write of a content key is a must-not-exist write (If-None-Match: *, decision 0027), as the GCS and Azure backends write, so a blob that is already stored is never written again and two uploads of one blob cannot race. S3 answers 412 for a taken key, and the existing object is adopted. There is no HeadObject before the write.

Role

platform layer, adapter: the backend for one object store, an S3 bucket, implementing platform/blob's Store. Blob storage belongs to no module, so the backends sit in the platform below the blob port. The composition root builds one from the blob.s3 configuration for the hot or the cold tier, or for an s3 member of a blob mirror. Tests outside the package build it on MinIO: the mirror's cross-cloud scenario, the composition root's runtime test and the CloudFront origin of the CDN acceptance feature. It imports platform/blob, platform/blob/session, platform/cdn, the standard library and the AWS SDK for Go v2.

Inputs

  • ClientConfig, which mirrors the blob.s3 section of the configuration: the endpoint (empty for AWS), the region, path-style addressing, and static keys or a profile of the shared AWS configuration, otherwise the AWS default chain. NewClient builds the SDK client and its presigner from it without a request.
  • Options for New: the bucket, which must exist; the key prefix; the part size, DefaultPartSize when zero and refused below S3's 5 MiB; the session.Store that keeps sessions between requests (the composition root passes session.InUploads under "s3:<tier>", such as "s3:blob", or "s3:<tier>.<member>" for a mirror member); the presigner, with which the store signs storage URLs; and a cdn.Signer, such as a cdn.CloudFront, with which it signs CDN URLs.
  • What platform/blob's Store and Upload take: upload options, the bytes appended, declared digests, upload IDs, the SHA-256 of a stored blob and, to sign a URL, its blob.SignOptions.

Outputs

  • Store, from New: a blob.Store that is also a blob.Signer, of storage URLs when it has a presigner and of CDN URLs when it has a CDN signer, and a blob.PartSizer. ObjectKey gives the key a CDN signer addresses.
  • API and PresignAPI, the parts of the SDK's client and presign client that the store calls, so tests substitute a fake.
  • NewClient and ClientConfig, DefaultPartSize, ErrConfig, which every configuration error wraps, and ErrSeek.

Invariants

  • A blob is stored under its lowercase hex SHA-256, and an object reaches that key only after Commit has verified the digest. A taken content key is adopted only when the stored object has the size that was written; any other size fails the commit and leaves the object alone.
  • The condition saves work and races but never guards correctness, because a write over a content key stores the same bytes: a store that ignores it (MinIO, on CopyObject) writes again, and a store that refuses it as NotImplemented is written once more without it and is not asked again.
  • Parts are buffered through platform/blob/session, and the session is saved after every append. Its document holds the multipart upload's key and ID and each part's number and ETag; the json tags of multipart and part are the storage format, because a session saved by one process is resumed by another.
  • A missing key is blob.ErrNotFound, and a finished or aborted multipart upload is blob.ErrUploadUnknown.
  • A store with a CDN signer answers blob.ViaCDN itself and is never wrapped, so it keeps PartSizer (decision 0029); New refuses a signer whose origin path does not contain the prefix. A presigned storage URL lasts at most 7 days, SigV4's limit.
  • A reader opens the object only when read and again after a Seek, so the seek-to-end probe of http.ServeContent costs no request and a ranged read costs one.
  • Errors the store creates start "blob/s3:". Those of platform/blob and platform/blob/session pass through unwrapped, because their texts already name their package. Every text stays byte-identical.

References

  • Decision 0004: docs/research/decisions/0004-storage-sql-metadata-and-blobs.md ("Blobs": content addressing and upload sessions that resume across requests).
  • Decision 0027: docs/research/decisions/0027-cloud-blob-backends.md ("Shared session layer", and "s3: must-not-exist writes").
  • Decision 0028: docs/research/decisions/0028-blob-mirroring.md (an s3 member of a mirror, whose chunks follow the member's part size).
  • Decision 0029: docs/research/decisions/0029-cdn-delivery-and-download-redirect.md ("Signing contract": presigned storage URLs and CloudFront).
  • Decision 0030: docs/research/decisions/0030-reference-deployments.md (the aws/single-bucket scenario that the real-cloud run uses, and its conformance/ prefix).
  • Research 18, section 4, "S3 answers (2026-10-16)": docs/research/18-cloud-blob-storage.md.
  • The cloud conformance workflow, .github/workflows/cloud-conformance.yml (job s3), and docs/testing/ci.md#real-cloud-conformance-manual.
  • Decision 0044: docs/research/decisions/0044-modular-monolith-migration.md, "Dependency rules": the platform imports no module.
  • Target layout: docs/research/architecture-options/5-modular-monolith.md, line 147 (platform/blob/s3) and line 1049 (the mapping row).
  • Plan: docs/implementation/plan.md#p2-stores, task 3, #p2b-cloud-blob-storage-and-mirroring, #p4b-reference-deployments-and-cdn-delivery and #p9d-modular-monolith-migration, epic PLT (https://github.com/weaveplatform/artifactweave/issues/938).
  • https://github.com/weaveplatform/artifactweave/issues/983

Status

Implemented P2: the store, with multipart uploads whose sessions are kept in the uploads table, path-style addressing for MinIO and presigned GETs (pull request 3). P2b moved its sessions onto the shared session layer (issue 8, pull request 30) and added PartSize, by which the mirror sizes its chunks (issue 39). P3 added the shared-configuration profile, so stores in one process can use different accounts (issue 42). P4b replaced the presigned GET with blob.Signer, whose storage URLs carry the file name and media type (issue 119), added CloudFront URLs through a cdn.Signer (issue 120) and the real-cloud conformance run (issue 128). On 2026-10-16 must-not-exist writes replaced the HeadObject before each write (issue 255, pull request 312). Moved here in P9d.

Testing

  • TestAccContractOnFake and TestAccContractWithDurableSessions: the blobtest contract on an in-memory fake of S3 with 256 KiB parts, the second with its sessions kept in the uploads table of the platform's in-memory store.
  • TestAccContractOnMinIO: the contract on MinIO with S3's 5 MiB part floor, so multipart uploads, the copy into place and presigned GETs run end to end. TestAccConcurrentCommitsOnMinIO: sessions commit one blob at once on each write path, all succeed and the blob reads back whole. Both need a Docker engine that runs Linux containers and skip without one.
  • TestAccNewClientBuildsWithoutNetwork: NewClient applies its configuration, and the store presigns, without a request.
  • TestUnitCommitWritesConditionallyWithoutAHead, TestUnitCommitAdoptsAStoredBlob, TestUnitCommitAdoptsItsOwnLandedRetry, TestUnitCommitRefusesAStoredObjectOfAnotherSize, TestUnitCommitAdoptCheckFails, TestUnitCommitWithoutConditionalSupport, TestUnitCommitWhereTheConditionIsIgnored and TestUnitConcurrentCommitsOfOneBlob: the must-not-exist writes, each outcome of a taken key, and stores that ignore or refuse the condition.
  • TestUnitKeysAreContentAddressedUnderPrefix, TestUnitDeclaredDigestSkipsTheCopy and TestUnitMultipartCopyAboveTheCopyLimit: the key layout, a small blob as one PutObject, parts on the content key when the digest is declared, and the multipart copy above the single-copy limit.
  • TestUnitNewValidates, TestUnitAppendFailures, TestUnitCommitFailures, TestUnitResumeWithDamagedState, TestUnitReadErrors, TestUnitDeleteWalkPresignErrors, TestUnitTranslate and TestUnitCDNSigning: configuration refused with ErrConfig, each SDK call failing through the fake, a damaged session ending as blob.ErrUploadUnknown, the mapping onto the blob sentinels, and CDN URLs for the prefixed key.
  • TestAccContractOnRealS3 and TestAccRealS3Probes: the contract on a real bucket, with presigned URLs fetched over HTTPS and the copy limits lowered so the multipart copy runs, and the probes, which log what S3 answers as PROBE lines. They skip unless ARTIFACTWEAVE_TEST_S3_BUCKET is set. The cloud conformance workflow dispatches them, and make conformance in the aws/single-bucket scenario runs them locally.

Index

Constants

View Source
const (
	// DefaultPartSize balances request count against the memory one session
	// holds for its pending tail.
	DefaultPartSize = 8 << 20
)

Variables

View Source
var ErrConfig = errors.New("blob/s3: invalid configuration")

ErrConfig reports an unusable store configuration.

View Source
var ErrSeek = errors.New("blob/s3: invalid seek")

ErrSeek reports an invalid Seek.

Functions

func NewClient

func NewClient(ctx context.Context, cfg ClientConfig) (*s3.Client, *s3.PresignClient, error)

NewClient builds the S3 client and presigner for cfg.

Types

type API

API is the subset of *s3.Client the store uses, so tests can substitute a fake that injects failures.

type ClientConfig

type ClientConfig struct {
	// Endpoint overrides the AWS endpoint, for MinIO and other S3-compatible
	// stores, e.g. "http://minio:9000". Empty means AWS.
	Endpoint string
	// Region is required by the signer even for stores that ignore it.
	Region string
	// PathStyle addresses buckets as <endpoint>/<bucket> instead of
	// <bucket>.<endpoint>; MinIO and most self-hosted stores need it.
	PathStyle bool
	// AccessKey and SecretKey are static credentials. Both empty means the
	// AWS default chain (environment, shared config, instance role).
	AccessKey string
	SecretKey string
	// Profile selects a named profile of the shared AWS configuration, so
	// stores in one process can use different accounts. Empty means the
	// default profile (or AWS_PROFILE).
	Profile string
}

ClientConfig mirrors the blob.s3 section of the application configuration.

type Options

type Options struct {
	// Bucket must already exist.
	Bucket string
	// Prefix is prepended to every key; a trailing slash is added if missing.
	Prefix string
	// PartSize is the multipart part size; zero means DefaultPartSize. S3
	// rejects parts under 5 MiB, so smaller values are refused.
	PartSize int64
	// Sessions holds upload sessions between requests; session.InUploads
	// with a backend name such as "s3:<store name>" keeps them durable.
	Sessions session.Store
	// Presign signs storage URLs when set (blob.Signer); NewClient always
	// returns one.
	Presign PresignAPI
	// CDN signs CDN URLs when set, such as a cdn.CloudFront in front of the
	// bucket (decision 0029).
	CDN cdn.Signer
}

Options configures New.

type PresignAPI

type PresignAPI interface {
	PresignGetObject(
		context.Context, *s3.GetObjectInput, ...func(*s3.PresignOptions),
	) (*v4.PresignedHTTPRequest, error)
}

PresignAPI is the subset of *s3.PresignClient the store uses.

type Store

type Store struct {
	// contains filtered or unexported fields
}

Store is a blob.Store on an S3 bucket.

func New

func New(api API, o Options) (*Store, error)

New returns a store over api.

func (*Store) BeginUpload

func (s *Store) BeginUpload(ctx context.Context, opts ...blob.UploadOption) (blob.Upload, error)

BeginUpload starts a session.

func (*Store) Delete

func (s *Store) Delete(ctx context.Context, sha string) error

Delete removes the blob; S3 deletes of missing keys already succeed.

func (*Store) Exists

func (s *Store) Exists(ctx context.Context, sha string) (bool, error)

Exists reports whether the blob is stored.

func (*Store) ObjectKey

func (s *Store) ObjectKey(sha string) string

ObjectKey is the key a blob is stored under, prefix included, for signers that address the bucket directly, such as a CDN. sha must be a valid digest (blob.ValidateSHA256).

func (*Store) Open

func (s *Store) Open(ctx context.Context, sha string) (io.ReadSeekCloser, blob.Info, error)

Open returns a reader that fetches ranges on demand.

func (*Store) PartSize

func (s *Store) PartSize() int64

PartSize returns the size of the parts an upload is written in.

func (*Store) ResumeUpload

func (s *Store) ResumeUpload(ctx context.Context, id string) (blob.Upload, error)

ResumeUpload reopens a session.

func (*Store) SignURL

func (s *Store) SignURL(ctx context.Context, sha string, o blob.SignOptions) (string, error)

SignURL returns a time-limited GET URL, carrying o's filename and media type as response overrides.

func (*Store) Signs

func (s *Store) Signs(via blob.Via) bool

Signs reports whether the store signs URLs for via: storage URLs when it has a presign client, CDN URLs when it has a CDN signer.

func (*Store) Stat

func (s *Store) Stat(ctx context.Context, sha string) (blob.Info, error)

Stat returns a blob's size and digest.

func (*Store) Walk

func (s *Store) Walk(ctx context.Context, fn func(blob.Info) error) error

Walk lists every blob under the prefix.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL