stds3fx

package
v0.0.258 Latest Latest
Warning

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

Go to latest
Published: Nov 9, 2026 License: MIT Imports: 15 Imported by: 0

Documentation

Overview

Package stds3fx provides an S3 client (minio-go) and the buckets it may use, resolved from aliases so code never spells out a deployment's bucket names. The client speaks the S3 API to STDS3_ENDPOINT, which is AWS S3 by default and any S3-compatible server otherwise. With STDS3_ACCESS_KEY and STDS3_SECRET_KEY it signs with those; without them it finds credentials the way AWS SDKs do, from the environment, the shared credentials file, or the role of the EC2 instance, ECS task or Kubernetes service account.

Pre-signed requests are signed for STDS3_PUBLIC_ENDPOINT when it is set, for clients that reach the server at another address than the service does, such as a browser on the host of a Docker Compose network the service runs in.

Tests get isolated buckets the way they get isolated databases: with STDS3_CREATE_AND_USE_RANDOM_BUCKETS every configured bucket name gets a random suffix and is created when the fx application starts, and with STDS3_AUTO_REMOVE_BUCKETS it is emptied and deleted when it stops. stds3testfx sets both. Leave them unset elsewhere, where infrastructure owns the buckets.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Provide

func Provide() fx.Option

Provide wires the module. It spells out what stdfx.ZapEnvCfgModule does, whose prefix for "stds3" would be STDS_3_.

Types

type Client

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

Client is the object storage client: the S3 client plus the resolved bucket names. It is safe for concurrent use.

func New

func New(params Params) (*Client, error)

New builds the client and, in random-bucket mode, ties bucket creation and removal to the application lifecycle.

func (*Client) Bucket

func (c *Client) Bucket(alias string) (string, bool)

Bucket returns the bucket name for an alias, such as "main". An alias that is not configured returns ok=false, which callers must treat as a programming error rather than fall back to another bucket.

func (*Client) Ping

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

Ping reports whether every configured bucket is reachable and exists, with a HeadBucket per bucket, all at once. It returns the first failure and stops the other checks. It only reads, so it is safe for the health check.

func (*Client) PresignGet added in v0.0.256

func (c *Client) PresignGet(
	ctx context.Context, bucket, key string, ttl time.Duration, params url.Values,
) (*PresignedRequest, error)

PresignGet signs a GET of the object at key in bucket, valid for ttl. Params may override headers of the response, such as response-content-disposition to have browsers save the object under a filename.

func (*Client) PresignPost added in v0.0.257

func (c *Client) PresignPost(
	ctx context.Context, bucket, key string, ttl time.Duration, conditions PostConditions,
) (*PresignedRequest, error)

PresignPost signs a POST of an object to key in bucket, valid for ttl, as an HTML form or any client can send it. Its policy carries conditions, so object storage refuses an upload that breaks them. Unlike a PUT, a POST lets the client send the object with a size anywhere in a range.

func (*Client) PresignPut added in v0.0.256

func (c *Client) PresignPut(
	ctx context.Context, bucket, key string, ttl time.Duration, headers http.Header,
) (*PresignedRequest, error)

PresignPut signs a PUT of an object to key in bucket, valid for ttl. The signature covers headers, such as Content-Type and Content-Length, so object storage refuses an upload that sends other values. The returned request carries headers without Content-Length, which clients set from the body themselves; browsers refuse to set it.

func (*Client) S3

func (c *Client) S3() *minio.Client

S3 returns the underlying S3 client for object operations; callers get bucket names from Client.Bucket.

type Config

type Config struct {
	// Buckets maps every bucket alias the code uses to the bucket name of the
	// environment, as alias:name pairs separated by commas.
	Buckets map[string]string `env:"BUCKETS,required"`
	// Endpoint is the URL of the S3 API, of AWS or an S3-compatible server. Its scheme
	// decides whether the client uses TLS.
	Endpoint string `env:"ENDPOINT" envDefault:"https://s3.amazonaws.com"`
	// PublicEndpoint is the URL clients send pre-signed requests to, when it is
	// not Endpoint. A signature covers the host, so a request signed for one
	// address fails at another. Leave it unset where both are the same.
	PublicEndpoint string `env:"PUBLIC_ENDPOINT"`
	// Region is the region the client signs for. Setting it spares the client
	// a bucket location lookup; most S3-compatible servers accept any region.
	Region string `env:"REGION" envDefault:"us-east-1"`
	// AccessKey and SecretKey are static credentials, such as those of an
	// S3-compatible server. Without them the client uses the AWS credential
	// sources.
	AccessKey string `env:"ACCESS_KEY"`
	SecretKey string `env:"SECRET_KEY"`
	// CreateAndUseRandomBuckets suffixes every bucket name with a random hex
	// string and creates the buckets on start, so parallel tests against one
	// server do not see each other's objects.
	CreateAndUseRandomBuckets bool `env:"CREATE_AND_USE_RANDOM_BUCKETS"`
	// AutoRemoveBuckets empties and deletes the random buckets on stop.
	AutoRemoveBuckets bool `env:"AUTO_REMOVE_BUCKETS"`
}

Config is the env-driven configuration of the module (prefix STDS3_).

type Params

type Params struct {
	fx.In
	fx.Lifecycle

	Config Config
	Logs   *zap.Logger
}

Params is the fx input for New.

type PostConditions added in v0.0.257

type PostConditions struct {
	// ContentType, when set, is the only content type accepted.
	ContentType string
	// MinSize and MaxSize bound the object's size in bytes when MaxSize is
	// positive; set both to one size to require exactly it.
	MinSize, MaxSize int64
}

PostConditions are what object storage requires of an object uploaded with a pre-signed POST, as conditions of its policy.

type PresignedRequest added in v0.0.256

type PresignedRequest struct {
	Method string
	URL    string
	// Headers must be sent as they are; the signature covers them.
	Headers http.Header
	// Fields are the form fields of a POST, which a client sends as they are
	// in a multipart/form-data body, before the object in a "file" field. They
	// are empty for other methods.
	Fields    map[string]string
	ExpiresAt time.Time
}

PresignedRequest is a request that a client sends to object storage directly, without credentials of its own: the signature in URL, or in Fields for a POST, grants it, until ExpiresAt. It is signed for STDS3_PUBLIC_ENDPOINT when that is set. Clients send it with stds3test.Send in tests.

Directories

Path Synopsis
Package stds3test sends, in tests, the requests that stds3fx pre-signs, the way clients send them.
Package stds3test sends, in tests, the requests that stds3fx pre-signs, the way clients send them.
Package stds3testfx provides the stds3fx client for tests, on an S3 server that is started for the tests, so they need no Docker Compose or other prior setup.
Package stds3testfx provides the stds3fx client for tests, on an S3 server that is started for the tests, so they need no Docker Compose or other prior setup.

Jump to

Keyboard shortcuts

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