Documentation
¶
Overview ¶
storage.go
Index ¶
- Constants
- Variables
- type ClientOption
- func WithHTTPClient(client *http.Client) ClientOption
- func WithRetryMaxAttempts(attempts int) ClientOption
- func WithRetryMode(mode aws.RetryMode) ClientOption
- func WithS3Client(client S3Client) ClientOption
- func WithS3ClientOption(option func(*s3.Options)) ClientOption
- func WithS3ConfigOption(option func(*s3config.LoadOptions) error) ClientOption
- type Config
- type File
- type S3Client
- type Storage
- type UploadFromRequestOptions
- type UploadOptions
Constants ¶
View Source
const ( // DefaultMaxFileSize represents the default maximum allowed file size (10MB) DefaultMaxFileSize int64 = 10485760 // DefaultUploadBasePath is the default base path for uploads DefaultUploadBasePath = "uploads" // DefaultFieldName is the default form field name for file uploads DefaultFieldName = "file" // DefaultConnectTimeout is the default connection timeout DefaultConnectTimeout = 5 * time.Second // DefaultRequestTimeout is the default request timeout DefaultRequestTimeout = 60 * time.Second // DefaultMaxRetries is the default number of retry attempts DefaultMaxRetries = 3 // DefaultRetryBaseDelay is the default delay between retry attempts DefaultRetryBaseDelay = 100 * time.Millisecond )
Default constants for configuration
Variables ¶
View Source
var ( // ErrFailedToUploadFile indicates a failure during file upload. ErrFailedToUploadFile = errors.New("failed to upload file") // ErrFileTooLarge indicates that the file size exceeds the allowed maximum. ErrFileTooLarge = errors.New("file size exceeds the maximum allowed limit") // ErrFailedToDeleteFile indicates a failure during file deletion. ErrFailedToDeleteFile = errors.New("failed to delete file") // ErrFailedToDeleteDirectory indicates a failure during directory deletion. ErrFailedToDeleteDirectory = errors.New("failed to delete directory") // ErrFailedToListFiles indicates a failure during file listing. ErrFailedToListFiles = errors.New("failed to list files") // ErrInvalidRequest indicates an invalid request or missing required file data. ErrInvalidRequest = errors.New("invalid request or missing file data") // ErrMissingConfig indicates that the required configuration is missing. // This error is returned when the minimum required configuration is not provided. ErrMissingConfig = errors.New("missing required configuration") // ErrInvalidEndpoint indicates that the provided endpoint is invalid. // This error is returned when the endpoint URL is not a valid URL. ErrInvalidEndpoint = errors.New("invalid endpoint URL") // ErrFailedToLoadConfig indicates a failure to load the AWS configuration. ErrFailedToLoadConfig = errors.New("failed to load AWS configuration") )
Package-level error declarations.
Functions ¶
This section is empty.
Types ¶
type ClientOption ¶
type ClientOption func(*clientOptions)
ClientOption defines a function that configures the storage client
func WithHTTPClient ¶
func WithHTTPClient(client *http.Client) ClientOption
WithHTTPClient sets a custom HTTP client for S3 requests
func WithRetryMaxAttempts ¶
func WithRetryMaxAttempts(attempts int) ClientOption
WithRetryMaxAttempts sets the maximum number of retry attempts
func WithRetryMode ¶
func WithRetryMode(mode aws.RetryMode) ClientOption
WithRetryMode sets the retry mode for AWS requests
func WithS3Client ¶
func WithS3Client(client S3Client) ClientOption
WithS3Client sets a custom pre-configured S3 client
func WithS3ClientOption ¶
func WithS3ClientOption(option func(*s3.Options)) ClientOption
WithS3ClientOption adds a custom S3 client option
func WithS3ConfigOption ¶
func WithS3ConfigOption(option func(*s3config.LoadOptions) error) ClientOption
WithS3ConfigOption adds a custom S3 config option
type Config ¶
type Config struct { Key string `env:"STORAGE_KEY,required"` Secret string `env:"STORAGE_SECRET,required"` Region string `env:"STORAGE_REGION,required"` Bucket string `env:"STORAGE_BUCKET,required"` Endpoint string `env:"STORAGE_ENDPOINT,required"` CDN string `env:"STORAGE_CDN"` MaxFileSize int64 `env:"STORAGE_MAX_FILE_SIZE" envDefault:"10485760"` // 10MB UploadBasePath string `env:"STORAGE_BASE_PATH" envDefault:"uploads"` ForcePathStyle bool `env:"STORAGE_FORCE_PATH_STYLE" envDefault:"false"` ConnectTimeout time.Duration `env:"STORAGE_CONNECT_TIMEOUT" envDefault:"5s"` RequestTimeout time.Duration `env:"STORAGE_REQUEST_TIMEOUT" envDefault:"60s"` MaxRetries int `env:"STORAGE_MAX_RETRIES" envDefault:"3"` RetryBaseDelay time.Duration `env:"STORAGE_RETRY_BASE_DELAY" envDefault:"100ms"` }
Config holds the configuration required to initialize the S3 client.
type S3Client ¶
type S3Client interface { PutObject(ctx context.Context, params *s3.PutObjectInput, optFns ...func(*s3.Options)) (*s3.PutObjectOutput, error) HeadObject(ctx context.Context, params *s3.HeadObjectInput, optFns ...func(*s3.Options)) (*s3.HeadObjectOutput, error) ListObjectsV2(ctx context.Context, params *s3.ListObjectsV2Input, optFns ...func(*s3.Options)) (*s3.ListObjectsV2Output, error) DeleteObject(ctx context.Context, params *s3.DeleteObjectInput, optFns ...func(*s3.Options)) (*s3.DeleteObjectOutput, error) DeleteObjects(ctx context.Context, params *s3.DeleteObjectsInput, optFns ...func(*s3.Options)) (*s3.DeleteObjectsOutput, error) }
S3Client defines the interface for S3 client methods used by the storage package
type Storage ¶
type Storage interface { GetFileURL(path string) string UploadFile(ctx context.Context, file []byte, opts UploadOptions) (File, error) UploadFileFromRequest(ctx context.Context, r *http.Request, opts UploadFromRequestOptions) (File, error) ListFiles(ctx context.Context, path string) ([]File, error) DeleteFile(ctx context.Context, path string) error DeleteDirectory(ctx context.Context, path string) error }
Storage interface and related types.
Click to show internal directories.
Click to hide internal directories.