storage

package
v0.39.0 Latest Latest
Warning

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

Go to latest
Published: Sep 24, 2026 License: Apache-2.0 Imports: 18 Imported by: 0

Documentation

Overview

Package storage provides a pluggable file storage abstraction with local filesystem and S3-compatible backends (AWS S3, RustFS, Cloudflare R2).

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type FileStorage

type FileStorage interface {
	// Save writes the contents of r to the given path, creating intermediate
	// directories as needed. If a file already exists at path it is overwritten.
	Save(ctx context.Context, path string, r io.Reader) error

	// Open returns a ReadCloser for the file at path. The caller is
	// responsible for closing the returned reader.
	Open(ctx context.Context, path string) (io.ReadCloser, error)

	// Delete removes the file at path. It is idempotent — deleting a
	// non-existent file returns nil.
	Delete(ctx context.Context, path string) error

	// Exists reports whether a file exists at path.
	Exists(ctx context.Context, path string) (bool, error)

	// List returns all file paths under the given prefix. Paths are relative
	// to the storage root. An empty prefix lists all files.
	List(ctx context.Context, prefix string) ([]string, error)
}

FileStorage defines the operations every storage backend must support.

type LocalOption

type LocalOption func(*LocalStorage)

LocalOption configures a LocalStorage instance.

func WithLocalLogger

func WithLocalLogger(l *slog.Logger) LocalOption

WithLocalLogger sets the logger used by LocalStorage.

type LocalStorage

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

LocalStorage implements FileStorage on the local filesystem.

func NewLocalStorage

func NewLocalStorage(basePath string, opts ...LocalOption) (*LocalStorage, error)

NewLocalStorage creates a LocalStorage rooted at basePath, creating the directory if it does not exist.

func (*LocalStorage) Delete

func (s *LocalStorage) Delete(_ context.Context, path string) error

func (*LocalStorage) Exists

func (s *LocalStorage) Exists(_ context.Context, path string) (bool, error)

func (*LocalStorage) List

func (s *LocalStorage) List(_ context.Context, prefix string) ([]string, error)

func (*LocalStorage) Open

func (s *LocalStorage) Open(_ context.Context, path string) (io.ReadCloser, error)

func (*LocalStorage) Save

func (s *LocalStorage) Save(_ context.Context, path string, r io.Reader) error

type S3Config

type S3Config struct {
	Endpoint        string // e.g. "http://localhost:9000" for RustFS
	Bucket          string
	Region          string
	AccessKeyID     string
	SecretAccessKey string
	UsePathStyle    bool // true for RustFS / path-style addressing
}

S3Config holds the parameters needed to connect to an S3-compatible service.

type S3Option

type S3Option func(*S3Storage)

S3Option configures an S3Storage instance.

func WithMaxUploadBuffer

func WithMaxUploadBuffer(n int64) S3Option

WithMaxUploadBuffer sets the maximum number of bytes Save will read into memory when buffering a non-seekable body (see Save). Seekable bodies are streamed directly and are unaffected by this limit. A non-positive value leaves the default (64 MiB) in place. Raising this allows larger non-seekable uploads at the cost of higher peak memory per upload.

func WithPublicRead

func WithPublicRead(v bool) S3Option

WithPublicRead controls whether EnsureBucket applies a public-read bucket policy after creating the bucket. Defaults to false.

func WithS3Logger

func WithS3Logger(l *slog.Logger) S3Option

WithS3Logger sets the logger used by S3Storage.

type S3Storage

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

S3Storage implements SignableStorage backed by an S3-compatible service (AWS S3, RustFS, Cloudflare R2).

func NewS3Storage

func NewS3Storage(cfg S3Config, opts ...S3Option) (*S3Storage, error)

NewS3Storage creates an S3Storage connected to the service described by cfg.

func (*S3Storage) Delete

func (s *S3Storage) Delete(ctx context.Context, path string) error

func (*S3Storage) EnsureBucket

func (s *S3Storage) EnsureBucket(ctx context.Context) error

EnsureBucket creates the bucket if it does not already exist. When WithPublicRead(true) is set, it also applies a public-read bucket policy.

func (*S3Storage) Exists

func (s *S3Storage) Exists(ctx context.Context, path string) (bool, error)

func (*S3Storage) List

func (s *S3Storage) List(ctx context.Context, prefix string) ([]string, error)

func (*S3Storage) Open

func (s *S3Storage) Open(ctx context.Context, path string) (io.ReadCloser, error)

func (*S3Storage) Save

func (s *S3Storage) Save(ctx context.Context, path string, r io.Reader) error

func (*S3Storage) SignURL

func (s *S3Storage) SignURL(ctx context.Context, path string, expiry time.Duration, opts ...SignOption) (string, error)

type SignOption

type SignOption func(*signConfig)

SignOption configures a single SignURL call.

func WithAttachment

func WithAttachment(filename string) SignOption

WithAttachment signs the URL so the response carries a Content-Disposition: attachment header with the given filename, forcing browsers to download the file instead of displaying it. An empty filename still forces the download but lets the browser pick the name.

type SignableStorage

type SignableStorage interface {
	FileStorage

	// SignURL returns a pre-signed GET URL for the file at path that
	// expires after the given duration. Options such as WithAttachment
	// alter how the backend serves the response.
	SignURL(ctx context.Context, path string, expiry time.Duration, opts ...SignOption) (string, error)
}

SignableStorage extends FileStorage with the ability to generate pre-signed URLs for direct client downloads.

Jump to

Keyboard shortcuts

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