Documentation
¶
Overview ¶
Package storage provides a pluggable file storage abstraction with local filesystem and S3-compatible backends (AWS S3, RustFS, Cloudflare R2).
Index ¶
- type FileStorage
- type LocalOption
- type LocalStorage
- func (s *LocalStorage) Delete(_ context.Context, path string) error
- func (s *LocalStorage) Exists(_ context.Context, path string) (bool, error)
- func (s *LocalStorage) List(_ context.Context, prefix string) ([]string, error)
- func (s *LocalStorage) Open(_ context.Context, path string) (io.ReadCloser, error)
- func (s *LocalStorage) Save(_ context.Context, path string, r io.Reader) error
- type S3Config
- type S3Option
- type S3Storage
- func (s *S3Storage) Delete(ctx context.Context, path string) error
- func (s *S3Storage) EnsureBucket(ctx context.Context) error
- func (s *S3Storage) Exists(ctx context.Context, path string) (bool, error)
- func (s *S3Storage) List(ctx context.Context, prefix string) ([]string, error)
- func (s *S3Storage) Open(ctx context.Context, path string) (io.ReadCloser, error)
- func (s *S3Storage) Save(ctx context.Context, path string, r io.Reader) error
- func (s *S3Storage) SignURL(ctx context.Context, path string, expiry time.Duration, opts ...SignOption) (string, error)
- type SignOption
- type SignableStorage
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) Open ¶
func (s *LocalStorage) Open(_ context.Context, path string) (io.ReadCloser, 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 ¶
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 ¶
WithPublicRead controls whether EnsureBucket applies a public-read bucket policy after creating the bucket. Defaults to false.
func WithS3Logger ¶
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 ¶
NewS3Storage creates an S3Storage connected to the service described by cfg.
func (*S3Storage) EnsureBucket ¶
EnsureBucket creates the bucket if it does not already exist. When WithPublicRead(true) is set, it also applies a public-read bucket policy.
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.