Documentation
¶
Overview ¶
Package storage provides a small S3-backed object storage wrapper with basic size accounting.
Index ¶
- Constants
- type Backend
- type ObjectHead
- type PutOption
- type Storage
- func (s *Storage) Delete(ctx context.Context, head *ObjectHead) error
- func (s *Storage) Exists(key string) bool
- func (s *Storage) Get(ctx context.Context, head *ObjectHead) (io.ReadCloser, error)
- func (s *Storage) GetBody(ctx context.Context, head *ObjectHead) (io.ReadCloser, error)
- func (s *Storage) List() map[string]int64
- func (s *Storage) LoadState(ctx context.Context) error
- func (s *Storage) Put(ctx context.Context, body io.Reader, opts ...PutOption) (*ObjectHead, error)
- type StorageOption
Constants ¶
const ( // ErrMaxBytesReached indicates that reading more bytes would exceed the // configured storage limit. ErrMaxBytesReached = errStr("maxBytes reached") // ErrObjectNotFound indicates that an object does not exist. ErrObjectNotFound = errStr("object not found") )
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Backend ¶
type Backend interface {
PutObject(ctx context.Context, params *s3.PutObjectInput, optFns ...func(*s3.Options)) (*s3.PutObjectOutput, error)
GetObject(ctx context.Context, params *s3.GetObjectInput, optFns ...func(*s3.Options)) (*s3.GetObjectOutput, error)
DeleteObject(ctx context.Context, params *s3.DeleteObjectInput, optFns ...func(*s3.Options)) (*s3.DeleteObjectOutput, error)
ListObjectsV2(ctx context.Context, params *s3.ListObjectsV2Input, optFns ...func(*s3.Options)) (*s3.ListObjectsV2Output, error)
}
Backend is the minimal S3 API surface required by Storage.
type ObjectHead ¶
type ObjectHead struct {
// Key is the object identifier inside the bucket.
Key string
// Type is the object's content type.
Type string
// Size is the number of bytes stored for the object.
Size int64
// SHA256 is the hex-encoded SHA-256 digest of the stored body.
SHA256 string
}
ObjectHead describes an object stored by Storage.
type PutOption ¶
type PutOption func(*ObjectHead)
PutOption configures metadata for a Put request.
func WithContentType ¶
WithContentType sets the stored content type metadata for Put.
func WithSizeLimit ¶
WithSizeLimit sets the maximum number of bytes Put will read from body.
Providing a size limit lets Put reserve capacity up front and use a bounded reader, which is slightly faster than reserving per read. The final stored object may be smaller if body ends before the limit.
type Storage ¶
type Storage struct {
// contains filtered or unexported fields
}
Storage stores objects in a single bucket and tracks total and in-flight byte usage against maxSize.
func New ¶
func New(backend Backend, bucket string, maxSize int64, opts ...StorageOption) *Storage
New returns a Storage that uses backend to store objects in bucket and enforces maxSize as the total allowed byte usage.
func (*Storage) Delete ¶
func (s *Storage) Delete(ctx context.Context, head *ObjectHead) error
Delete removes an object from the bucket and subtracts its tracked size from storage usage.
func (*Storage) Exists ¶ added in v1.2.0
Exists reports whether key is currently tracked in storage.
func (*Storage) Get ¶
func (s *Storage) Get(ctx context.Context, head *ObjectHead) (io.ReadCloser, error)
Get opens an object body for reading.
The caller must close the returned reader.
func (*Storage) GetBody ¶ added in v1.2.1
func (s *Storage) GetBody(ctx context.Context, head *ObjectHead) (io.ReadCloser, error)
GetBody opens an object body for reading.
The caller must close the returned reader.
func (*Storage) List ¶ added in v1.2.0
List returns a copy of the tracked object sizes keyed by object name.
func (*Storage) LoadState ¶
LoadState refreshes Storage's tracked byte usage from the backend bucket.
Call LoadState after constructing Storage and before using it so in-memory accounting starts from the bucket's current size.
func (*Storage) Put ¶
Put stores body in the configured bucket and returns the resulting object metadata.
When WithSizeLimit is provided, Put can reserve storage up front and stream through a bounded reader, which is more efficient than reserving bytes on each read. The returned ObjectHead.Size is the number of bytes actually stored.
type StorageOption ¶ added in v1.2.1
type StorageOption func(*Storage)
StorageOption configures Storage.
func WithPublicEndpointURL ¶ added in v1.2.1
func WithPublicEndpointURL(rawURL string) StorageOption
WithPublicEndpointURL sets the base URL used by GetBody for public object downloads. When empty, GetBody uses the backend's GetObject method.