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
- Variables
- func Close() error
- func DeleteObject(ctx context.Context, objectName string) error
- func GetBucketName() string
- func ListObjects(ctx context.Context, prefix string) ([]string, error)
- func ObjectExists(ctx context.Context, objectName string) (bool, error)
- func ReadObject(ctx context.Context, objectName string) ([]byte, error)
- func ReadObjectAsReader(ctx context.Context, objectName string) (io.ReadCloser, error)
- func Setup() error
- func SetupContext(ctx context.Context) error
- func WriteObject(ctx context.Context, objectName string, data []byte, contentType string) error
- type Driver
- type PresignUploadOptions
- type PresignedUpload
Constants ¶
const ( DefaultPresignUploadExpiry = 15 * time.Minute MaxPresignUploadExpiry = 7 * 24 * time.Hour )
Variables ¶
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 DeleteObject ¶
DeleteObject removes an object.
func GetBucketName ¶
func GetBucketName() string
GetBucketName returns the configured bucket name, or empty if not initialized.
func ListObjects ¶
ListObjects lists object names under the given prefix.
func ObjectExists ¶
ObjectExists reports whether an object exists.
func ReadObject ¶
ReadObject downloads an object and returns its full contents.
func ReadObjectAsReader ¶
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 ¶
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.
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.