storage

package
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Jul 3, 2026 License: MIT Imports: 14 Imported by: 0

Documentation

Overview

Package storage provides a unified file storage abstraction with local-disk and S3-compatible (AWS S3, MinIO, Wasabi, R2) drivers.

Index

Constants

This section is empty.

Variables

View Source
var ErrSignedURLUnsupported = errors.New("storage: signed URLs are not supported by this driver")

ErrSignedURLUnsupported is returned by drivers that cannot produce pre-signed URLs.

Functions

This section is empty.

Types

type Disk

type Disk interface {
	// Put writes r to the given path, creating directories as needed.
	Put(ctx context.Context, path string, r io.Reader, opts ...PutOptions) error
	// Get returns a reader for the object at path.
	Get(ctx context.Context, path string) (io.ReadCloser, error)
	// Delete removes the object at path.
	Delete(ctx context.Context, path string) error
	// Exists reports whether an object exists at path.
	Exists(ctx context.Context, path string) (bool, error)
	// URL returns the public URL for path. For signed URLs, use SignedURL.
	URL(path string) string
	// SignedURL returns a pre-signed URL valid for ttl.
	SignedURL(ctx context.Context, path string, ttl time.Duration) (string, error)
	// List returns the keys under prefix.
	List(ctx context.Context, prefix string) ([]string, error)
	// Size returns the byte size of the object at path.
	Size(ctx context.Context, path string) (int64, error)
	// Copy duplicates src to dst within the same disk.
	Copy(ctx context.Context, src, dst string) error
	// Move renames src to dst within the same disk.
	Move(ctx context.Context, src, dst string) error
}

Disk is the interface every storage driver must implement.

type Local

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

Local is a local-disk storage driver.

func NewLocal

func NewLocal(root, baseURL string) (*Local, error)

NewLocal creates a local disk driver.

disk := storage.NewLocal("/var/app/storage", "https://example.com/storage")

func (*Local) Copy

func (l *Local) Copy(ctx context.Context, src, dst string) error

func (*Local) Delete

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

func (*Local) Exists

func (l *Local) Exists(_ context.Context, path string) (bool, error)

func (*Local) Get

func (l *Local) Get(_ context.Context, path string) (io.ReadCloser, error)

func (*Local) List

func (l *Local) List(_ context.Context, prefix string) ([]string, error)

func (*Local) Move

func (l *Local) Move(ctx context.Context, src, dst string) error

func (*Local) Put

func (l *Local) Put(_ context.Context, path string, r io.Reader, _ ...PutOptions) error

func (*Local) SignedURL

func (l *Local) SignedURL(_ context.Context, _ string, _ time.Duration) (string, error)

SignedURL is not supported for local disk. It returns ErrSignedURLUnsupported rather than silently handing back a permanent, unsigned URL that callers would mistakenly treat as access-controlled.

func (*Local) Size

func (l *Local) Size(_ context.Context, path string) (int64, error)

func (*Local) URL

func (l *Local) URL(path string) string

type Manager

type Manager struct {
	Default string
	// contains filtered or unexported fields
}

Manager holds named disk instances.

func NewManager

func NewManager(defaultDisk string, disks map[string]Disk) *Manager

NewManager creates a Manager with the given named disks.

func (*Manager) Delete

func (m *Manager) Delete(ctx context.Context, path string) error

Delete deletes from the default disk.

func (*Manager) Disk

func (m *Manager) Disk(name string) Disk

Disk returns a named disk. It panics if the disk is not configured — use it only with names you registered at startup; prefer DiskErr when the name comes from config or user input.

func (*Manager) DiskErr

func (m *Manager) DiskErr(name string) (Disk, error)

DiskErr returns a named disk, or an error if no disk is registered under that name. This is the non-panicking counterpart of Disk.

func (*Manager) Get

func (m *Manager) Get(ctx context.Context, path string) (io.ReadCloser, error)

Get reads from the default disk.

func (*Manager) Put

func (m *Manager) Put(ctx context.Context, path string, r io.Reader, opts ...PutOptions) error

Put writes to the default disk.

func (*Manager) URL

func (m *Manager) URL(path string) string

URL returns the public URL for a path on the default disk.

type PutOptions

type PutOptions struct {
	ContentType string
	ACL         string // e.g. "public-read", "private"
	Metadata    map[string]string
}

PutOptions holds optional metadata for a Put operation.

type S3

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

S3 is an S3-compatible storage driver.

func NewS3

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

NewS3 creates an S3 driver.

func (*S3) Copy

func (s *S3) Copy(ctx context.Context, src, dst string) error

func (*S3) Delete

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

func (*S3) Exists

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

func (*S3) Get

func (s *S3) Get(ctx context.Context, path string) (io.ReadCloser, error)

func (*S3) List

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

func (*S3) Move

func (s *S3) Move(ctx context.Context, src, dst string) error

func (*S3) Put

func (s *S3) Put(ctx context.Context, path string, r io.Reader, opts ...PutOptions) error

func (*S3) SignedURL

func (s *S3) SignedURL(ctx context.Context, path string, ttl time.Duration) (string, error)

func (*S3) Size

func (s *S3) Size(ctx context.Context, path string) (int64, error)

func (*S3) URL

func (s *S3) URL(path string) string

type S3Config

type S3Config struct {
	Bucket          string
	Region          string
	AccessKeyID     string
	SecretAccessKey string
	// Endpoint is the base URL for S3-compatible services (MinIO, Wasabi, R2).
	// Leave empty for AWS S3.
	Endpoint string
	// BaseURL is the public CDN/bucket URL used by URL(). If empty, the S3 URL is used.
	BaseURL string
	// DefaultACL is the default ACL for new objects (default: "private").
	DefaultACL string
	// ForcePathStyle enables path-style addressing (required by MinIO).
	ForcePathStyle bool
}

S3Config holds credentials and settings for an S3-compatible object store.

Jump to

Keyboard shortcuts

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