objects

package
v1.52.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 27, 2025 License: MPL-2.0 Imports: 6 Imported by: 0

Documentation

Overview

Package objects provides Encore applications with the ability to create and use Object Storage buckets (like for example Amazon S3) for storing and retrieving files in a cloud-agnostic manner.

For more information see https://encore.dev/docs/primitives/object-storage

Index

Constants

This section is empty.

Variables

View Source
var (
	// ErrObjectNotFound is returned when requested object does not exist in the bucket.
	ErrObjectNotFound = errors.New("objects: object doesn't exist")

	// ErrPreconditionFailed is returned when a precondition for an operation is not met,
	// such as when an object already exists and Preconditions.NotExists is true.
	ErrPreconditionFailed = errors.New("objects: precondition failed")

	// ErrInvalidArgument is returned when an argument for an operation is invalid or out
	// of bounds. Such as when a too long time-to-live is passed to a sign URL operation.
	ErrInvalidArgument = errors.New("objects: invalid argument")
)

Functions

func BucketRef

func BucketRef[P BucketPerms](bucket *Bucket) (_ P)

BucketRef returns an interface reference to a bucket, that can be freely passed around within a service without being subject to Encore's typical static analysis restrictions that normally apply to *Bucket objects.

This works because using BucketRef effectively declares which operations you want to be able to perform since the type argument P must be a permission-declaring interface (implementing BucketPerms).

The returned reference is scoped down to those permissions.

For example:

var MyBucket = objects.NewBucket(...)
var ref = objects.BucketRef[objects.ReadWriter](MyBucket)
// ref.Upload(...) can now be used to upload objects to MyBucket.

Multiple permissions can be combined by defining a custom interface that embeds multiple permission interfaces:

var ref = objects.BucketRef[interface { objects.Uploader; objects.Downloader }](MyBucket)

func WithPreconditions

func WithPreconditions(pre Preconditions) (_ withPreconditionsOption)

WithPreconditions is an UploadOption for only uploading an object if certain preconditions are met.

func WithTTL added in v1.46.1

func WithTTL(TTL time.Duration) (_ withTTLOption)

WithTTL is used for signed URLs, to specify the lifetime of the generated URL. The max value is seven days. The default lifetime, if this option is missing, is one hour.

func WithUploadAttrs

func WithUploadAttrs(attrs UploadAttrs) (_ withUploadAttrsOption)

WithUploadAttrs is an UploadOption for specifying additional object attributes to set during upload.

func WithVersion

func WithVersion(version string) (_ withVersionOption)

WithVersion specifies that the operation should be performed against the provided version of the object.

Types

type AttrsOption

type AttrsOption interface {
	// contains filtered or unexported methods
}

AttrsOption describes available options for the Attrs operation.

type Attrser

type Attrser interface {
	// Attrs resolves the attributes of an object.
	Attrs(ctx context.Context, object string, options ...AttrsOption) (*ObjectAttrs, error)

	// Exists checks whether an object exists in the bucket.
	Exists(ctx context.Context, object string, options ...ExistsOption) (bool, error)
}

Attrser is the interface for resolving objects' attributes in a bucket. It can be used in conjunction with BucketRef to declare a reference that can check object attributes in a bucket.

For example:

var MyBucket = objects.NewBucket(...)
var ref = objects.BucketRef[objects.Attrser](MyBucket)

The ref object can then be used to remove objects and can be passed around freely within the service, without being subject to Encore's static analysis restrictions that apply to MyBucket.

type Bucket

type Bucket struct {
	// contains filtered or unexported fields
}

Bucket represents an object storage bucket, containing a set of files.

See NewBucket for more information on how to declare a Bucket.

func Named

func Named(name constStr) (_ *Bucket)

Named returns a database object connected to the database with the given name.

The name must be a string literal constant, to facilitate static analysis.

func NewBucket

func NewBucket(name string, cfg BucketConfig) (_ *Bucket)

NewBucket declares a new object storage bucket.

See https://encore.dev/docs/primitives/object-storage for more information.

func (*Bucket) Attrs

func (*Bucket) Attrs(ctx context.Context, object string, options ...AttrsOption) (_ *ObjectAttrs, _ error)

Attrs returns the attributes of an object in the bucket. If the object does not exist, it returns ErrObjectNotFound.

func (*Bucket) Download

func (*Bucket) Download(ctx context.Context, object string, options ...DownloadOption) (_ *Reader)

Download downloads an object from the bucket. Any error is encountered is reported by the methods on *Reader. To check if the operation failed, call (*Reader).Err.

If the object does not exist, the error may be checked with errors.Is(err, ErrObjectNotFound).

func (*Bucket) Exists

func (*Bucket) Exists(ctx context.Context, object string, options ...ExistsOption) (_ bool, _ error)

Exists reports whether an object exists in the bucket.

func (*Bucket) List

