s3

package
v0.17.0 Latest Latest
Warning

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

Go to latest
Published: Nov 22, 2026 License: MIT Imports: 16 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Client

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

Client reads and writes objects in one bucket.

func Default

func Default(bucket string) (*Client, error)

Default builds a client with a 5 second timeout and 3 total attempts.

func New

func New(bucket string, timeout time.Duration, maxRetryAttempts int) (*Client, error)

New builds a client for bucket. timeout is the deadline for each call. maxRetryAttempts is passed to the SDK as MaxAttempts, which counts the first attempt.

func (*Client) CheckBucket added in v0.15.0

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

CheckBucket confirms the bucket exists and the caller can reach it. A missing bucket wraps ErrNotFound.

func (*Client) Get

func (c *Client) Get(ctx context.Context, key string) (*GetResult, error)

Get returns the object at key. A missing key wraps ErrNotFound. The caller closes Body. Closing it ends the Get deadline.

func (*Client) Head added in v0.15.0

func (c *Client) Head(ctx context.Context, key string) (*HeadResult, error)

Head returns the metadata for key without downloading its body. A missing key wraps ErrNotFound.

S3 answers 404 for a missing bucket as well as a missing key, so a missing bucket also wraps ErrNotFound here. Call CheckBucket first when the two must be told apart.

func (*Client) List

func (c *Client) List(ctx context.Context, prefix, continuationToken string) (*ListResult, error)

List returns one page of objects under prefix. An empty prefix lists the bucket. Pass the previous NextContinuationToken to read the next page. An empty page is success.

func (*Client) Put

func (c *Client) Put(ctx context.Context, key string, body io.Reader, contentType string, opts ...PutOption) (*PutResult, error)

Put writes body to key, replacing any existing object, and returns the new object's ETag. A seekable body lets the SDK set the content length. Pass WithMetadata to store user metadata alongside the object.

Pass WithIfMatch or WithIfNoneMatch, not both, to make the write conditional. Passing both fails with ErrBadRequest, as S3 does not support the pair. When S3 refuses a conditional write, Put returns an error wrapping one of:

  • ErrPreconditionFailed (412): the condition does not hold. Another writer changed or created the object, so retrying the same write fails again.
  • ErrNotFound (404): WithIfMatch was used and no object, or no bucket, exists at the key.

Any 409, S3's conflict status, wraps ErrConflict, whether or not the Put is conditional. On a conditional Put it usually means another conditional write to the same key was in progress. For WithIfMatch, AWS advises reading the current ETag and trying again; for WithIfNoneMatch, trying again.

The client does not retry any of these.

A conditional Put can report ErrPreconditionFailed even though its own write landed. The client retries an attempt that failed with a connection error or a transient server error. If S3 stored the object on the first attempt but the response was lost, the retry finds the object already changed and gets 412. Put cannot tell this apart from a write by someone else. Callers that need to know can write a unique token with WithMetadata and read it back with Head.

type GetResult

type GetResult struct {
	Body         io.ReadCloser
	ContentType  string
	Size         int64
	LastModified time.Time
	Metadata     map[string]string
	ETag         string
}

GetResult is the body and metadata for one key. The caller closes Body. The Get deadline stays in effect until Body is closed. Metadata holds the user metadata set on the object, keyed without the x-amz-meta- prefix. ETag is the object's entity tag in the form S3 sends it, surrounding double quotes included, for example "9b2cf535f27731c974343645a3985328". Pass it unchanged to WithIfMatch.

type HeadResult added in v0.15.0

type HeadResult struct {
	ContentType  string
	Size         int64
	LastModified time.Time
	Metadata     map[string]string
	ETag         string
}

HeadResult is the metadata for one key, read without its body. Metadata holds the user metadata set on the object, keyed without the x-amz-meta- prefix. ETag has the same quoted form as GetResult.ETag.

type ListResult

type ListResult struct {
	Objects               []ObjectInfo
	NextContinuationToken string
}

ListResult is one page of objects. NextContinuationToken is empty on the last page.

type ObjectInfo

type ObjectInfo struct {
	Key          string
	Size         int64
	LastModified time.Time
}

ObjectInfo is one key returned by List.

type PutOption added in v0.15.0

type PutOption func(*sdks3.PutObjectInput)

PutOption sets an optional field on a Put.

func WithIfMatch added in v0.17.0

func WithIfMatch(etag string) PutOption

WithIfMatch makes the Put write only if the object at the key currently has this ETag, so a writer holding an old copy cannot overwrite a newer one. Pass an ETag exactly as returned by Get, Head or Put. A mismatch makes Put return an error wrapping ErrPreconditionFailed. If no object exists at the key, S3 answers 404 instead and Put returns an error wrapping ErrNotFound. S3 answers 404 for a missing bucket as well, so with WithIfMatch a missing bucket also wraps ErrNotFound. Call CheckBucket first when the two must be told apart. An empty etag makes Put fail with ErrBadRequest instead of writing unconditionally.

func WithIfNoneMatch added in v0.17.0

func WithIfNoneMatch() PutOption

WithIfNoneMatch makes the Put write only if no object exists at the key (If-None-Match: *). If one does, Put returns an error wrapping ErrPreconditionFailed.

func WithMetadata added in v0.15.0

func WithMetadata(metadata map[string]string) PutOption

WithMetadata stores metadata as the object's user metadata, served back as x-amz-meta-<key> headers and returned by Get and Head. S3 lowercases the keys. It replaces any metadata the object had before, as Put replaces the whole object.

type PutResult added in v0.17.0

type PutResult struct {
	ETag string
}

PutResult describes the object a Put wrote. ETag has the same quoted form as GetResult.ETag and can be passed unchanged to WithIfMatch on the next Put.

Jump to

Keyboard shortcuts

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