storage

package
v1.1.0 Latest Latest
Warning

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

Go to latest
Published: Feb 4, 2026 License: MIT Imports: 22 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func CreateMinioClient

func CreateMinioClient(ctx context.Context, config Config, log *zap.Logger) (*minio.Client, error)

func DeleteRecursive

func DeleteRecursive(ctx context.Context, s Storage, prefix string) error

DeleteRecursive deletes all files with the given prefix

func DeleteSimple

func DeleteSimple(ctx context.Context, s Storage, key string) error

DeleteSimple deletes a single file

func DownloadFile

func DownloadFile(c *fiber.Ctx, s Storage, key string) error

DownloadFile forces a file download through Fiber

func DownloadToBytes

func DownloadToBytes(ctx context.Context, s Storage, key string) ([]byte, error)

DownloadToBytes downloads a file and returns it as bytes

func FileExists

func FileExists(ctx context.Context, s Storage, key string) (bool, error)

FileExists checks if a file exists in storage

func GenerateKey

func GenerateKey(prefix, filename string) string

GenerateKey generates a unique storage key from a filename

func GenerateUniqueKey

func GenerateUniqueKey(prefix, filename string) string

GenerateUniqueKey generates a unique key with UUID-like identifier

func GetPublicURL

func GetPublicURL(ctx context.Context, s Storage, key string) (string, error)

GetPublicURL returns a permanent public URL for a file

func GetTemporaryURL

func GetTemporaryURL(ctx context.Context, s Storage, key string, validFor time.Duration) (string, error)

GetTemporaryURL generates a temporary signed URL valid for the specified duration

func ProxyToFiber

func ProxyToFiber(c *fiber.Ctx, s Storage, key string, opts *ProxyOptions) error

ProxyToFiber proxies a file with support for range requests and caching

func ServeFile

func ServeFile(c *fiber.Ctx, s Storage, key string) error

ServeFile is a simple helper to serve a file through Fiber

func Start

func Start(lc fx.Lifecycle, storage Storage)

func StreamToFiber

func StreamToFiber(c *fiber.Ctx, s Storage, key string) error

StreamToFiber streams a file directly to a Fiber response with proper headers

Types

type Config

type Config struct {
	PublicEndpoint  string `mapstructure:"public_endpoint" yaml:"public_endpoint"`
	Endpoint        string `mapstructure:"endpoint" yaml:"endpoint"`
	AccessKeyID     string `mapstructure:"access_key_id" yaml:"access_key_id"`
	SecretAccessKey string `mapstructure:"secret_access_key" yaml:"secret_access_key"`
	BucketName      string `mapstructure:"bucket_name" yaml:"bucket_name"`
	UseSSL          bool   `mapstructure:"use_ssl" yaml:"use_ssl"`
}

type DeleteOptions

type DeleteOptions struct {
	// Recursive deletes all files with matching prefix
	Recursive bool
}

DeleteOptions configures file deletion behavior

func DefaultDeleteOptions

func DefaultDeleteOptions() *DeleteOptions

Helper function for delete options

type DownloadOptions

type DownloadOptions struct {
	// Range specifies byte range (e.g., "bytes=0-1023")
	Range string

	// IfModifiedSince for conditional downloads
	IfModifiedSince *time.Time
}

DownloadOptions configures file download behavior

func DefaultDownloadOptions

func DefaultDownloadOptions() *DownloadOptions

Helper function for download options

type ListOptions

type ListOptions struct {
	// Prefix filters by key prefix
	Prefix string

	// MaxResults limits number of results
	MaxResults int

	// ContinuationToken for pagination
	ContinuationToken string
}

ListOptions configures file listing behavior

func DefaultListOptions

func DefaultListOptions() *ListOptions

Helper function for list options

type ListResult

type ListResult struct {
	Items             []*Metadata
	ContinuationToken string
	HasMore           bool
}

ListResult contains list operation results

type Metadata

type Metadata struct {
	Key         string            // Storage key/path
	FileName    string            // Original filename
	ContentType string            // MIME type
	URL         string            // Access URL
	Size        int64             // File size in bytes
	ETag        string            // Entity tag for caching
	Width       int               // Image width (0 if not applicable)
	Height      int               // Image height (0 if not applicable)
	ModTime     time.Time         // Last modified time
	Custom      map[string]string // Custom metadata
}

Metadata contains information about an uploaded/stored file

func CopyFile

func CopyFile(ctx context.Context, s Storage, srcKey, destKey string) (*Metadata, error)

CopyFile copies a file from one key to another (requires download + upload)

func GetFileInfo

func GetFileInfo(ctx context.Context, s Storage, key string) (*Metadata, error)

GetFileInfo retrieves file information without downloading

func ListAll

func ListAll(ctx context.Context, s Storage) ([]*Metadata, error)

ListAll lists all files in storage (be careful with large buckets)

func ListByPrefix

func ListByPrefix(ctx context.Context, s Storage, prefix string) ([]*Metadata, error)

ListByPrefix lists all files with a given prefix

func UploadBytes

func UploadBytes(ctx context.Context, s Storage, key string, data []byte, contentType string) (*Metadata, error)

UploadBytes uploads byte data as a file

func UploadFile

func UploadFile(ctx context.Context, s Storage, key string, reader io.Reader, contentType string) (*Metadata, error)

UploadFile is a convenience function to upload a file with minimal configuration

