Documentation
¶
Index ¶
- Constants
- Variables
- func GenerateDownloadToken(key, secret string, expiresAt int64) string
- func GenerateToken(key, secret string, expiresAt int64, contentType string, maxSizeBytes int64) string
- func VerifyDownloadToken(key, token, secret string, expiresAt int64) bool
- func VerifyToken(key, token, secret string, expiresAt int64, contentType string, ...) bool
- type LocalConfig
- type LocalFSDriver
- func (d *LocalFSDriver) ContentPattern() string
- func (d *LocalFSDriver) Delete(ctx context.Context, key string) error
- func (d *LocalFSDriver) Get(ctx context.Context, key string) (io.ReadCloser, string, error)
- func (d *LocalFSDriver) GetDownloadURL(_ context.Context, key string) (string, error)
- func (d *LocalFSDriver) GetUploadURL(_ context.Context, key string, contentType string, maxSizeBytes int64) (string, error)
- func (d *LocalFSDriver) PresignTTL() time.Duration
- func (d *LocalFSDriver) Save(ctx context.Context, key string, body io.Reader, contentType string) error
- func (d *LocalFSDriver) VerifyDownloadToken(key, token string, expiresAt int64) bool
- func (d *LocalFSDriver) VerifyToken(key, token string, expiresAt int64, contentType string, maxSizeBytes int64) bool
- type LocalOption
- type S3Config
- type S3Driver
- func (d *S3Driver) Delete(ctx context.Context, key string) error
- func (d *S3Driver) Get(ctx context.Context, key string) (io.ReadCloser, string, error)
- func (d *S3Driver) GetDownloadURL(ctx context.Context, key string) (string, error)
- func (d *S3Driver) GetUploadURL(ctx context.Context, key string, contentType string, maxSizeBytes int64) (string, error)
- func (d *S3Driver) PresignTTL() time.Duration
- func (d *S3Driver) Save(ctx context.Context, key string, content io.Reader, contentType string) error
Constants ¶
const DefaultLocalRoutePrefix = "/api/v1/storage"
DefaultLocalRoutePrefix is the path the local driver's content routes sit under unless WithRoutePrefix sets another.
const DefaultMime = "application/octet-stream"
DefaultMime is the fallback MIME type when none is provided.
const DefaultPresignTTL = 15 * time.Minute
DefaultPresignTTL is the default time-to-live for presigned upload and download URLs.
Variables ¶
var ErrInvalidPath = errors.New("invalid path: traversal or invalid key not allowed")
ErrInvalidPath is returned when a key or resolved path is invalid (e.g. path traversal). Callers can use errors.Is(err, drivers.ErrInvalidPath) to detect validation failures.
Functions ¶
func GenerateDownloadToken ¶
GenerateDownloadToken creates an HMAC-SHA256 token specifically for download links (only signs key and expiration).
func GenerateToken ¶
func GenerateToken(key, secret string, expiresAt int64, contentType string, maxSizeBytes int64) string
GenerateToken creates an HMAC-SHA256 token signing multiple constraints.
func VerifyDownloadToken ¶
VerifyDownloadToken checks if a provided download token matches the expected signature.
Types ¶
type LocalConfig ¶ added in v0.2.0
type LocalConfig struct {
// BaseDir is the directory files are stored under (created if absent).
BaseDir string `yaml:"baseDir"`
// PublicURL is the origin of the server that serves the driver's
// content routes (e.g. http://localhost:8080). See
// storage.LocalContentHandler.
PublicURL string `yaml:"publicURL"`
// RoutePrefix is the path the content routes sit under on that server,
// e.g. "/files" for routes at /files/{key}/content. Empty means
// DefaultLocalRoutePrefix ("/api/v1/storage"). Otherwise it follows the
// rules of WithRoutePrefix.
RoutePrefix string `yaml:"routePrefix"`
// PutSecret signs presigned upload URLs for the local driver.
PutSecret string `yaml:"putSecret"`
}
LocalConfig holds configuration for the local filesystem storage backend.
LocalConfig carries yaml struct tags, so it can be embedded in a larger application config struct and populated generically (e.g. via yaml.Unmarshal).
func (LocalConfig) Validate ¶ added in v0.2.0
func (c LocalConfig) Validate() error
Validate ensures the local storage configuration is usable.
type LocalFSDriver ¶
type LocalFSDriver struct {
BaseDir string
PublicURL string
// contains filtered or unexported fields
}
LocalFSDriver implements StorageDriver for local disk with directory hashing
func NewLocalFSDriver ¶
func NewLocalFSDriver(baseDir, publicURL, secretKey string, presignTTL time.Duration, opts ...LocalOption) (*LocalFSDriver, error)
NewLocalFSDriver creates a new LocalFSDriver. baseDir is where files will be stored. publicURL is the origin of the server that serves ContentPattern (e.g. http://localhost:8080), using storage.LocalContentHandler. secretKey is the secret used for HMAC signing of local-put upload URLs. presignTTL is the default time-to-live for presigned URLs.
func (*LocalFSDriver) ContentPattern ¶ added in v0.3.0
func (d *LocalFSDriver) ContentPattern() string
ContentPattern is the ServeMux path pattern of the driver's content routes, the route prefix followed by "/{key}/content". Upload and download URLs point at it on PublicURL, and storage.LocalContentHandler serves it.
func (*LocalFSDriver) Delete ¶
func (d *LocalFSDriver) Delete(ctx context.Context, key string) error
func (*LocalFSDriver) Get ¶
func (d *LocalFSDriver) Get(ctx context.Context, key string) (io.ReadCloser, string, error)
func (*LocalFSDriver) GetDownloadURL ¶
func (*LocalFSDriver) GetUploadURL ¶
func (d *LocalFSDriver) GetUploadURL(_ context.Context, key string, contentType string, maxSizeBytes int64) (string, error)
GetUploadURL returns a presigned URL pointing to the PUT route on ContentPattern. Note: This method does NOT create the file on disk. It only signs the security constraints (key, expiration, size limit). The actual resource allocation (file creation) happens in Save() when the PUT request is eventually processed, matching S3's deferred behavior.
func (*LocalFSDriver) PresignTTL ¶ added in v0.3.0
func (d *LocalFSDriver) PresignTTL() time.Duration
PresignTTL reports how long the driver's presigned URLs stay valid.
func (*LocalFSDriver) VerifyDownloadToken ¶
func (d *LocalFSDriver) VerifyDownloadToken(key, token string, expiresAt int64) bool
VerifyDownloadToken checks if a provided download token is valid and not expired.
func (*LocalFSDriver) VerifyToken ¶
func (d *LocalFSDriver) VerifyToken(key, token string, expiresAt int64, contentType string, maxSizeBytes int64) bool
VerifyToken checks if a token is valid for a given key and constraints using the driver's secret.
type LocalOption ¶ added in v0.3.0
type LocalOption func(*LocalFSDriver)
LocalOption configures a LocalFSDriver.
func WithRoutePrefix ¶ added in v0.3.0
func WithRoutePrefix(prefix string) LocalOption
WithRoutePrefix sets the path the driver's content routes sit under, e.g. "/files" for routes at /files/{key}/content. It must start with "/", must not end with one, and must not contain {, }, ?, #, whitespace, or empty, "." or ".." segments; NewLocalFSDriver returns an error naming the rule a bad prefix breaks. Without it the prefix is DefaultLocalRoutePrefix.
type S3Config ¶ added in v0.2.0
type S3Config struct {
// Endpoint is an optional custom endpoint URL for S3-compatible stores
// (e.g. MinIO or LocalStack). Empty targets AWS S3.
Endpoint string `yaml:"endpoint"`
// Bucket is the S3 bucket that files are stored in. Required.
Bucket string `yaml:"bucket"`
// Region is the AWS region of the bucket. Required.
Region string `yaml:"region"`
// AccessKey and SecretKey are optional static credentials. They must be
// set together; when both are empty the default AWS credential chain is
// used.
AccessKey string `yaml:"accessKey"`
SecretKey string `yaml:"secretKey"`
// PublicURL is an optional base URL files are served from, for a
// CDN-fronted or otherwise rewritten bucket.
PublicURL string `yaml:"publicURL"`
}
S3Config holds configuration for the S3 storage backend.
S3Config carries yaml struct tags, so it can be embedded in a larger application config struct and populated generically (e.g. via yaml.Unmarshal).
type S3Driver ¶
type S3Driver struct {
Client *s3.Client
PresignClient *s3.PresignClient
Bucket string
PublicURL string // Optional: Base URL if files are public
// contains filtered or unexported fields
}
S3Driver implements StorageDriver for S3-compatible storage. Save streams via PutObject with no size limit of its own; presigned uploads are limited by the ContentLength signed into the URL.
func NewS3Driver ¶
func (*S3Driver) GetDownloadURL ¶
func (*S3Driver) GetUploadURL ¶
func (*S3Driver) PresignTTL ¶ added in v0.3.0
PresignTTL reports how long the driver's presigned URLs stay valid.