Documentation
¶
Overview ¶
Package objects provides a clean, modern object storage service with dynamic multi-provider support, context-based client injection, and integration with external credential systems. This package supersedes the original objects package with better separation of concerns and support for per-tenant storage providers.
Index ¶
- Constants
- Variables
- func AddUpload()
- func DoneUpload()
- func GetFileIDsFromContext(ctx context.Context) []string
- func InferReaderSize(r io.Reader) (int64, bool)
- func ParseFilesFromSource[T FileSource](source T, keys ...string) (map[string][]File, error)
- func ProcessFilesForMutation[T Mutation](ctx context.Context, mutation T, key string, parentType ...string) (context.Context, error)
- func ReaderToSeeker(r io.Reader) (io.ReadSeeker, error)
- func RemoveFileFromContext(ctx context.Context, f File) context.Context
- func UpdateFileInContextByKey(ctx context.Context, key string, f File) context.Context
- func WaitForUploads()
- func WriteFilesToContext(ctx context.Context, f Files) context.Context
- type BufferedReader
- func (br *BufferedReader) Close() error
- func (br *BufferedReader) Data() []byte
- func (br *BufferedReader) NewReadSeeker() io.ReadSeeker
- func (br *BufferedReader) NewReader() io.Reader
- func (br *BufferedReader) Read(p []byte) (n int, err error)
- func (br *BufferedReader) Reset()
- func (br *BufferedReader) Seek(offset int64, whence int) (int64, error)
- func (br *BufferedReader) Size() int64
- type DownloadOptions
- type File
- type FileContextKey
- type FileMetadata
- type FileSource
- type Files
- type GenericMutationAdapter
- type LenReader
- type Mutation
- type ParentObject
- type ProviderHints
- type SizedReader
- type StatReader
- type UploadOptions
- type ValidationFunc
Constants ¶
const (
// MaxInMemorySize is the maximum file size we'll buffer in memory (10MB)
MaxInMemorySize = 10 * 1024 * 1024
)
Variables ¶
var ( // ErrNoStorageProvider is returned when no storage provider is available ErrNoStorageProvider = errors.New("no storage provider available") // ErrInsufficientProviderInfo is returned when insufficient information to resolve storage client ErrInsufficientProviderInfo = errors.New("insufficient information to resolve storage client: need integration_id+hush_id or organization_id") // ErrProviderHintsRequired is returned when provider hints are required for file upload ErrProviderHintsRequired = errors.New("provider hints required for file upload") // ErrReaderCannotBeNil is returned when a nil reader is provided to BufferedReader ErrReaderCannotBeNil = errors.New("reader cannot be nil") // ErrFailedToReadData is returned when reading data from a reader fails ErrFailedToReadData = errors.New("failed to read data from reader") // ErrFileSizeExceedsLimit is returned when file size exceeds the specified limit ErrFileSizeExceedsLimit = errors.New("file size exceeds limit") // ErrUnsupportedMimeType is returned when an unsupported mime type is uploaded ErrUnsupportedMimeType = errors.New("unsupported mime type uploaded") )
Functions ¶
func GetFileIDsFromContext ¶
GetFileIDsFromContext returns the file IDs from the context that are associated with the request
func InferReaderSize ¶
InferReaderSize attempts to determine the total size of the provided reader without modifying its current position. It returns the reported size and true when available.
func ParseFilesFromSource ¶
func ParseFilesFromSource[T FileSource](source T, keys ...string) (map[string][]File, error)
ParseFilesFromSource extracts files from any source using generics
func ProcessFilesForMutation ¶
func ProcessFilesForMutation[T Mutation](ctx context.Context, mutation T, key string, parentType ...string) (context.Context, error)
ProcessFilesForMutation is a generic helper for ent hooks that: 1. Gets files from context using the provided key 2. Updates files with parent information from mutation 3. Updates context with modified files This replaces the pattern of individual checkXXXFile functions
func ReaderToSeeker ¶
func ReaderToSeeker(r io.Reader) (io.ReadSeeker, error)
ReaderToSeeker function takes an io.Reader as input and returns an io.ReadSeeker which can be used to upload files to the object storage If the reader is already a ReadSeeker (e.g., BufferedReader from injectFileUploader), it returns it directly. For files under MaxInMemorySize (10MB), it uses in-memory buffering for efficiency. For larger files, it falls back to temporary file storage.
func RemoveFileFromContext ¶
RemoveFileFromContext removes the file from the context based on the file ID
func UpdateFileInContextByKey ¶
UpdateFileInContextByKey updates the file in the context based on the key and the file ID
func WaitForUploads ¶
func WaitForUploads()
WaitForUploads waits for all in-flight uploads to complete
func WriteFilesToContext ¶
WriteFilesToContext retrieves any existing files from the context, appends the new files to the existing files map based on the form field name, then returns a new context with the updated files map stored in it
Types ¶
type BufferedReader ¶
type BufferedReader struct {
// contains filtered or unexported fields
}
BufferedReader wraps file data and provides both Reader and ReadSeeker interfaces
func NewBufferedReader ¶
func NewBufferedReader(data []byte) *BufferedReader
NewBufferedReader creates a BufferedReader from raw data
func NewBufferedReaderFromReader ¶
func NewBufferedReaderFromReader(r io.Reader) (*BufferedReader, error)
NewBufferedReaderFromReader creates a BufferedReader from an io.Reader This is the robust method for handling inbound file data that can work with all providers It buffers files up to MaxInMemorySize - if the file exceeds this, it returns an error indicating the caller should use disk-based buffering instead
func NewBufferedReaderFromReaderWithLimit ¶
func NewBufferedReaderFromReaderWithLimit(r io.Reader, maxSize int64) (*BufferedReader, error)
NewBufferedReaderFromReaderWithLimit creates a BufferedReader from an io.Reader with a size limit
func (*BufferedReader) Close ¶
func (br *BufferedReader) Close() error
Close implements io.Closer (no-op for memory buffer)
func (*BufferedReader) Data ¶
func (br *BufferedReader) Data() []byte
Data returns a copy of the underlying data
func (*BufferedReader) NewReadSeeker ¶
func (br *BufferedReader) NewReadSeeker() io.ReadSeeker
NewReadSeeker creates a new independent ReadSeeker from the buffered data
func (*BufferedReader) NewReader ¶
func (br *BufferedReader) NewReader() io.Reader
NewReader creates a new independent reader from the buffered data
func (*BufferedReader) Read ¶
func (br *BufferedReader) Read(p []byte) (n int, err error)
Read implements io.Reader
func (*BufferedReader) Reset ¶
func (br *BufferedReader) Reset()
Reset resets the reader to the beginning
func (*BufferedReader) Seek ¶
func (br *BufferedReader) Seek(offset int64, whence int) (int64, error)
Seek implements io.Seeker
func (*BufferedReader) Size ¶
func (br *BufferedReader) Size() int64
Size returns the total size of the buffered data
type DownloadOptions ¶
type DownloadOptions = storage.DownloadOptions
File aliases storage.File so callers can reference a single top-level type.
type FileContextKey ¶
type FileContextKey struct {
Files Files
}
FileContextKey is the context key for the files This is the key that is used to store the files in the context, which is then used to retrieve the files in subsequent parts of the request this is different than the `key` in the multipart form, which is the form field name that the file was uploaded with
type FileMetadata ¶
type FileMetadata = storage.FileMetadata
File aliases storage.File so callers can reference a single top-level type.
type FileSource ¶
FileSource represents any source that can provide file uploads. The tilde (~) allows for types that are identical or aliases to the specified types.
type GenericMutationAdapter ¶
type GenericMutationAdapter[T any] struct { // contains filtered or unexported fields }
GenericMutationAdapter adapts existing ent GenericMutation interface to our Mutation interface
func (*GenericMutationAdapter[T]) ID ¶
func (a *GenericMutationAdapter[T]) ID() (string, bool)
ID implements the Mutation interface
func (*GenericMutationAdapter[T]) Type ¶
func (a *GenericMutationAdapter[T]) Type() string
Type implements the Mutation interface
type LenReader ¶
type LenReader interface {
Len() int
}
LenReader describes readers that expose remaining length semantics (e.g. *bytes.Reader).
type ParentObject ¶
type ParentObject = storage.ParentObject
File aliases storage.File so callers can reference a single top-level type.
type ProviderHints ¶
type ProviderHints = storage.ProviderHints
File aliases storage.File so callers can reference a single top-level type.
type SizedReader ¶
type SizedReader interface {
Size() int64
}
SizedReader describes readers that can report their size without consuming the stream.
type StatReader ¶
StatReader describes readers backed by file descriptors that can return stat information.
type UploadOptions ¶
type UploadOptions = storage.UploadOptions
File aliases storage.File so callers can reference a single top-level type.
type ValidationFunc ¶
ValidationFunc is a type that can be used to dynamically validate a file
func ChainValidators ¶
func ChainValidators(validators ...ValidationFunc) ValidationFunc
ChainValidators returns a validator that accepts multiple validating criteria
func MimeTypeValidator ¶
func MimeTypeValidator(validMimeTypes ...string) ValidationFunc
MimeTypeValidator returns a validator that accepts a file when its content type matches one of validMimeTypes. Both sides are normalized: structured-parameter suffixes (e.g. "; charset=utf-8") are stripped before comparison so an allowlist entry of "text/html" matches an incoming "text/html; charset=utf-8" returned by MIME sniffers.
Source Files
¶
Directories
¶
| Path | Synopsis |
|---|---|
|
providers/disk
Package disk is the local disk storage provider for objects service
|
Package disk is the local disk storage provider for objects service |
|
providers/r2
Package r2 is the Cloudflare R2 storage provider for objects service
|
Package r2 is the Cloudflare R2 storage provider for objects service |
|
providers/s3
Package s3 is the AWS S3 storage provider for objects service
|
Package s3 is the AWS S3 storage provider for objects service |
|
proxy
Package proxy implements a storage proxy that provides presigned URL generation
|
Package proxy implements a storage proxy that provides presigned URL generation |