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") // ErrMutationIDNotFound is returned when mutation ID is not found ErrMutationIDNotFound = errors.New("mutation ID not found") // 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 ¶ added in v0.38.1
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 ¶ added in v0.39.0
func ParseFilesFromSource[T FileSource](source T, keys ...string) (map[string][]File, error)
ParseFilesFromSource extracts files from any source using generics
func ProcessFilesForMutation ¶ added in v0.39.0
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 ¶ added in v0.3.1
RemoveFileFromContext removes the file from the context based on the file ID
func UpdateFileInContextByKey ¶ added in v0.3.1
UpdateFileInContextByKey updates the file in the context based on the key and the file ID
func WaitForUploads ¶ added in v0.39.0
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 ¶ added in v0.38.1
type BufferedReader struct {
// contains filtered or unexported fields
}
BufferedReader wraps file data and provides both Reader and ReadSeeker interfaces
func NewBufferedReader ¶ added in v0.38.1
func NewBufferedReader(data []byte) *BufferedReader
NewBufferedReader creates a BufferedReader from raw data
func NewBufferedReaderFromReader ¶ added in v0.38.1
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 ¶ added in v0.38.1
func NewBufferedReaderFromReaderWithLimit(r io.Reader, maxSize int64) (*BufferedReader, error)
NewBufferedReaderFromReaderWithLimit creates a BufferedReader from an io.Reader with a size limit
func (*BufferedReader) Close ¶ added in v0.38.1
func (br *BufferedReader) Close() error
Close implements io.Closer (no-op for memory buffer)
func (*BufferedReader) Data ¶ added in v0.38.1
func (br *BufferedReader) Data() []byte
Data returns a copy of the underlying data
func (*BufferedReader) NewReadSeeker ¶ added in v0.38.1
func (br *BufferedReader) NewReadSeeker() io.ReadSeeker
NewReadSeeker creates a new independent ReadSeeker from the buffered data
func (*BufferedReader) NewReader ¶ added in v0.38.1
func (br *BufferedReader) NewReader() io.Reader
NewReader creates a new independent reader from the buffered data
func (*BufferedReader) Read ¶ added in v0.38.1
func (br *BufferedReader) Read(p []byte) (n int, err error)
Read implements io.Reader
func (*BufferedReader) Reset ¶ added in v0.38.1
func (br *BufferedReader) Reset()
Reset resets the reader to the beginning
func (*BufferedReader) Seek ¶ added in v0.38.1
func (br *BufferedReader) Seek(offset int64, whence int) (int64, error)
Seek implements io.Seeker
func (*BufferedReader) Size ¶ added in v0.38.1
func (br *BufferedReader) Size() int64
Size returns the total size of the buffered data
type DownloadOptions ¶ added in v0.39.0
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 ¶ added in v0.39.0
type FileMetadata = storage.FileMetadata
File aliases storage.File so callers can reference a single top-level type.
type FileSource ¶ added in v0.39.0
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 ¶ added in v0.39.0
type GenericMutationAdapter[T any] struct { // contains filtered or unexported fields }
GenericMutationAdapter adapts existing ent GenericMutation interface to our Mutation interface
func (*GenericMutationAdapter[T]) ID ¶ added in v0.39.0
func (a *GenericMutationAdapter[T]) ID() (string, error)
ID implements the Mutation interface
func (*GenericMutationAdapter[T]) Type ¶ added in v0.39.0
func (a *GenericMutationAdapter[T]) Type() string
Type implements the Mutation interface
type LenReader ¶ added in v0.38.1
type LenReader interface {
Len() int
}
LenReader describes readers that expose remaining length semantics (e.g. *bytes.Reader).
type ParentObject ¶ added in v0.3.1
type ParentObject = storage.ParentObject
File aliases storage.File so callers can reference a single top-level type.
type ProviderHints ¶ added in v0.39.0
type ProviderHints = storage.ProviderHints
File aliases storage.File so callers can reference a single top-level type.
type SizedReader ¶ added in v0.38.1
type SizedReader interface {
Size() int64
}
SizedReader describes readers that can report their size without consuming the stream.
type StatReader ¶ added in v0.38.1
StatReader describes readers backed by file descriptors that can return stat information.
type UploadOptions ¶ added in v0.39.0
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 makes sure we only accept a valid mimetype. It takes in an array of supported mimes MimeTypeValidator is a validator factory that ensures the file's content type matches one of the provided types When validation fails it wraps ErrUnsupportedMimeType and includes a normalized mime type without charset parameters
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 |
|
types
Package storagetypes contains types used across the storage package
|
Package storagetypes contains types used across the storage package |