func (*Bucket) List(ctx context.Context, query *Query, options ...ListOption) (_ iter.Seq2[*ListEntry, error])

List lists objects in the bucket.

func (*Bucket) PublicURL added in v1.44.6

func (*Bucket) PublicURL(object string, options ...PublicURLOption) (_ *url.URL)

PublicURL returns the public URL for accessing an object in the bucket.

func (*Bucket) Remove

func (*Bucket) Remove(ctx context.Context, object string, options ...RemoveOption) (_ error)

Remove removes an object from the bucket.

func (*Bucket) SignedDownloadURL added in v1.46.1

func (*Bucket) SignedDownloadURL(ctx context.Context, object string, options ...DownloadURLOption) (_ *SignedDownloadURL, _ error)

Generates an external URL to allow downloading an object from the bucket.

Anyone with possession of the URL can read the given object without any additional auth.

func (*Bucket) SignedUploadURL added in v1.46.1

func (*Bucket) SignedUploadURL(ctx context.Context, object string, options ...UploadURLOption) (_ *SignedUploadURL, _ error)

Generates an external URL to allow uploading an object to the bucket.

Anyone with possession of the URL can write to the given object name without any additional auth.

func (*Bucket) Upload

func (*Bucket) Upload(ctx context.Context, object string, options ...UploadOption) (_ *Writer)

Upload uploads a new object to the bucket.

The returned writer must be successfully closed for the upload to complete. To abort the upload, call (*Writer).Abort or cancel the provided context.

type BucketConfig

type BucketConfig struct {
	// Whether objects in the bucket should be publicly accessible, via CDN.
	Public bool

	// Whether objects stored in the bucket should be versioned.
	//
	// If true, the bucket will store multiple versions of each object
	// whenever it changes, as opposed to overwriting the old version.
	Versioned bool
}

BucketConfig is the configuration for a Bucket.

type BucketPerms

type BucketPerms interface {
}

BucketPerms is the type constraint for all permission-declaring interfaces that can be used with BucketRef.

type DownloadOption

type DownloadOption interface {
	// contains filtered or unexported methods
}

DownloadOption describes available options for the Download operation.

type DownloadURLOption added in v1.46.1

type DownloadURLOption interface {
	// contains filtered or unexported methods
}

DownloadURLOption describes available options for the SignedDownloadURL operation.

type Downloader

type Downloader interface {
	// Download downloads an object from the bucket.
	Download(ctx context.Context, object string, options ...DownloadOption) *Reader
}

Downloader is the interface for downloading objects from a bucket. It can be used in conjunction with BucketRef to declare a reference that can download objects from the bucket.

For example:

var MyBucket = objects.NewBucket(...)
var ref = objects.BucketRef[objects.Downloader](MyBucket)

The ref object can then be used to download objects and can be passed around freely within the service, without being subject to Encore's static analysis restrictions that apply to MyBucket.

type ExistsOption

type ExistsOption interface {
	// contains filtered or unexported methods
}

ExistsOption describes available options for the Exists operation.

type ListEntry

type ListEntry struct {
	// The name of the object.
	Name string
	// The size of the object, in bytes.
	Size int64
	// The computed ETag of the object.
	ETag string
}

ListEntry describes an objects during listing.

type ListOption

type ListOption interface {
	// contains filtered or unexported methods
}

ListOption describes available options for the List operation.

type Lister

type Lister interface {
	// List lists objects in the bucket.
	List(ctx context.Context, query *Query, options ...ListOption) iter.Seq2[*ListEntry, error]
}

Lister is the interface for listing objects in a bucket. It can be used in conjunction with BucketRef to declare a reference that can list objects in the bucket.

For example:

var MyBucket = objects.NewBucket(...)
var ref = objects.BucketRef[objects.Lister](MyBucket)

The ref object can then be used to list objects and can be passed around freely within the service, without being subject to Encore's static analysis restrictions that apply to MyBucket.

type ObjectAttrs

type ObjectAttrs struct {
	// The name of the object.
	Name string

	// The version of the object, if bucket versioning is enabled.
	Version string

	// The content type of the object, if set during upload.
	ContentType string

	// The size of the object, in bytes.
	Size int64

	// The computed ETag of the object.
	ETag string
}

ObjectAttrs describes the attributes of an object.

type Preconditions

type Preconditions struct {
	// NotExists specifies that the object must not exist prior to uploading.
	NotExists bool
}

Preconditions are the available preconditions for an upload operation.

type PublicURLOption added in v1.44.6

type PublicURLOption interface {
	// contains filtered or unexported methods
}

PublicURLOption describes available options for the PublicURL operation.

type PublicURLer added in v1.44.6

type PublicURLer interface {
	// PublicURL resolves the public URL for retrieving an object.
	PublicURL(object string, options ...PublicURLOption) *url.URL
}

PublicURLer is the interface for resolving the public URL for an object. It can be used in conjunction with BucketRef to declare a reference that can resolve an object's public URL.

For example:

