storage

package
v0.1.4 Latest Latest
Warning

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

Go to latest
Published: Aug 13, 2026 License: AGPL-3.0 Imports: 20 Imported by: 0

Documentation

Overview

Package storage defines the object storage interface and provides implementations for local filesystem and S3-compatible object storage.

Index

Constants

This section is empty.

Variables

View Source
var ErrHashMismatch = errors.New("hash mismatch")

ErrHashMismatch is returned when the computed SHA-256 hash of an uploaded object does not match the expected OID.

View Source
var ErrInsufficientStorage = errors.New("insufficient storage")

ErrInsufficientStorage is returned when the filesystem has no space left.

Functions

This section is empty.

Types

type Adapter

type Adapter interface {
	Exists(ctx context.Context, endpoint, oid string) (bool, error)
	Get(ctx context.Context, endpoint, oid string) (io.ReadCloser, int64, error)
	Put(ctx context.Context, endpoint, oid string, reader io.Reader) error
	Size(ctx context.Context, endpoint, oid string) (int64, error)
	AvailableSpace(ctx context.Context) (uint64, error)
}

Adapter is the interface for object storage backends.

All methods accept a context.Context for cancellation. Implementations should check ctx.Err() before starting work. For streaming operations like Put, the context may not be monitored during I/O — callers should not rely on context cancellation to immediately abort an in-progress write. Network-backed implementations (e.g., S3) should propagate the context into their underlying client calls.

Put does not enforce a size limit on the reader. Callers are responsible for wrapping the reader with an appropriate limit (e.g., http.MaxBytesReader) before passing it to Put.

type Local

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

Local stores objects on the local filesystem.

func NewLocal

func NewLocal(basePath string) *Local

NewLocal creates a Local storage adapter rooted at basePath.

func (*Local) AvailableSpace

func (l *Local) AvailableSpace(ctx context.Context) (uint64, error)

AvailableSpace returns the number of bytes available for non-privileged users on the filesystem that contains the storage base path.

func (*Local) Exists

func (l *Local) Exists(ctx context.Context, endpoint, oid string) (bool, error)

Exists reports whether the object exists in storage.

func (*Local) Get

func (l *Local) Get(ctx context.Context, endpoint, oid string) (io.ReadCloser, int64, error)

Get opens the object for reading and returns its size.

func (*Local) Put

func (l *Local) Put(ctx context.Context, endpoint, oid string, reader io.Reader) error

Put streams reader content to storage, verifying the SHA-256 hash matches the OID. The context is not checked during I/O because the reader (typically an http.Request.Body) will return an error when the client disconnects, which naturally terminates the io.Copy.

func (*Local) Size

func (l *Local) Size(ctx context.Context, endpoint, oid string) (int64, error)

Size returns the size of the stored object in bytes.

type PresignedURLProvider

type PresignedURLProvider interface {
	PresignGet(ctx context.Context, endpoint, oid string, expiry time.Duration) (href string, err error)
	PresignPut(ctx context.Context, endpoint, oid string, expiry time.Duration) (href string, err error)
}

PresignedURLProvider is an optional interface that storage adapters can implement to provide presigned URLs for direct client-to-storage transfers. When an adapter implements this interface, the batch handler uses the presigned URLs instead of generating HMAC-signed proxy URLs. The local filesystem adapter does not implement this interface.

type S3

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

S3 stores objects in an S3-compatible bucket. It supports both AWS S3 and Cloudflare R2 through configurable endpoint and path style options.

func NewS3

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

NewS3 creates an S3 storage adapter with the given configuration.

func (*S3) AvailableSpace

func (s *S3) AvailableSpace(_ context.Context) (uint64, error)

AvailableSpace returns math.MaxUint64 because S3 buckets have no practical storage limit.

func (*S3) Exists

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

Exists reports whether the object exists in the S3 bucket.

func (*S3) Get

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

Get opens the object for reading and returns its size.

func (*S3) PresignGet

func (s *S3) PresignGet(ctx context.Context, endpoint, oid string, expiry time.Duration) (string, error)

PresignGet returns a presigned GET URL for direct client downloads.

func (*S3) PresignPut

func (s *S3) PresignPut(ctx context.Context, endpoint, oid string, expiry time.Duration) (string, error)

PresignPut returns a presigned PUT URL for direct client uploads.

func (*S3) Put

func (s *S3) Put(ctx context.Context, endpoint, oid string, reader io.Reader) error

Put streams reader content to S3, verifying the SHA-256 hash matches the OID. If the hash does not match, the uploaded object is deleted and ErrHashMismatch is returned.

func (*S3) Size

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

Size returns the size of the stored object in bytes.

type S3Config

type S3Config struct {
	Bucket          string
	Region          string
	Endpoint        string
	AccessKeyID     string
	SecretAccessKey string
	Prefix          string
	PresignedURLs   bool
	ForcePathStyle  bool
}

S3Config holds the settings for creating an S3 storage adapter.

Jump to

Keyboard shortcuts

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