Documentation
ΒΆ
Overview ΒΆ
Package storage provides unified object storage with support for multiple backends including local filesystem, S3, GCS, and Azure Blob Storage.
Index ΒΆ
- Constants
- Variables
- func IsValidSize(size int64, maxSize int64) bool
- func NewExtension(opts ...ConfigOption) forge.Extension
- func NewExtensionWithConfig(config Config) forge.Extension
- func ValidateMetadata(metadata map[string]string) error
- type BackendConfig
- type BackendHealth
- type BackendType
- type BufferPool
- type BytesBufferPool
- type CircuitBreaker
- func (cb *CircuitBreaker) CanAttempt() bool
- func (cb *CircuitBreaker) Execute(ctx context.Context, name string, fn func() error) error
- func (cb *CircuitBreaker) GetState() CircuitState
- func (cb *CircuitBreaker) RecordFailure()
- func (cb *CircuitBreaker) RecordSuccess()
- func (cb *CircuitBreaker) Reset()
- type CircuitState
- type Config
- type ConfigOption
- func WithBackend(name string, backend BackendConfig) ConfigOption
- func WithBackends(backends map[string]BackendConfig) ConfigOption
- func WithCDN(enabled bool, baseURL string) ConfigOption
- func WithChunkSize(size int) ConfigOption
- func WithConfig(config Config) ConfigOption
- func WithDefault(name string) ConfigOption
- func WithEnhancedBackend(enabled bool) ConfigOption
- func WithLocalBackend(name, rootDir, baseURL string) ConfigOption
- func WithMaxUploadSize(size int64) ConfigOption
- func WithPresignedURLs(enabled bool, expiry time.Duration) ConfigOption
- func WithRequireConfig(require bool) ConfigOption
- func WithResilience(resilience ResilienceConfig) ConfigOption
- func WithS3Backend(name, bucket, region, endpoint string) ConfigOption
- type EnhancedLocalBackend
- func (b *EnhancedLocalBackend) Copy(ctx context.Context, srcKey, dstKey string) error
- func (b *EnhancedLocalBackend) Delete(ctx context.Context, key string) error
- func (b *EnhancedLocalBackend) Download(ctx context.Context, key string) (io.ReadCloser, error)
- func (b *EnhancedLocalBackend) Exists(ctx context.Context, key string) (bool, error)
- func (b *EnhancedLocalBackend) List(ctx context.Context, prefix string, opts ...ListOption) ([]Object, error)
- func (b *EnhancedLocalBackend) Metadata(ctx context.Context, key string) (*ObjectMetadata, error)
- func (b *EnhancedLocalBackend) Move(ctx context.Context, srcKey, dstKey string) error
- func (b *EnhancedLocalBackend) PresignDownload(ctx context.Context, key string, expiry time.Duration) (string, error)
- func (b *EnhancedLocalBackend) PresignUpload(ctx context.Context, key string, expiry time.Duration) (string, error)
- func (b *EnhancedLocalBackend) Upload(ctx context.Context, key string, data io.Reader, opts ...UploadOption) error
- type EnhancedLocalConfig
- type Extension
- type HealthCheckConfig
- type HealthChecker
- type ListOption
- type ListOptions
- type LocalBackend
- func (b *LocalBackend) Copy(ctx context.Context, srcKey, dstKey string) error
- func (b *LocalBackend) Delete(ctx context.Context, key string) error
- func (b *LocalBackend) Download(ctx context.Context, key string) (io.ReadCloser, error)
- func (b *LocalBackend) Exists(ctx context.Context, key string) (bool, error)
- func (b *LocalBackend) List(ctx context.Context, prefix string, opts ...ListOption) ([]Object, error)
- func (b *LocalBackend) Metadata(ctx context.Context, key string) (*ObjectMetadata, error)
- func (b *LocalBackend) Move(ctx context.Context, srcKey, dstKey string) error
- func (b *LocalBackend) PresignDownload(ctx context.Context, key string, expiry time.Duration) (string, error)
- func (b *LocalBackend) PresignUpload(ctx context.Context, key string, expiry time.Duration) (string, error)
- func (b *LocalBackend) Upload(ctx context.Context, key string, data io.Reader, opts ...UploadOption) error
- type Object
- type ObjectMetadata
- type OverallHealth
- type PathValidator
- type RateLimiter
- type ResilienceConfig
- type ResilientStorage
- func (rs *ResilientStorage) Copy(ctx context.Context, srcKey, dstKey string) error
- func (rs *ResilientStorage) Delete(ctx context.Context, key string) error
- func (rs *ResilientStorage) Download(ctx context.Context, key string) (io.ReadCloser, error)
- func (rs *ResilientStorage) Exists(ctx context.Context, key string) (bool, error)
- func (rs *ResilientStorage) GetCircuitBreakerState() CircuitState
- func (rs *ResilientStorage) List(ctx context.Context, prefix string, opts ...ListOption) ([]Object, error)
- func (rs *ResilientStorage) Metadata(ctx context.Context, key string) (*ObjectMetadata, error)
- func (rs *ResilientStorage) Move(ctx context.Context, srcKey, dstKey string) error
- func (rs *ResilientStorage) PresignDownload(ctx context.Context, key string, expiry time.Duration) (string, error)
- func (rs *ResilientStorage) PresignUpload(ctx context.Context, key string, expiry time.Duration) (string, error)
- func (rs *ResilientStorage) ResetCircuitBreaker()
- func (rs *ResilientStorage) Upload(ctx context.Context, key string, data io.Reader, opts ...UploadOption) error
- type S3Backend
- func (b *S3Backend) Copy(ctx context.Context, srcKey, dstKey string) error
- func (b *S3Backend) Delete(ctx context.Context, key string) error
- func (b *S3Backend) Download(ctx context.Context, key string) (io.ReadCloser, error)
- func (b *S3Backend) Exists(ctx context.Context, key string) (bool, error)
- func (b *S3Backend) List(ctx context.Context, prefix string, opts ...ListOption) ([]Object, error)
- func (b *S3Backend) Metadata(ctx context.Context, key string) (*ObjectMetadata, error)
- func (b *S3Backend) Move(ctx context.Context, srcKey, dstKey string) error
- func (b *S3Backend) PresignDownload(ctx context.Context, key string, expiry time.Duration) (string, error)
- func (b *S3Backend) PresignUpload(ctx context.Context, key string, expiry time.Duration) (string, error)
- func (b *S3Backend) Upload(ctx context.Context, key string, data io.Reader, opts ...UploadOption) error
- type S3Config
- type Storage
- func GetBackend(c forge.Container, name string) (Storage, error)
- func GetBackendFromApp(app forge.App, name string) (Storage, error)
- func GetNamedBackend(c forge.Container, name string) (Storage, error)
- func GetNamedBackendFromApp(app forge.App, name string) (Storage, error)
- func GetStorage(c forge.Container) (Storage, error)
- func GetStorageFromApp(app forge.App) (Storage, error)
- func MustGetBackend(c forge.Container, name string) Storage
- func MustGetBackendFromApp(app forge.App, name string) Storage
- func MustGetNamedBackend(c forge.Container, name string) Storage
- func MustGetNamedBackendFromApp(app forge.App, name string) Storage
- func MustGetStorage(c forge.Container) Storage
- func MustGetStorageFromApp(app forge.App) Storage
- type StorageManager
- func GetManager(c forge.Container) (*StorageManager, error)
- func GetManagerFromApp(app forge.App) (*StorageManager, error)
- func MustGetManager(c forge.Container) *StorageManager
- func MustGetManagerFromApp(app forge.App) *StorageManager
- func NewStorageManager(config Config, logger forge.Logger, metrics forge.Metrics) *StorageManager
- func (m *StorageManager) Backend(name string) Storage
- func (m *StorageManager) BackendHealth(ctx context.Context, name string) (*BackendHealth, error)
- func (m *StorageManager) Copy(ctx context.Context, srcKey, dstKey string) error
- func (m *StorageManager) DefaultBackend() (Storage, error)
- func (m *StorageManager) Delete(ctx context.Context, key string) error
- func (m *StorageManager) Download(ctx context.Context, key string) (io.ReadCloser, error)
- func (m *StorageManager) Exists(ctx context.Context, key string) (bool, error)
- func (m *StorageManager) GetURL(ctx context.Context, key string) string
- func (m *StorageManager) Health(ctx context.Context) error
- func (m *StorageManager) HealthCheckAll(ctx context.Context) map[string]*BackendHealth
- func (m *StorageManager) HealthDetailed(ctx context.Context, checkAll bool) (*OverallHealth, error)
- func (m *StorageManager) List(ctx context.Context, prefix string, opts ...ListOption) ([]Object, error)
- func (m *StorageManager) Metadata(ctx context.Context, key string) (*ObjectMetadata, error)
- func (m *StorageManager) Move(ctx context.Context, srcKey, dstKey string) error
- func (m *StorageManager) Name() string
- func (m *StorageManager) PresignDownload(ctx context.Context, key string, expiry time.Duration) (string, error)
- func (m *StorageManager) PresignUpload(ctx context.Context, key string, expiry time.Duration) (string, error)
- func (m *StorageManager) Start(ctx context.Context) error
- func (m *StorageManager) Stop(ctx context.Context) error
- func (m *StorageManager) Upload(ctx context.Context, key string, data io.Reader, opts ...UploadOption) error
- type UploadOption
- type UploadOptions
Constants ΒΆ
const ( // ManagerKey is the DI key for the StorageManager singleton. // Use GetManager() or MustGetManager() to resolve it. ManagerKey = "storage.manager" // StorageKey is the DI key for the default Storage backend. // Use GetStorage() or MustGetStorage() to resolve it. StorageKey = "storage.default" )
DI container keys for storage services. These constants define the keys used to register and resolve storage services from the Forge DI container.
Variables ΒΆ
var ( // Configuration errors. ErrNoBackendsConfigured = errors.New("no storage backends configured") ErrNoDefaultBackend = errors.New("no default backend specified") ErrDefaultBackendNotFound = errors.New("default backend not found in configuration") ErrInvalidBackendType = errors.New("invalid backend type") ErrBackendNotFound = errors.New("backend not found") // Operation errors. ErrObjectNotFound = errors.New("object not found") ErrObjectAlreadyExists = errors.New("object already exists") ErrInvalidKey = errors.New("invalid object key") ErrUploadFailed = errors.New("upload failed") ErrDownloadFailed = errors.New("download failed") ErrDeleteFailed = errors.New("delete failed") ErrPresignNotSupported = errors.New("presigned URLs not supported for this backend") ErrMultipartNotSupported = errors.New("multipart upload not supported for this backend") // Resilience errors. ErrCircuitBreakerOpen = errors.New("circuit breaker is open") ErrRateLimitExceeded = errors.New("rate limit exceeded") // Validation errors. ErrFileTooLarge = errors.New("file size exceeds maximum allowed size") ErrInvalidContentType = errors.New("invalid content type") ErrInvalidPath = errors.New("invalid path: potential path traversal detected") )
Functions ΒΆ
func IsValidSize ΒΆ
IsValidSize checks if a file size is within limits.
func NewExtension ΒΆ
func NewExtension(opts ...ConfigOption) forge.Extension
NewExtension creates a new storage extension with variadic options.
func NewExtensionWithConfig ΒΆ
NewExtensionWithConfig creates a new storage extension with a complete config.
func ValidateMetadata ΒΆ
ValidateMetadata validates metadata keys and values.
Types ΒΆ
type BackendConfig ΒΆ
type BackendConfig struct {
Type BackendType `json:"type" yaml:"type"`
Config map[string]any `json:"config" yaml:"config"`
}
BackendConfig is the configuration for a storage backend.
type BackendHealth ΒΆ
type BackendHealth struct {
Name string `json:"name"`
Healthy bool `json:"healthy"`
ResponseTime time.Duration `json:"response_time"`
Error string `json:"error,omitempty"`
LastChecked time.Time `json:"last_checked"`
CheckType string `json:"check_type"`
}
BackendHealth represents health status of a backend.
type BackendType ΒΆ
type BackendType string
BackendType represents the type of storage backend.
const ( // BackendTypeLocal represents local filesystem storage. BackendTypeLocal BackendType = "local" // BackendTypeS3 represents AWS S3 storage. BackendTypeS3 BackendType = "s3" // BackendTypeGCS represents Google Cloud Storage. BackendTypeGCS BackendType = "gcs" // BackendTypeAzure represents Azure Blob Storage. BackendTypeAzure BackendType = "azure" )
func (BackendType) String ΒΆ
func (t BackendType) String() string
String returns the string representation of the backend type.
type BufferPool ΒΆ
type BufferPool struct {
// contains filtered or unexported fields
}
BufferPool manages a pool of reusable buffers for efficient I/O operations.
func NewBufferPool ΒΆ
func NewBufferPool(bufferSize int) *BufferPool
NewBufferPool creates a new buffer pool with the specified buffer size.
type BytesBufferPool ΒΆ
type BytesBufferPool struct {
// contains filtered or unexported fields
}
BytesBufferPool manages a pool of bytes.Buffer for efficient operations.
func NewBytesBufferPool ΒΆ
func NewBytesBufferPool() *BytesBufferPool
NewBytesBufferPool creates a new bytes buffer pool.
func (*BytesBufferPool) Get ΒΆ
func (bbp *BytesBufferPool) Get() *bytes.Buffer
Get retrieves a buffer from the pool.
func (*BytesBufferPool) Put ΒΆ
func (bbp *BytesBufferPool) Put(buf *bytes.Buffer)
Put returns a buffer to the pool.
type CircuitBreaker ΒΆ
type CircuitBreaker struct {
// contains filtered or unexported fields
}
CircuitBreaker implements circuit breaker pattern.
func NewCircuitBreaker ΒΆ
func NewCircuitBreaker(config ResilienceConfig, logger forge.Logger, metrics forge.Metrics) *CircuitBreaker
NewCircuitBreaker creates a new circuit breaker.
func (*CircuitBreaker) CanAttempt ΒΆ
func (cb *CircuitBreaker) CanAttempt() bool
CanAttempt checks if a request can be attempted.
func (*CircuitBreaker) GetState ΒΆ
func (cb *CircuitBreaker) GetState() CircuitState
GetState returns current circuit state.
func (*CircuitBreaker) RecordFailure ΒΆ
func (cb *CircuitBreaker) RecordFailure()
RecordFailure records a failed execution.
func (*CircuitBreaker) RecordSuccess ΒΆ
func (cb *CircuitBreaker) RecordSuccess()
RecordSuccess records a successful execution.
type CircuitState ΒΆ
type CircuitState int
CircuitState represents circuit breaker state.
const ( CircuitClosed CircuitState = iota CircuitOpen CircuitHalfOpen )
func (CircuitState) String ΒΆ
func (s CircuitState) String() string
type Config ΒΆ
type Config struct {
// Default backend name
Default string `default:"local" json:"default" yaml:"default"`
// Backend configurations
Backends map[string]BackendConfig `json:"backends" yaml:"backends"`
// Features
EnablePresignedURLs bool `default:"true" json:"enable_presigned_urls" yaml:"enable_presigned_urls"`
PresignExpiry time.Duration `default:"15m" json:"presign_expiry" yaml:"presign_expiry"`
MaxUploadSize int64 `default:"5368709120" json:"max_upload_size" yaml:"max_upload_size"` // 5GB
ChunkSize int `default:"5242880" json:"chunk_size" yaml:"chunk_size"` // 5MB
// CDN
EnableCDN bool `default:"false" json:"enable_cdn" yaml:"enable_cdn"`
CDNBaseURL string `json:"cdn_base_url" yaml:"cdn_base_url"`
// Resilience configuration
Resilience ResilienceConfig `json:"resilience" yaml:"resilience"`
// Use enhanced backend (with locking, pooling, etc.)
UseEnhancedBackend bool `default:"true" json:"use_enhanced_backend" yaml:"use_enhanced_backend"`
// Config loading flags
RequireConfig bool `json:"-" yaml:"-"`
}
Config is the storage extension configuration.
func DefaultConfig ΒΆ
func DefaultConfig() Config
DefaultConfig returns the default storage configuration.
type ConfigOption ΒΆ
type ConfigOption func(*Config)
ConfigOption is a functional option for Config.
func WithBackend ΒΆ
func WithBackend(name string, backend BackendConfig) ConfigOption
WithBackend adds a single backend configuration.
func WithBackends ΒΆ
func WithBackends(backends map[string]BackendConfig) ConfigOption
WithBackends sets all backend configurations.
func WithCDN ΒΆ
func WithCDN(enabled bool, baseURL string) ConfigOption
WithCDN enables CDN with the specified base URL.
func WithChunkSize ΒΆ
func WithChunkSize(size int) ConfigOption
WithChunkSize sets the chunk size for multipart uploads.
func WithConfig ΒΆ
func WithConfig(config Config) ConfigOption
WithConfig replaces the entire config.
func WithDefault ΒΆ
func WithDefault(name string) ConfigOption
WithDefault sets the default backend name.
func WithEnhancedBackend ΒΆ
func WithEnhancedBackend(enabled bool) ConfigOption
WithEnhancedBackend enables/disables enhanced backend features (locking, pooling, etc.).
func WithLocalBackend ΒΆ
func WithLocalBackend(name, rootDir, baseURL string) ConfigOption
WithLocalBackend adds a local filesystem backend.
func WithMaxUploadSize ΒΆ
func WithMaxUploadSize(size int64) ConfigOption
WithMaxUploadSize sets the maximum upload size.
func WithPresignedURLs ΒΆ
func WithPresignedURLs(enabled bool, expiry time.Duration) ConfigOption
WithPresignedURLs enables/disables presigned URLs.
func WithRequireConfig ΒΆ
func WithRequireConfig(require bool) ConfigOption
WithRequireConfig requires config from YAML.
func WithResilience ΒΆ
func WithResilience(resilience ResilienceConfig) ConfigOption
WithResilience sets the resilience configuration.
func WithS3Backend ΒΆ
func WithS3Backend(name, bucket, region, endpoint string) ConfigOption
WithS3Backend adds an S3 backend.
type EnhancedLocalBackend ΒΆ
type EnhancedLocalBackend struct {
// contains filtered or unexported fields
}
EnhancedLocalBackend implements enhanced local filesystem storage with proper locking and pooling.
func NewEnhancedLocalBackend ΒΆ
func NewEnhancedLocalBackend(config map[string]any, logger forge.Logger, metrics forge.Metrics) (*EnhancedLocalBackend, error)
NewEnhancedLocalBackend creates a new enhanced local filesystem backend.
func (*EnhancedLocalBackend) Copy ΒΆ
func (b *EnhancedLocalBackend) Copy(ctx context.Context, srcKey, dstKey string) error
Copy copies a file with proper locking.
func (*EnhancedLocalBackend) Delete ΒΆ
func (b *EnhancedLocalBackend) Delete(ctx context.Context, key string) error
Delete deletes a file with proper locking.
func (*EnhancedLocalBackend) Download ΒΆ
func (b *EnhancedLocalBackend) Download(ctx context.Context, key string) (io.ReadCloser, error)
Download downloads a file with proper locking.
func (*EnhancedLocalBackend) List ΒΆ
func (b *EnhancedLocalBackend) List(ctx context.Context, prefix string, opts ...ListOption) ([]Object, error)
List lists files with a prefix.
func (*EnhancedLocalBackend) Metadata ΒΆ
func (b *EnhancedLocalBackend) Metadata(ctx context.Context, key string) (*ObjectMetadata, error)
Metadata retrieves object metadata.
func (*EnhancedLocalBackend) Move ΒΆ
func (b *EnhancedLocalBackend) Move(ctx context.Context, srcKey, dstKey string) error
Move moves a file with proper locking.
func (*EnhancedLocalBackend) PresignDownload ΒΆ
func (b *EnhancedLocalBackend) PresignDownload(ctx context.Context, key string, expiry time.Duration) (string, error)
PresignDownload generates a presigned URL for download.
func (*EnhancedLocalBackend) PresignUpload ΒΆ
func (b *EnhancedLocalBackend) PresignUpload(ctx context.Context, key string, expiry time.Duration) (string, error)
PresignUpload generates a presigned URL for upload.
func (*EnhancedLocalBackend) Upload ΒΆ
func (b *EnhancedLocalBackend) Upload(ctx context.Context, key string, data io.Reader, opts ...UploadOption) error
Upload uploads a file with proper locking and validation.
type EnhancedLocalConfig ΒΆ
type EnhancedLocalConfig struct {
RootDir string
BaseURL string
Secret string
ChunkSize int64
MaxUploadSize int64
}
EnhancedLocalConfig contains configuration for enhanced local backend.
type Extension ΒΆ
type Extension struct {
*forge.BaseExtension
// contains filtered or unexported fields
}
Extension implements the storage extension. The extension is now a lightweight facade that loads config and registers services.
func (*Extension) Health ΒΆ
Health checks the extension health. Manager health is managed by Vessel through StorageManager.Health().
type HealthCheckConfig ΒΆ
type HealthCheckConfig struct {
Timeout time.Duration `default:"5s" json:"timeout" yaml:"timeout"`
WriteTestFile bool `default:"true" json:"write_test_file" yaml:"write_test_file"`
TestKey string `default:".health_check" json:"test_key" yaml:"test_key"`
CheckAll bool `default:"false" json:"check_all" yaml:"check_all"`
EnableMetrics bool `default:"true" json:"enable_metrics" yaml:"enable_metrics"`
}
HealthCheckConfig configures health check behavior.
func DefaultHealthCheckConfig ΒΆ
func DefaultHealthCheckConfig() HealthCheckConfig
DefaultHealthCheckConfig returns default health check configuration.
type HealthChecker ΒΆ
type HealthChecker struct {
// contains filtered or unexported fields
}
HealthChecker performs comprehensive health checks on storage backends.
func NewHealthChecker ΒΆ
func NewHealthChecker(backends map[string]Storage, logger forge.Logger, metrics forge.Metrics, config HealthCheckConfig) *HealthChecker
NewHealthChecker creates a new health checker.
func (*HealthChecker) CheckHealth ΒΆ
func (hc *HealthChecker) CheckHealth(ctx context.Context, defaultBackend string, checkAll bool) (*OverallHealth, error)
CheckHealth performs health check on default backend or all backends.
func (*HealthChecker) GetBackendHealth ΒΆ
func (hc *HealthChecker) GetBackendHealth(ctx context.Context, name string) (*BackendHealth, error)
GetBackendHealth gets health status of a specific backend.
type ListOption ΒΆ
type ListOption func(*ListOptions)
ListOption is a functional option for listing.
func WithRecursive ΒΆ
func WithRecursive(recursive bool) ListOption
WithRecursive sets recursive listing.
type ListOptions ΒΆ
ListOptions contains list options.
type LocalBackend ΒΆ
type LocalBackend struct {
// contains filtered or unexported fields
}
LocalBackend implements storage using local filesystem.
func NewLocalBackend ΒΆ
func NewLocalBackend(config map[string]any, logger forge.Logger, metrics forge.Metrics) (*LocalBackend, error)
NewLocalBackend creates a new local filesystem backend.
func (*LocalBackend) Copy ΒΆ
func (b *LocalBackend) Copy(ctx context.Context, srcKey, dstKey string) error
Copy copies a file.
func (*LocalBackend) Delete ΒΆ
func (b *LocalBackend) Delete(ctx context.Context, key string) error
Delete deletes a file.
func (*LocalBackend) Download ΒΆ
func (b *LocalBackend) Download(ctx context.Context, key string) (io.ReadCloser, error)
Download downloads a file.
func (*LocalBackend) List ΒΆ
func (b *LocalBackend) List(ctx context.Context, prefix string, opts ...ListOption) ([]Object, error)
List lists files with a prefix.
func (*LocalBackend) Metadata ΒΆ
func (b *LocalBackend) Metadata(ctx context.Context, key string) (*ObjectMetadata, error)
Metadata retrieves object metadata.
func (*LocalBackend) Move ΒΆ
func (b *LocalBackend) Move(ctx context.Context, srcKey, dstKey string) error
Move moves a file.
func (*LocalBackend) PresignDownload ΒΆ
func (b *LocalBackend) PresignDownload(ctx context.Context, key string, expiry time.Duration) (string, error)
PresignDownload generates a presigned URL for download.
func (*LocalBackend) PresignUpload ΒΆ
func (b *LocalBackend) PresignUpload(ctx context.Context, key string, expiry time.Duration) (string, error)
PresignUpload generates a presigned URL for upload.
func (*LocalBackend) Upload ΒΆ
func (b *LocalBackend) Upload(ctx context.Context, key string, data io.Reader, opts ...UploadOption) error
Upload uploads a file.
type Object ΒΆ
type Object struct {
Key string `json:"key"`
Size int64 `json:"size"`
LastModified time.Time `json:"last_modified"`
ETag string `json:"etag"`
ContentType string `json:"content_type"`
Metadata map[string]string `json:"metadata"`
}
Object represents a storage object.
type ObjectMetadata ΒΆ
type ObjectMetadata struct {
Key string `json:"key"`
Size int64 `json:"size"`
LastModified time.Time `json:"last_modified"`
ETag string `json:"etag"`
ContentType string `json:"content_type"`
Metadata map[string]string `json:"metadata"`
}
ObjectMetadata represents object metadata.
type OverallHealth ΒΆ
type OverallHealth struct {
Healthy bool `json:"healthy"`
BackendCount int `json:"backend_count"`
HealthyCount int `json:"healthy_count"`
UnhealthyCount int `json:"unhealthy_count"`
Backends map[string]BackendHealth `json:"backends"`
CheckedAt time.Time `json:"checked_at"`
}
OverallHealth represents overall storage health.
type PathValidator ΒΆ
type PathValidator struct {
// contains filtered or unexported fields
}
PathValidator validates storage paths for security.
func NewPathValidator ΒΆ
func NewPathValidator() *PathValidator
NewPathValidator creates a new path validator with default rules.
func (*PathValidator) SanitizeKey ΒΆ
func (pv *PathValidator) SanitizeKey(key string) string
SanitizeKey sanitizes a key to make it safe.
func (*PathValidator) ValidateContentType ΒΆ
func (pv *PathValidator) ValidateContentType(contentType string) error
ValidateContentType validates content type.
func (*PathValidator) ValidateKey ΒΆ
func (pv *PathValidator) ValidateKey(key string) error
ValidateKey validates a storage key for security issues.
type RateLimiter ΒΆ
type RateLimiter struct {
// contains filtered or unexported fields
}
RateLimiter implements token bucket rate limiting.
func NewRateLimiter ΒΆ
func NewRateLimiter(config ResilienceConfig, metrics forge.Metrics) *RateLimiter
NewRateLimiter creates a new rate limiter.
func (*RateLimiter) Allow ΒΆ
func (rl *RateLimiter) Allow() bool
Allow checks if a request is allowed.
type ResilienceConfig ΒΆ
type ResilienceConfig struct {
// Retry configuration
MaxRetries int `default:"3" json:"max_retries" yaml:"max_retries"`
InitialBackoff time.Duration `default:"100ms" json:"initial_backoff" yaml:"initial_backoff"`
MaxBackoff time.Duration `default:"10s" json:"max_backoff" yaml:"max_backoff"`
BackoffMultiplier float64 `default:"2.0" json:"backoff_multiplier" yaml:"backoff_multiplier"`
// Circuit breaker configuration
CircuitBreakerEnabled bool `default:"true" json:"circuit_breaker_enabled" yaml:"circuit_breaker_enabled"`
CircuitBreakerThreshold uint32 `default:"5" json:"circuit_breaker_threshold" yaml:"circuit_breaker_threshold"`
CircuitBreakerTimeout time.Duration `default:"60s" json:"circuit_breaker_timeout" yaml:"circuit_breaker_timeout"`
CircuitBreakerHalfOpenMax uint32 `default:"3" json:"circuit_breaker_half_open_max" yaml:"circuit_breaker_half_open_max"`
// Rate limiting
RateLimitEnabled bool `default:"true" json:"rate_limit_enabled" yaml:"rate_limit_enabled"`
RateLimitPerSec int `default:"100" json:"rate_limit_per_sec" yaml:"rate_limit_per_sec"`
RateLimitBurst int `default:"200" json:"rate_limit_burst" yaml:"rate_limit_burst"`
// Timeout configuration
OperationTimeout time.Duration `default:"30s" json:"operation_timeout" yaml:"operation_timeout"`
}
ResilienceConfig configures resilience features.
func DefaultResilienceConfig ΒΆ
func DefaultResilienceConfig() ResilienceConfig
DefaultResilienceConfig returns default resilience configuration.
type ResilientStorage ΒΆ
type ResilientStorage struct {
// contains filtered or unexported fields
}
ResilientStorage wraps a storage backend with resilience features.
func NewResilientStorage ΒΆ
func NewResilientStorage(backend Storage, config ResilienceConfig, logger forge.Logger, metrics forge.Metrics) *ResilientStorage
NewResilientStorage creates a resilient storage wrapper.
func (*ResilientStorage) Copy ΒΆ
func (rs *ResilientStorage) Copy(ctx context.Context, srcKey, dstKey string) error
Copy copies with resilience.
func (*ResilientStorage) Delete ΒΆ
func (rs *ResilientStorage) Delete(ctx context.Context, key string) error
Delete deletes with resilience.
func (*ResilientStorage) Download ΒΆ
func (rs *ResilientStorage) Download(ctx context.Context, key string) (io.ReadCloser, error)
Download downloads with resilience.
func (*ResilientStorage) GetCircuitBreakerState ΒΆ
func (rs *ResilientStorage) GetCircuitBreakerState() CircuitState
GetCircuitBreakerState returns circuit breaker state.
func (*ResilientStorage) List ΒΆ
func (rs *ResilientStorage) List(ctx context.Context, prefix string, opts ...ListOption) ([]Object, error)
List lists with resilience.
func (*ResilientStorage) Metadata ΒΆ
func (rs *ResilientStorage) Metadata(ctx context.Context, key string) (*ObjectMetadata, error)
Metadata gets metadata with resilience.
func (*ResilientStorage) Move ΒΆ
func (rs *ResilientStorage) Move(ctx context.Context, srcKey, dstKey string) error
Move moves with resilience.
func (*ResilientStorage) PresignDownload ΒΆ
func (rs *ResilientStorage) PresignDownload(ctx context.Context, key string, expiry time.Duration) (string, error)
PresignDownload presigns download URL with resilience.
func (*ResilientStorage) PresignUpload ΒΆ
func (rs *ResilientStorage) PresignUpload(ctx context.Context, key string, expiry time.Duration) (string, error)
PresignUpload presigns upload URL with resilience.
func (*ResilientStorage) ResetCircuitBreaker ΒΆ
func (rs *ResilientStorage) ResetCircuitBreaker()
ResetCircuitBreaker resets the circuit breaker.
func (*ResilientStorage) Upload ΒΆ
func (rs *ResilientStorage) Upload(ctx context.Context, key string, data io.Reader, opts ...UploadOption) error
Upload uploads with resilience.
type S3Backend ΒΆ
type S3Backend struct {
// contains filtered or unexported fields
}
S3Backend implements storage using AWS S3.
func NewS3Backend ΒΆ
func NewS3Backend(configMap map[string]any, logger forge.Logger, metrics forge.Metrics) (*S3Backend, error)
NewS3Backend creates a new S3 storage backend.
func (*S3Backend) PresignDownload ΒΆ
func (b *S3Backend) PresignDownload(ctx context.Context, key string, expiry time.Duration) (string, error)
PresignDownload generates a presigned URL for download.
type S3Config ΒΆ
type S3Config struct {
Region string
Bucket string
Prefix string
AccessKeyID string
SecretAccessKey string
SessionToken string
Endpoint string // For S3-compatible services
UsePathStyle bool // For S3-compatible services
}
S3Config contains S3 configuration.
type Storage ΒΆ
type Storage interface {
// Upload uploads an object
Upload(ctx context.Context, key string, data io.Reader, opts ...UploadOption) error
// Download downloads an object
Download(ctx context.Context, key string) (io.ReadCloser, error)
// Delete deletes an object
Delete(ctx context.Context, key string) error
// List lists objects with a prefix
List(ctx context.Context, prefix string, opts ...ListOption) ([]Object, error)
// Metadata retrieves object metadata
Metadata(ctx context.Context, key string) (*ObjectMetadata, error)
// Exists checks if an object exists
Exists(ctx context.Context, key string) (bool, error)
// Copy copies an object
Copy(ctx context.Context, srcKey, dstKey string) error
// Move moves an object
Move(ctx context.Context, srcKey, dstKey string) error
// PresignUpload generates a presigned URL for upload
PresignUpload(ctx context.Context, key string, expiry time.Duration) (string, error)
// PresignDownload generates a presigned URL for download
PresignDownload(ctx context.Context, key string, expiry time.Duration) (string, error)
}
Storage defines the unified storage interface.
func GetBackend ΒΆ
GetBackend retrieves a named backend through the StorageManager. This is an alias for GetNamedBackend for consistency with the manager's Backend() method.
Example:
s3, err := storage.GetBackend(c, "s3")
if err != nil {
return err
}
func GetBackendFromApp ΒΆ
GetBackendFromApp retrieves a named backend from the app. This is an alias for GetNamedBackendFromApp. Returns error if app is nil, manager not found, or backend not found.
Example:
s3, err := storage.GetBackendFromApp(app, "s3")
if err != nil {
return err
}
func GetNamedBackend ΒΆ
GetNamedBackend retrieves a named backend through the StorageManager. This is useful when you have multiple backends configured.
Example:
// Use S3 for production files
s3, err := storage.GetNamedBackend(c, "s3")
if err != nil {
return err
}
// Use local storage for temporary files
local, err := storage.GetNamedBackend(c, "local")
if err != nil {
return err
}
func GetNamedBackendFromApp ΒΆ
GetNamedBackendFromApp retrieves a named backend from the app. Returns error if app is nil, manager not found, or backend not found.
Example:
s3, err := storage.GetNamedBackendFromApp(app, "s3")
if err != nil {
return err
}
func GetStorage ΒΆ
GetStorage retrieves the default Storage backend from the container. Returns error if not found or type assertion fails.
This is the most common helper for accessing storage services. It returns the default backend configured in the extension.
Example:
s, err := storage.GetStorage(c)
if err != nil {
return fmt.Errorf("failed to get storage: %w", err)
}
err = s.Upload(ctx, "file.pdf", reader)
func GetStorageFromApp ΒΆ
GetStorageFromApp retrieves the default Storage backend from the app. Returns error if app is nil or storage not found.
Example:
s, err := storage.GetStorageFromApp(app)
if err != nil {
return err
}
func MustGetBackend ΒΆ
MustGetBackend retrieves a named backend through the StorageManager. This is an alias for MustGetNamedBackend for consistency with the manager's Backend() method. Panics if manager not found or backend not found.
Example:
s3 := storage.MustGetBackend(c, "s3")
func MustGetBackendFromApp ΒΆ
MustGetBackendFromApp retrieves a named backend from the app. This is an alias for MustGetNamedBackendFromApp. Panics if app is nil, manager not found, or backend not found.
Example:
s3 := storage.MustGetBackendFromApp(app, "s3")
func MustGetNamedBackend ΒΆ
MustGetNamedBackend retrieves a named backend through the StorageManager. Panics if manager not found or backend not found.
Example:
type BackupService struct {
primary Storage
backup Storage
}
func NewBackupService(c forge.Container) *BackupService {
return &BackupService{
primary: storage.MustGetNamedBackend(c, "s3"),
backup: storage.MustGetNamedBackend(c, "gcs"),
}
}
func MustGetNamedBackendFromApp ΒΆ
MustGetNamedBackendFromApp retrieves a named backend from the app. Panics if app is nil, manager not found, or backend not found.
Example:
s3 := storage.MustGetNamedBackendFromApp(app, "s3")
func MustGetStorage ΒΆ
MustGetStorage retrieves the default Storage backend from the container. Panics if not found or type assertion fails.
This is the recommended helper for services that require storage.
Example:
func NewFileService(c forge.Container) *FileService {
return &FileService{
storage: storage.MustGetStorage(c),
}
}
func MustGetStorageFromApp ΒΆ
MustGetStorageFromApp retrieves the default Storage backend from the app. Panics if app is nil or storage not found.
Example:
func setupRoutes(app forge.App) {
s := storage.MustGetStorageFromApp(app)
// Use storage
}
type StorageManager ΒΆ
type StorageManager struct {
// contains filtered or unexported fields
}
StorageManager manages multiple storage backends.
func GetManager ΒΆ
func GetManager(c forge.Container) (*StorageManager, error)
GetManager retrieves the StorageManager from the container. Returns error if not found or type assertion fails.
Example:
manager, err := storage.GetManager(c)
if err != nil {
return fmt.Errorf("storage not available: %w", err)
}
s3 := manager.Backend("s3")
func GetManagerFromApp ΒΆ
func GetManagerFromApp(app forge.App) (*StorageManager, error)
GetManagerFromApp retrieves the StorageManager from the app. Returns error if app is nil or manager not found.
Example:
manager, err := storage.GetManagerFromApp(app)
if err != nil {
return err
}
func MustGetManager ΒΆ
func MustGetManager(c forge.Container) *StorageManager
MustGetManager retrieves the StorageManager from the container. Panics if not found or type assertion fails.
Example:
manager := storage.MustGetManager(c) backends := manager.backends
func MustGetManagerFromApp ΒΆ
func MustGetManagerFromApp(app forge.App) *StorageManager
MustGetManagerFromApp retrieves the StorageManager from the app. Panics if app is nil or manager not found.
Example:
manager := storage.MustGetManagerFromApp(app)
func NewStorageManager ΒΆ
NewStorageManager creates a new storage manager.
func (*StorageManager) Backend ΒΆ
func (m *StorageManager) Backend(name string) Storage
Backend returns a specific backend.
func (*StorageManager) BackendHealth ΒΆ
func (m *StorageManager) BackendHealth(ctx context.Context, name string) (*BackendHealth, error)
BackendHealth returns health of a specific backend.
func (*StorageManager) Copy ΒΆ
func (m *StorageManager) Copy(ctx context.Context, srcKey, dstKey string) error
Copy copies in the default backend.
func (*StorageManager) DefaultBackend ΒΆ
func (m *StorageManager) DefaultBackend() (Storage, error)
DefaultBackend returns the default backend. Returns error if no default backend is configured.
func (*StorageManager) Delete ΒΆ
func (m *StorageManager) Delete(ctx context.Context, key string) error
Delete deletes from the default backend.
func (*StorageManager) Download ΒΆ
func (m *StorageManager) Download(ctx context.Context, key string) (io.ReadCloser, error)
Download downloads from the default backend.
func (*StorageManager) GetURL ΒΆ
func (m *StorageManager) GetURL(ctx context.Context, key string) string
GetURL returns the URL for an object (CDN or direct).
func (*StorageManager) Health ΒΆ
func (m *StorageManager) Health(ctx context.Context) error
Health checks the health of all backends.
func (*StorageManager) HealthCheckAll ΒΆ
func (m *StorageManager) HealthCheckAll(ctx context.Context) map[string]*BackendHealth
HealthCheckAll checks the health of all backends and returns a map of backend names to their health status.
func (*StorageManager) HealthDetailed ΒΆ
func (m *StorageManager) HealthDetailed(ctx context.Context, checkAll bool) (*OverallHealth, error)
HealthDetailed returns detailed health information.
func (*StorageManager) List ΒΆ
func (m *StorageManager) List(ctx context.Context, prefix string, opts ...ListOption) ([]Object, error)
List lists from the default backend.
func (*StorageManager) Metadata ΒΆ
func (m *StorageManager) Metadata(ctx context.Context, key string) (*ObjectMetadata, error)
Metadata gets metadata from the default backend.
func (*StorageManager) Move ΒΆ
func (m *StorageManager) Move(ctx context.Context, srcKey, dstKey string) error
Move moves in the default backend.
func (*StorageManager) Name ΒΆ
func (m *StorageManager) Name() string
Name returns the service name for Vessel's lifecycle management. Implements di.Service interface.
func (*StorageManager) PresignDownload ΒΆ
func (m *StorageManager) PresignDownload(ctx context.Context, key string, expiry time.Duration) (string, error)
PresignDownload generates a presigned download URL for the default backend.
func (*StorageManager) PresignUpload ΒΆ
func (m *StorageManager) PresignUpload(ctx context.Context, key string, expiry time.Duration) (string, error)
PresignUpload generates a presigned upload URL for the default backend.
func (*StorageManager) Start ΒΆ
func (m *StorageManager) Start(ctx context.Context) error
Start initializes all storage backends.
func (*StorageManager) Stop ΒΆ
func (m *StorageManager) Stop(ctx context.Context) error
Stop closes all storage backends.
func (*StorageManager) Upload ΒΆ
func (m *StorageManager) Upload(ctx context.Context, key string, data io.Reader, opts ...UploadOption) error
Upload uploads to the default backend.
type UploadOption ΒΆ
type UploadOption func(*UploadOptions)
UploadOption is a functional option for uploads.
func WithContentType ΒΆ
func WithContentType(contentType string) UploadOption
WithContentType sets the content type.
func WithMetadata ΒΆ
func WithMetadata(metadata map[string]string) UploadOption
WithMetadata sets metadata.