s3

package
v1.24.0 Latest Latest
Warning

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

Go to latest
Published: Jul 23, 2026 License: MIT Imports: 24 Imported by: 0

Documentation

Overview

Package s3 implements blob.ObjectStorage on top of Amazon S3 for storage and a CloudFront CDN for delivery. It spans two buckets: clients POST bytes to a presigned policy in the UPLOAD bucket, the server reads them back to validate them, and validated bytes are copied into the ORIGIN bucket (fronted by CloudFront) and removed from the upload bucket. Downloads are short-lived CloudFront signed URLs against the origin bucket. The server never proxies blob bytes — it only reads them back (via GetUploaded) to derive metadata during finalization.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func LoadCloudFrontPrivateKey

func LoadCloudFrontPrivateKey(ctx context.Context, client *secretsmanager.Client, secretID string) (*rsa.PrivateKey, error)

LoadCloudFrontPrivateKey fetches the PEM-encoded RSA private key used to sign CloudFront download URLs from AWS Secrets Manager and parses it into the *rsa.PrivateKey that Config.PrivateKey expects. secretID is the secret's name or ARN; the secret value must be an unencrypted RSA private key in either PKCS#1 ("RSA PRIVATE KEY", from `openssl genrsa`) or PKCS#8 ("PRIVATE KEY", from `openssl genpkey`) PEM form. The caller constructs and configures the *secretsmanager.Client, mirroring how NewStorage takes a ready *s3.Client.

Types

type Config

type Config struct {
	// UploadBucket is the bucket clients upload to via presigned POST policies. It
	// holds untrusted, not-yet-validated bytes and should carry a lifecycle rule
	// to expire abandoned uploads.
	UploadBucket string

	// OriginBucket is the bucket the CDN serves from. Only validated bytes,
	// promoted out of the upload bucket, ever land here.
	OriginBucket string

	// Region is the AWS region of the buckets. It scopes the SigV4 credential and
	// signing key for upload POST policies and seeds the default upload endpoint.
	Region string

	// UploadEndpointURL is the base URL (scheme + host) the upload POST targets,
	// already resolved for the upload bucket — e.g. a regional endpoint
	// "https://my-bucket.s3.us-east-1.amazonaws.com" or an S3 Transfer
	// Acceleration endpoint "https://my-bucket.s3-accelerate.amazonaws.com". When
	// empty, the regional virtual-hosted-style endpoint is derived from the bucket
	// and Region. It does not affect the POST policy signature, which
	// is always scoped to the bucket's region, so pointing it at an accelerated
	// (or dualstack/custom) endpoint is safe.
	UploadEndpointURL string

	// UploadTTL is how long a presigned upload policy stays valid.
	UploadTTL time.Duration

	// DownloadTTL is how long a minted CloudFront download URL stays valid.
	DownloadTTL time.Duration

	// CDNBaseURL is the CloudFront distribution base URL (scheme + host, e.g.
	// "https://d111111abcdef8.cloudfront.net") fronting the origin bucket.
	// Download URLs are this joined with the object key, then signed.
	CDNBaseURL string

	// CloudFrontKeyID is the CloudFront public key id paired with PrivateKey.
	CloudFrontKeyID string

	// PrivateKey is the RSA private key whose public half is registered with
	// CloudFront; it signs download URLs.
	PrivateKey *rsa.PrivateKey
}

Config configures the two-bucket S3 + CloudFront backend.

type Storage

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

Storage is the two-bucket S3 + CloudFront implementation of blob.ObjectStorage.

func NewStorage

func NewStorage(client *s3.Client, cfg Config) *Storage

NewStorage builds a Storage over an existing S3 client. The client is constructed and configured (region, credentials) by the caller, mirroring how the DynamoDB-backed stores take a ready *dynamodb.Client. The client's credentials and the configured Region are used to sign upload POST policies.

func (*Storage) CopyToOrigin

func (s *Storage) CopyToOrigin(ctx context.Context, key string) error

func (*Storage) DeleteUpload

func (s *Storage) DeleteUpload(ctx context.Context, key string) error

func (*Storage) GetUploaded

func (s *Storage) GetUploaded(ctx context.Context, key string) ([]byte, error)

func (*Storage) PresignUpload

func (s *Storage) PresignUpload(ctx context.Context, key, mimeType string, sizeBytes uint64) (*blobpb.UploadTarget, error)

func (*Storage) PutOrigin added in v1.22.0

func (s *Storage) PutOrigin(ctx context.Context, key, mimeType string, data []byte) error

func (*Storage) SignDownloadURL

func (s *Storage) SignDownloadURL(_ context.Context, key string) (*blobpb.DownloadUrl, error)

func (*Storage) UploadExists added in v1.22.0

func (s *Storage) UploadExists(ctx context.Context, key string) (bool, error)

Jump to

Keyboard shortcuts

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