s3

package
v0.2.0 Latest Latest
Warning

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

Go to latest
Published: Nov 4, 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.

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