storage

package
v0.6.1 Latest Latest
Warning

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

Go to latest
Published: Nov 11, 2026 License: MIT Imports: 18 Imported by: ? 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 storage provides a multi-driver object storage facade configured from the config package.

Role in architecture:

  • Infrastructure facade: exposes one process-wide storage driver and bucket for object read/write.

Responsibilities:

  • Setup / SetupContext: create a storage driver from config.Storage; empty STORAGE_DRIVER disables storage.
  • GetBucketName: access the configured default bucket name.
  • ReadObject, ReadObjectAsReader, WriteObject, DeleteObject, ObjectExists, ListObjects, PresignUpload: delegate to the configured driver; ctx is first parameter.
  • Close: close the configured driver.

Constraints:

  • Drivers are selected by STORAGE_DRIVER. Supported drivers are gcs, s3, and r2.
  • Do not store context in package state; pass ctx through every I/O call.
  • No business logic; only storage driver lifecycle and operation delegation.

This package must NOT:

  • Contain use-case logic; only storage operations.

Index

Constants

View Source
const (
	DefaultPresignUploadExpiry = 15 * time.Minute
	MaxPresignUploadExpiry     = 7 * 24 * time.Hour
)

Variables

View Source
var ErrNotInitialized = errors.New("storage: not initialized (call Setup or set STORAGE_DRIVER)")

ErrNotInitialized is returned by package operations before Setup succeeds (or when STORAGE_DRIVER is empty / Setup disabled the driver).

Functions

func Close

func Close() error

Close shuts down the active driver and clears package state.

func DeleteObject

func DeleteObject(ctx context.Context, objectName string) error

DeleteObject removes an object.

func GetBucketName

func GetBucketName() string

GetBucketName returns the configured bucket name, or empty if not initialized.

func ListObjects

func ListObjects(ctx context.Context, prefix string) ([]string, error)

ListObjects lists object names under the given prefix.

func ObjectExists

func ObjectExists(ctx context.Context, objectName string) (bool, error)

ObjectExists reports whether an object exists.

func ReadObject

func ReadObject(ctx context.Context, objectName string) ([]byte, error)

ReadObject downloads an object and returns its full contents.

func ReadObjectAsReader

func ReadObjectAsReader(ctx context.Context, objectName string) (io.ReadCloser, error)

ReadObjectAsReader returns a streaming reader for an object. The caller must Close it.

func Setup

func Setup() error

Setup initializes the storage driver from config using a background context. No-op when STORAGE_DRIVER is empty.

func SetupContext

func SetupContext(ctx context.Context) error

SetupContext initializes the storage driver from config. When STORAGE_DRIVER is empty, any previously configured driver is closed and the package returns to the uninitialized state.

func WriteObject

func WriteObject(ctx context.Context, objectName string, data []byte, contentType string) error

WriteObject uploads object bytes with an optional content type.

Types

type Driver

type Driver interface {
	ReadObject(ctx context.Context, objectName string) ([]byte, error)
	ReadObjectAsReader(ctx context.Context, objectName string) (io.ReadCloser, error)
	WriteObject(ctx context.Context, objectName string, data []byte, contentType string) error
	DeleteObject(ctx context.Context, objectName string) error
	ObjectExists(ctx context.Context, objectName string) (bool, error)
	ListObjects(ctx context.Context, prefix string) ([]string, error)
	PresignUpload(ctx context.Context, objectName string, opts PresignUploadOptions) (PresignedUpload, error)
	Close() error
}

Driver is the provider-neutral object storage contract implemented by GCS and S3-compatible backends.

type PresignUploadOptions

type PresignUploadOptions struct {
	// Expiry is how long the URL remains valid.
	// Zero means DefaultPresignUploadExpiry (15m).
	Expiry time.Duration

	// ContentType, when non-empty, is included in the signature.
	// The client MUST send the same Content-Type header on PUT.
	ContentType string
}

PresignUploadOptions configures a client-side PUT upload URL.

type PresignedUpload

type PresignedUpload struct {
	URL       string
	Method    string
	Headers   map[string]string
	ExpiresAt time.Time
}

PresignedUpload is a one-shot client upload grant.

func PresignUpload

func PresignUpload(ctx context.Context, objectName string, opts PresignUploadOptions) (PresignedUpload, error)

PresignUpload returns a PUT URL for objectName using the active driver.

Jump to

Keyboard shortcuts

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