func UploadMultipartSimple

func UploadMultipartSimple(ctx context.Context, s Storage, key string, fileHeader *multipart.FileHeader) (*Metadata, error)

UploadMultipartSimple uploads a multipart file with a simple key

func UploadPublic

func UploadPublic(ctx context.Context, s Storage, key string, reader io.Reader, contentType string) (*Metadata, error)

UploadPublic uploads a file and makes it publicly accessible

func UploadWithCache

func UploadWithCache(ctx context.Context, s Storage, key string, reader io.Reader, contentType string, cacheControl string) (*Metadata, error)

UploadWithCache uploads a file with cache control headers

func UploadWithMetadata

func UploadWithMetadata(ctx context.Context, s Storage, key string, reader io.Reader, contentType string, metadata map[string]string) (*Metadata, error)

UploadWithMetadata uploads a file with custom metadata

type MinioStorage

type MinioStorage struct {
	*logging.Log
	// contains filtered or unexported fields
}

func (*MinioStorage) Delete

func (s *MinioStorage) Delete(ctx context.Context, key string, opts *DeleteOptions) error

Delete removes a file

func (*MinioStorage) Download

func (s *MinioStorage) Download(ctx context.Context, key string, opts *DownloadOptions) (io.ReadCloser, error)

Download retrieves a file

func (*MinioStorage) Exists

func (s *MinioStorage) Exists(ctx context.Context, key string) (bool, error)

Exists checks if a file exists

func (*MinioStorage) GetMetadata

func (s *MinioStorage) GetMetadata(ctx context.Context, key string) (*Metadata, error)

GetMetadata retrieves file metadata without downloading

func (*MinioStorage) GetSignedURL

func (s *MinioStorage) GetSignedURL(ctx context.Context, key string, expiry time.Duration) (string, error)

GetSignedURL returns a temporary signed URL

func (*MinioStorage) GetURL

func (s *MinioStorage) GetURL(ctx context.Context, key string) (string, error)

GetURL returns a public URL for accessing the file

func (*MinioStorage) List

func (s *MinioStorage) List(ctx context.Context, opts *ListOptions) (*ListResult, error)

List lists files matching criteria

func (*MinioStorage) Start

func (s *MinioStorage) Start(ctx context.Context) error

func (*MinioStorage) Stop

func (s *MinioStorage) Stop(context.Context) error

func (*MinioStorage) Upload

func (s *MinioStorage) Upload(ctx context.Context, reader io.Reader, opts *UploadOptions) (*Metadata, error)

Upload uploads a file from an io.Reader

func (*MinioStorage) UploadMultipart

func (s *MinioStorage) UploadMultipart(ctx context.Context, fileHeader *multipart.FileHeader, opts *UploadOptions) (*Metadata, error)

UploadMultipart uploads from a multipart form file

type ProxyOptions

type ProxyOptions struct {
	// EnableCaching enables ETag-based caching
	EnableCaching bool

	// EnableRangeRequests enables HTTP range request support
	EnableRangeRequests bool

	// CacheControl sets the Cache-Control header
	CacheControl string

	// Attachment forces download instead of inline display
	Attachment bool
}

ProxyOptions configures file proxying behavior

func DefaultProxyOptions

func DefaultProxyOptions() *ProxyOptions

DefaultProxyOptions returns sensible defaults for proxying

type Storage

type Storage interface {
	lifecycle.StartStoper
	logging.Logger

	// Upload uploads a file from an io.Reader
	Upload(ctx context.Context, reader io.Reader, opts *UploadOptions) (*Metadata, error)

	// UploadMultipart uploads from a multipart form file (convenience method)
	UploadMultipart(ctx context.Context, fileHeader *multipart.FileHeader, opts *UploadOptions) (*Metadata, error)

	// Download retrieves a file
	Download(ctx context.Context, key string, opts *DownloadOptions) (io.ReadCloser, error)

	// Delete removes a file
	Delete(ctx context.Context, key string, opts *DeleteOptions) error

	// Exists checks if a file exists
	Exists(ctx context.Context, key string) (bool, error)

	// GetMetadata retrieves file metadata without downloading
	GetMetadata(ctx context.Context, key string) (*Metadata, error)

	// List lists files matching criteria
	List(ctx context.Context, opts *ListOptions) (*ListResult, error)

	// GetURL returns a public URL for accessing the file
	GetURL(ctx context.Context, key string) (string, error)

	// GetSignedURL returns a temporary signed URL (if supported)
	GetSignedURL(ctx context.Context, key string, expiry time.Duration) (string, error)
}

Storage is the unified interface for all storage operations

func NewMinioStorage

func NewMinioStorage(config Config) (Storage, error)

type UploadOptions

type UploadOptions struct {
	// Key is the storage path/key. If empty, auto-generated from filename
	Key string

	// ContentType overrides auto-detection
	ContentType string

	// Metadata allows custom key-value pairs
	Metadata map[string]string

	// ExtractImageDimensions automatically extracts width/height for images
	ExtractImageDimensions bool

	// CacheControl sets cache control headers
	CacheControl string

	// Public makes the file publicly accessible (if supported)
	Public bool
}

UploadOptions configures file upload behavior

func DefaultUploadOptions

func DefaultUploadOptions(key string) *UploadOptions

Helper function to create default upload options

Jump to

Keyboard shortcuts

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