staticupload

package
v2.16.4 Latest Latest
Warning

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

Go to latest
Published: May 14, 2024 License: AGPL-3.0 Imports: 14 Imported by: 0

Documentation

Overview

Package staticupload provides a static file uploader/updater/remover for the CDN / static API.

This uploader uses AWS S3 as a backend and cloudfront as a CDN. It understands how to upload files and invalidate the CDN cache accordingly.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func New

func New(ctx context.Context, config Config, log *slog.Logger) (*Client, CloseFunc, error)

New creates a new Client. Call CloseFunc when done with operations.

Types

type CacheInvalidationStrategy

type CacheInvalidationStrategy int

CacheInvalidationStrategy is the strategy to use for invalidating the CDN cache.

const (
	// CacheInvalidateEager invalidates the CDN cache immediately for every key that is uploaded.
	CacheInvalidateEager CacheInvalidationStrategy = iota
	// CacheInvalidateBatchOnFlush invalidates the CDN cache in batches when the client is flushed / closed.
	// This is useful when uploading many files at once but may fail to invalidate the cache if close is not called.
	CacheInvalidateBatchOnFlush
)

type Client

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

Client is a static file uploader/updater/remover for the CDN / static API. It has the same interface as the S3 uploader.

func (*Client) DeleteObject

func (c *Client) DeleteObject(ctx context.Context, params *s3.DeleteObjectInput,
	optFns ...func(*s3.Options),
) (*s3.DeleteObjectOutput, error)

DeleteObject deletes the given key from S3 and invalidates the CDN cache. It returns the delete output or an error. The error will be of type InvalidationError if the CDN cache could not be invalidated.

func (*Client) DeleteObjects

func (c *Client) DeleteObjects(
	ctx context.Context, params *s3.DeleteObjectsInput,
	optFns ...func(*s3.Options),
) (*s3.DeleteObjectsOutput, error)

DeleteObjects deletes the given objects from S3 and invalidates the CDN cache. It returns the delete output or an error. The error will be of type InvalidationError if the CDN cache could not be invalidated.

func (*Client) Flush

func (c *Client) Flush(ctx context.Context) error

Flush flushes the client by invalidating the CDN cache for modified keys. It waits for all invalidations to finish. It returns nil on success or an error. The error will be of type InvalidationError if the CDN cache could not be invalidated.

func (*Client) GetObject

func (c *Client) GetObject(
	ctx context.Context, params *s3.GetObjectInput, optFns ...func(*s3.Options),
) (*s3.GetObjectOutput, error)

GetObject retrieves objects from Amazon S3.

func (*Client) ListObjectsV2

func (c *Client) ListObjectsV2(
	ctx context.Context, params *s3.ListObjectsV2Input, optFns ...func(*s3.Options),
) (*s3.ListObjectsV2Output, error)

ListObjectsV2 returns some or all (up to 1,000) of the objects in a bucket.

func (*Client) Upload

func (c *Client) Upload(
	ctx context.Context, input *s3.PutObjectInput, opts ...func(*s3manager.Uploader),
) (*s3manager.UploadOutput, error)

Upload uploads the given object to S3 and invalidates the CDN cache. It returns the upload output or an error. The error will be of type InvalidationError if the CDN cache could not be invalidated.

type CloseFunc

type CloseFunc func(ctx context.Context) error

CloseFunc is a function that closes the client.

type Config

type Config struct {
	// Region is the AWS region to use.
	Region string
	// Bucket is the name of the S3 bucket to use.
	Bucket string
	// DistributionID is the ID of the CloudFront distribution to use.
	DistributionID            string
	CacheInvalidationStrategy CacheInvalidationStrategy
	// CacheInvalidationWaitTimeout is the timeout to wait for the CDN cache to invalidate.
	// set to 0 to disable waiting for the CDN cache to invalidate.
	CacheInvalidationWaitTimeout time.Duration
}

Config is the configuration for the Client.

func (*Config) SetsDefault

func (c *Config) SetsDefault()

SetsDefault checks if all necessary values are set and sets default values otherwise.

type InvalidationError

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

InvalidationError is an error that occurs when invalidating the CDN cache.

func NewInvalidationError added in v2.9.0

func NewInvalidationError(err error) *InvalidationError

NewInvalidationError creates a new InvalidationError.

func (*InvalidationError) Error

func (e *InvalidationError) Error() string

Error returns the error message.

func (*InvalidationError) Unwrap

func (e *InvalidationError) Unwrap() error

Unwrap returns the inner error.

Jump to

Keyboard shortcuts

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