blobstore

package
v1.8.3 Latest Latest
Warning

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

Go to latest
Published: Nov 23, 2026 License: AGPL-3.0 Imports: 23 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package blobstore keeps the files ReputeCast stores outside the database (digital products' files, lead magnets, chunked uploads, audience exports) in the local files folder or in an S3-compatible bucket (storage.backend). Both backends name objects with the same keys, so moving from one to the other is a copy (`reputecast storage migrate`).

Index

Constants

View Source
const MaxKeyBytes = 900

MaxKeyBytes is the longest key (S3's limit, less room for a prefix).

Variables

View Source
var ErrNotExist = fs.ErrNotExist

ErrNotExist is what every backend returns for a key that holds no object (errors.Is(err, fs.ErrNotExist) holds too).

View Source
var ErrSize = errors.New("blobstore: the content's length isn't the size given")

ErrSize is a Put whose reader holds more or fewer bytes than its size.

Functions

func NewS3Client

func NewS3Client(ctx context.Context, c config.S3Config) (*s3.Client, error)

NewS3Client builds the S3 client for a bucket's settings: the endpoint and path style of an S3-compatible service, the region, and static keys or else the default AWS credential chain (environment, shared files, an IAM role, IRSA).

func ValidKey

func ValidKey(key string) error

ValidKey checks a key is one every backend stores the same way: slash separated, relative, without empty, "." or ".." segments, backslashes or control characters.

Types

type CopyOptions

type CopyOptions struct {
	// DryRun counts what would be copied and copies nothing.
	DryRun bool
	// Verify reads every copy back and compares its SHA-256 with the
	// source's; a copy that differs is deleted and counts as failed.
	Verify bool
	// Workers copy side by side (0: 8).
	Workers int
	// Failed hears of each object that couldn't be copied (it may be
	// called from several goroutines at once).
	Failed func(key string, err error)
}

CopyOptions says how Copy copies.

type CopyStats

type CopyStats struct {
	Objects, Bytes      int64
	Copied, CopiedBytes int64
	Skipped, Failed     int64
}

CopyStats counts a Copy: every object of the source and its bytes; those copied (or, in a dry run, to copy), those the destination holds already at the same size, and those that failed.

func Copy

func Copy(ctx context.Context, dst, src Store, opts CopyOptions) (CopyStats, error)

Copy copies every object of src into dst under the same key, skipping those dst holds already at the same size. It never deletes from src. The returned error is the walk's (a source that can't be listed, a context that ended); objects that fail are counted and reported to opts.Failed.

type File

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

File reads a stored object, or several one after the other (an upload's chunks), as one file: in order (io.Reader), from anywhere (io.Seeker, for http.ServeContent's ranges) and at any offset (io.ReaderAt, for an archive's directory). Reading in order holds one ranged request open from where it began; Seek costs nothing until the next Read.

func Join

func Join(ctx context.Context, s Store, parts []Part) *File

Join reads parts one after the other as one File of their total size.

func OpenFile

func OpenFile(ctx context.Context, s Store, key string) (*File, error)

OpenFile opens an object to read as a File (its size and time from Stat, so a missing key fails here, before anything is sent).

func (*File) Close

func (f *File) Close() error

Close ends the open request; the File reads nothing more.

func (*File) ModTime

func (f *File) ModTime() time.Time

ModTime is when the object was written (OpenFile only).

func (*File) Read

func (f *File) Read(p []byte) (int, error)

func (*File) ReadAt

func (f *File) ReadAt(p []byte, off int64) (int, error)

ReadAt reads len(p) bytes at off. It doesn't move Read's position, and several ReadAt calls may run at once.

func (*File) Seek

func (f *File) Seek(offset int64, whence int) (int64, error)

func (*File) Size

func (f *File) Size() int64

Size is the file's length in bytes.

type Info

type Info struct {
	Size    int64
	ModTime time.Time
}

Info describes a stored object.

type Local

type Local struct {
	Dir string
}

Local keeps objects as files under a folder (files.dir), a key's slashes its subfolders: the layout ReputeCast has always used, so a folder written before object storage existed reads as it is.

func (Local) Delete

func (l Local) Delete(_ context.Context, key string) error

Delete removes a key's file and the folders it leaves empty (an upload's, a workspace's), never the files folder itself.

func (Local) List

func (l Local) List(ctx context.Context, prefix string, fn func(key string, info Info) error) error

List walks the folders the prefix reaches into; files being written aren't listed.

func (Local) Open

func (l Local) Open(_ context.Context, key string) (io.ReadCloser, error)

func (Local) OpenRange

func (l Local) OpenRange(_ context.Context, key string, off, n int64) (io.ReadCloser, error)

func (Local) Put

func (l Local) Put(ctx context.Context, key string, r io.Reader, size int64, _ string) error

Put writes to a temporary file next to the key's and renames it into place once it is complete and on disk.

func (Local) Stat

func (l Local) Stat(_ context.Context, key string) (Info, error)

type Part

type Part struct {
	Key  string
	Size int64
}

Part is one object of a File made of several, in order.

type S3

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

S3 keeps objects in an S3-compatible bucket (AWS S3, MinIO, Cloudflare R2, DigitalOcean Spaces, Backblaze B2, Wasabi…), under the configured prefix. Large objects and objects of unknown size go up in parts.

func NewS3

func NewS3(ctx context.Context, c config.S3Config) (*S3, error)

NewS3 opens the bucket storage.s3 names.

func (*S3) Bucket

func (s *S3) Bucket() string

Bucket is the bucket's name.

func (*S3) Check

func (s *S3) Check(ctx context.Context) error

Check asks the bucket whether it exists and these credentials reach it.

func (*S3) Delete

func (s *S3) Delete(ctx context.Context, key string) error

func (*S3) List

func (s *S3) List(ctx context.Context, prefix string, fn func(key string, info Info) error) error

func (*S3) Open

func (s *S3) Open(ctx context.Context, key string) (io.ReadCloser, error)

func (*S3) OpenRange

func (s *S3) OpenRange(ctx context.Context, key string, off, n int64) (io.ReadCloser, error)

func (*S3) Prefix

func (s *S3) Prefix() string

Prefix is what every key is put under ("" or ending in "/").

func (*S3) Put

func (s *S3) Put(ctx context.Context, key string, r io.Reader, size int64, contentType string) error

Put uploads in one request up to 16 MB, in 8 MB parts beyond (and for a reader of unknown size past its first 16 MB); a failed multipart upload is aborted, so nothing is left behind.

func (*S3) Stat

func (s *S3) Stat(ctx context.Context, key string) (Info, error)

type Store

type Store interface {
	// Put stores r's bytes under key, replacing what was there. size is
	// r's length, or -1 when it isn't known (a reader that ends early or
	// runs past size fails the Put, which then stores nothing). A Put is
	// atomic: readers see the old object or the new one, never a part.
	Put(ctx context.Context, key string, r io.Reader, size int64, contentType string) error
	// Open reads an object from its start.
	Open(ctx context.Context, key string) (io.ReadCloser, error)
	// OpenRange reads at most n bytes from off (n < 0: to the end). A
	// range past the end reads nothing.
	OpenRange(ctx context.Context, key string, off, n int64) (io.ReadCloser, error)
	// Stat reports an object's size and when it was written.
	Stat(ctx context.Context, key string) (Info, error)
	// Delete removes an object; a key that doesn't exist isn't an error.
	Delete(ctx context.Context, key string) error
	// List calls fn for every object whose key starts with prefix, in no
	// particular order; fn's error stops the walk and is returned.
	List(ctx context.Context, prefix string, fn func(key string, info Info) error) error
}

Store keeps objects under slash-separated keys ("<workspace>/ab/<sha256>", "uploads/<id>/000000000000").

func Open

func Open(ctx context.Context, cfg *config.Config) (Store, error)

Open builds the store the configuration names: the files folder (storage.backend local) or the bucket (s3).

Directories

Path Synopsis
Package blobstoretest is the contract every blobstore backend passes, and the test bucket integration tests use.
Package blobstoretest is the contract every blobstore backend passes, and the test bucket integration tests use.

Jump to

Keyboard shortcuts

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