var MyBucket = objects.NewBucket(...)
var ref = objects.BucketRef[objects.PublicURLer](MyBucket)

The ref object can then be used to remove objects and can be passed around freely within the service, without being subject to Encore's static analysis restrictions that apply to MyBucket.

type Query

type Query struct {
	// Prefix indicates to only return objects
	// whose name starts with the given prefix.
	Prefix string

	// Maximum number of objects to return. Zero means no limit.
	Limit int64
}

Query describes the set of objects to query for using List.

type ReadWriter

ReadWriter is a utility permission interface that provides all the read-write permissions.

type Reader

type Reader struct {
	// contains filtered or unexported fields
}

Reader is the reader for an object being downloaded from a bucket.

func (*Reader) Close

func (*Reader) Close() (_ error)

Close closes the reader. It must be called to release resources.

func (*Reader) Err

func (*Reader) Err() (_ error)

Err returns the error encountered during reading, if any.

func (*Reader) Read

func (*Reader) Read(p []byte) (_ int, _ error)

Read reads data from the object being downloaded.

type RemoveOption

type RemoveOption interface {
	// contains filtered or unexported methods
}

RemoveOption describes available options for the Remove operation.

type Remover

type Remover interface {
	// Remove removes an object from the bucket.
	Remove(ctx context.Context, object string, options ...RemoveOption) error
}

Remove is the interface for removing objects from a bucket. It can be used in conjunction with BucketRef to declare a reference that can remove objects from the bucket.

For example:

var MyBucket = objects.NewBucket(...)
var ref = objects.BucketRef[objects.Remover](MyBucket)

The ref object can then be used to remove objects and can be passed around freely within the service, without being subject to Encore's static analysis restrictions that apply to MyBucket.

type SignedDownloadURL added in v1.46.1

type SignedDownloadURL struct {
	// The signed URL
	URL string
}

type SignedDownloader added in v1.46.1

type SignedDownloader interface {
	// SignedDownloadURL returns a signed URL that can be used to download directly
	// from storage, without any other authentication.
	SignedDownloadURL(ctx context.Context, object string, options ...DownloadURLOption) (*SignedDownloadURL, error)
}

SignedDownloader is the interface for creating external URLs to download objects from a bucket. It can be used in conjunction with BucketRef to declare a reference that can generate download URLs for the bucket.

For example:

var MyBucket = objects.NewBucket(...)
var ref = objects.BucketRef[objects.SignedDownloader](MyBucket)

The ref object can then be used to generate download URLs and can be passed around freely within the service, without being subject to Encore's static analysis restrictions that apply to MyBucket.

type SignedUploadURL added in v1.46.1

type SignedUploadURL struct {
	// The signed URL
	URL string
}

type SignedUploader added in v1.46.1

type SignedUploader interface {
	// SignedUploadURL returns a signed URL that can be used to upload directly to
	// storage, without any other authentication.
	SignedUploadURL(ctx context.Context, object string, options ...UploadURLOption) (*SignedUploadURL, error)
}

SignedUploader is the interface for creating external URLs to upload objects to a bucket. It can be used in conjunction with BucketRef to declare a reference that can generate upload URLs to the bucket.

For example:

var MyBucket = objects.NewBucket(...)
var ref = objects.BucketRef[objects.SignedUploader](MyBucket)

The ref object can then be used to generate upload URLs and can be passed around freely within the service, without being subject to Encore's static analysis restrictions that apply to MyBucket.

type UploadAttrs

type UploadAttrs struct {
	// ContentType specifies the content type of the object.
	ContentType string
}

UploadAttrs specifies additional object attributes to set during upload.

type UploadOption

type UploadOption interface {
}

UploadOption describes available options for the Upload operation.

type UploadURLOption added in v1.46.1

type UploadURLOption interface {
	// contains filtered or unexported methods
}

UploadURLOption describes available options for the SignedUploadURL operation.

type Uploader

type Uploader interface {
	// Upload begins uploading an object to the bucket.
	Upload(ctx context.Context, object string, options ...UploadOption) *Writer
}

Uploader is the interface for uploading objects to a bucket. It can be used in conjunction with BucketRef to declare a reference that can upload objects to the bucket.

For example:

var MyBucket = objects.NewBucket(...)
var ref = objects.BucketRef[objects.Uploader](MyBucket)

The ref object can then be used to upload objects and can be passed around freely within the service, without being subject to Encore's static analysis restrictions that apply to MyBucket.

type Writer

type Writer struct {
	// contains filtered or unexported fields
}

Writer is the writer for an object being uploaded to a bucket.

func (*Writer) Abort

func (*Writer) Abort(err error)

Abort aborts the upload.

func (*Writer) Close

func (*Writer) Close() (_ error)

Close closes the upload, completing the upload if no errors occurred.

func (*Writer) Write

func (*Writer) Write(p []byte) (_ int, _ error)

Write writes data to the object being uploaded.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL