## Search

Page index: https://gasmod.dev/llms.txt. Add index.md to any page URL for its Markdown.

This site has a search endpoint: https://context7.com/api/v2/ask?siteKey=ask_b845b566cb63bd033e9c576d&query=<URL-encoded question>. It returns documentation that answers the question, with a source link for each part. No API key is needed. If nothing matches, it says so.

# storage/s3

> Package s3 is an object store on Amazon S3, or a server that speaks its API, as a storage.Provider, with a storage.Presigner of URLs to it.

The Markdown of https://gasmod.dev/reference/storage/s3/ from the Gas documentation, whose index for LLMs is https://gasmod.dev/llms.txt

```go
import "github.com/gasmod/gas/storage/s3"
```

Package s3 is an object store on Amazon S3, or a server that speaks its
API, as a storage.Provider, with a storage.Presigner of URLs to it.

It sends its requests with the AWS SDK for Go v2, and writes objects with
the SDK's transfer manager, so that a body of any length, or of none
known, is written.

[Provider](#Provider) implements storage.Provider, gas.Service and
gas.ReadyReporter. [Presigner](#Presigner) implements storage.Presigner, gas.Service
and gas.ReadyReporter. Both keep the contract of the storage package.

## Usage

In an app, the container builds the store from [New](#New), and the
presigner from [NewPresigner](#NewPresigner), and calls their Init. By hand, with a
configuration given in code:

```go
cfg := s3.DefaultConfig()
cfg.Storage.Region = "eu-west-1"
cfg.Storage.Bucket = "photos"

store, err := s3.New(s3.WithConfig(cfg))(nil, logger)
if err != nil {
	return err
}
if err := store.Init(ctx); err != nil {
	return err
}
defer store.Close(ctx)

_, err = store.PutObject(ctx, &storage.PutObjectInput{
	Key:  "cats/tom.txt",
	Body: strings.NewReader("Tom"),
})
info, err := store.HeadObject(ctx, &storage.HeadObjectInput{Key: "cats/tom.txt"})
```

After this the bucket `photos`, which must exist, holds `cats/tom.txt`,
and:

- info.Size is 3.
- info.ContentType is `application/octet-stream`.
- info.ETag is the hex MD5 of `Tom`.

## Configuration

The store and the presigner read the same settings, in the `storage`
section, in [Settings](#Settings):

- `storage.region`: the AWS region. Default: none; the AWS SDK's
default chain decides, as under Credentials.
- `storage.endpoint`: the URL requests go to, an absolute `http` or
`https` URL with a host, and with no user information, query,
fragment, or path but `/`, such as an emulator's. Default: none, the
region's S3 endpoint. An `http` endpoint sends every request in
clear text, the access key ID, the session token and the objects
included; the secret key signs the requests and is not sent. Use it
only for a server on a network of its own.
- `storage.usepathstyle`: whether requests send the bucket in the
URL's path, `https://host/bucket/key`, rather than in its host,
`https://bucket.host/key`. Default: true when `endpoint` is set, as
most servers that speak S3's API want, and false otherwise, as AWS
wants.
- `storage.bucket`: the configured bucket, which an input with a nil
Bucket names, and which CheckReady checks. It keeps the bucket
rules of the storage package. Init does not create it. Default:
none.
- `storage.accesskeyid`, `storage.secretaccesskey`,
`storage.sessiontoken`: static credentials, as under Credentials.
The access key ID is not a secret, but it maps to the AWS account
ID, and the store never logs it. The secret key and the token are
each a [Secret](#Secret), which prints, marshals to JSON or YAML, and logs
with log/slog as `[redacted]`, and on which encoding/gob fails
rather than encode the value. Default: none.
- `storage.optimeout`: the bound of each request, retries included,
above 0, as under Timeouts. Default: [DefaultOpTimeout](#DefaultOpTimeout) (10s).
- `storage.maxretries`: how many times a failed request is retried, 0
to [MaxRetries](#MaxRetries) (10), with the AWS SDK's standard retryer and its
backoff, whatever `AWS_MAX_ATTEMPTS` and `AWS_RETRY_MODE` say. The
presigner sends no request, and ignores it. Default:
[DefaultMaxRetries](#DefaultMaxRetries), 2, so 3 attempts.
- `storage.rawerrors`: whether a failed request's error also shows
and wraps the AWS SDK's error, as under Errors of S3. It breaks the
guarantees under Security, so use it to debug. Default: false.

[Provider.Init](#Provider.Init) and [Presigner.Init](#Presigner.Init) resolve the configuration each
time they run:

- Without [WithConfig](#WithConfig), or [WithPresignerConfig](#WithPresignerConfig), Init binds
[DefaultConfig](#DefaultConfig) from the gas.ConfigProvider, which checks the
`validate` tags. A nil ConfigProvider is the error
storage.ErrNoConfigProvider.
- With WithConfig, it takes a copy of the given Config, never changing
the caller's struct, and checks the same tags.

Either way, these rules then run, in this order, and the first that
fails is the error:

1. `endpoint`, when set, is an absolute `http` or `https` URL with a
host, and with no user information, query, fragment, or path but
`/`.
2. `bucket`, when set, keeps the bucket rules.

A failure wraps storage.ErrInvalidConfig and names the setting, never
its value.

[Provider.Config](#Provider.Config) returns a deep copy of the configuration the last
successful Init bound, defaults included: the zero Config before the
first, and the last one bound after Close. A failed Init leaves it as
it was. It is safe to call at any time, while Init or Close runs
included.

Changing the copy changes nothing in the store: UsePathStyle, when set,
points to a bool of its own. The secrets keep their type, [Secret](#Secret), so
they print and marshal as `[redacted]`.

## Credentials

Init builds the client with the AWS SDK's default configuration, which
comes from:

- the environment, such as `AWS_REGION` and `AWS_ACCESS_KEY_ID`;
- the shared configuration and credentials files, and their profile;
- the container and instance roles.

Its HTTP client is the store's own, on the AWS SDK's default transport,
and follows no redirect, as the SDK's does not. AWS configuration that
fails to load, such as a profile that does not exist, fails Init with
storage.ErrInvalidConfig.

A `region` setting takes precedence over the chain's region. Without
one, a region the chain does not find fails Init with [ErrNoRegion](#ErrNoRegion).

Static credentials are used when both `accesskeyid` and
`secretaccesskey` are set, with `sessiontoken` when it is set too. With
either key empty, the chain's credentials apply, as gas/email/ses and
gas/queue/sqs do it, and the other static values are not used.

When some of them are set, but not both keys, Init logs
`static credentials incomplete, using the default chain` at warn level,
with `missing` naming the keys that are not set, never a value.

Init retrieves the credentials within its context and `optimeout`, so
that it fails, with [ErrNoCredentials](#ErrNoCredentials), when there are none, rather
than the first request. The client then refreshes them as they expire.
Init sends no request to S3.

## Custom clients

[WithClient](#WithClient) gives the store a client to use instead, such as an
\*s3.Client built elsewhere or a fake of [API](#API). Then:

- `region`, `endpoint`, `usepathstyle`, the credentials and
`maxretries` have no effect on the requests.
- Init neither checks a region nor retrieves credentials.

CreateBucket creates buckets in the region of the client Init built, or
of an \*s3.Client given, and else in the `region` setting.

[WithPresignClient](#WithPresignClient) does the same for the presigner, with a fake of
[PresignAPI](#PresignAPI) or an \*s3.PresignClient.

## Requests

Every method checks its input with the rules of the storage package,
before any request, so that a request that could never succeed is
neither sent nor retried.

The bucket methods:

- CreateBucket sends a LocationConstraint of its region, but in
us-east-1.
- Creating a bucket the credentials own already, which S3 answers with
BucketAlreadyOwnedByYou, is not an error.
- HeadBucket returns the region S3 reports.

HeadObject and GetObject send a Range as a normalized `bytes=` header,
and a Version and a PartNumber as they are. S3 keeps versions and parts,
so neither is storage.ErrNotSupported. A version that does not exist is
storage.ErrVersionNotFound with storage.ErrObjectNotFound.

HeadObject of an object in a missing bucket is storage.ErrObjectNotFound
alone: a HEAD reply has no body, so its 404 does not tell a missing
bucket from a missing key.

## Writes

PutObject writes with the AWS SDK's transfer manager:

- A body that holds less than 16 MiB is read whole into memory first,
and written with one PutObject.
- A larger one is written in parts of 8 MiB, sent up to five at a
time, with CreateMultipartUpload, UploadPart and
CompleteMultipartUpload. Its ETag then is not an MD5 of the
contents. The transfer manager then holds up to six parts in memory
at once, beside the first 16 MiB it read.

The transfer manager sends a CRC32 checksum of each request's body,
which S3 checks. Expires is sent as S3 stores it, an IMF-fixdate.

A WriteCondition is sent as If-None-Match or If-Match, the ETag quoted.
With parts, S3 evaluates it when the upload completes, after every part
was sent. The condition holds as S3 decides it:

- S3 may also refuse a write racing another conditional one with
ConditionalRequestConflict ([ErrConflict](#ErrConflict)).
- An If-Match of a key that holds no object is
storage.ErrObjectNotFound, as S3 answers it with NoSuchKey.

## Failed uploads

An upload in parts can fail because a request failed, the body failed,
the condition did not hold, ctx ended or a Close canceled it. It is then
aborted with AbortMultipartUpload, on a context of its own bounded by
`optimeout`, so that S3 frees its parts even when ctx has ended.

When the abort fails too, the parts stay stored, and billed, until an
abort or a lifecycle rule of the bucket removes them. The error then
wraps [ErrUploadNotAborted](#ErrUploadNotAborted) and the abort's reason after the
PutObject's, and PutObject logs `upload in parts not aborted` at warn
level. A bucket lifecycle rule that aborts incomplete uploads after a
day is the remedy on AWS.

The transfer manager logs a completion that failed with the standard
library's log package, outside the store's logger. It logs the error's
text, which is this package's, never the AWS SDK's, and the upload's ID
as a pointer address (transfermanager v0.5.5 formats the \*string with
%v), not the ID itself. A version of the transfer manager that printed
the ID would write it to standard error, so the version is pinned and
must be checked when it changes.

## Copies

CopyObject sends these requests:

1. A HeadObject of the source, which gives its length and its ETag.
2. S3's CopyObject, with the source in x-amz-copy-source and the ETag
in x-amz-copy-source-if-match, so that the size CopyObject returns
is that of the contents S3 copied. The source is its bucket, `/` and
its key, with every byte but the unreserved characters of RFC 3986
percent-encoded.

Without Replace it sends the COPY metadata directive and no header field
or metadata. With Replace, it sends the REPLACE directive with the
input's, as PutObject sends them.

Missing buckets are told apart as on the other backends:

- A HeadObject that finds no source, whose bodiless 404 does not tell a
missing bucket from a missing key, is followed by a HeadBucket of the
source's bucket. So a source in a missing bucket is
storage.ErrObjectNotFound and storage.ErrBucketNotFound.
- A NoSuchBucket reply to the CopyObject is the destination's bucket,
and is storage.ErrBucketNotFound alone.

A source that a write replaced since its HeadObject fails the CopyObject
with PreconditionFailed, which writes nothing. CopyObject then starts
again from the HeadObject. A copy refused with a conflict three times in
all is [ErrConflict](#ErrConflict), with the reason `conflict` and the code
PreconditionFailed, never storage.ErrPreconditionFailed, as the input
holds no condition.

S3 copies a source of up to 5 GiB in one request, in its own time, which
`optimeout` bounds, so copying a large object needs an `optimeout` long
enough. The copy's ETag is S3's: the MD5 of the contents for a source
written in one part without SSE-KMS.

## Listing

ListObjects sends ListObjectsV2 with encoding-type url, and decodes the
keys and the common prefixes, so that a key XML cannot carry is listed.

A continuation token of ListObjects or ListBuckets seals S3's own token,
which can tell a key on some servers. It seals it with a key Init draws
at random, bound to the listing that issued it, as the internal listing
package does for the memory store. A token from before a restart, or
from another listing, is storage.ErrInvalidToken, refused before any
request.

A token is good only for the Provider that issued it, until its next
Init: it does not survive a restart and does not move between replicas
of an app, which draw keys of their own.

S3 does not bound the length of its own token. One too long to seal,
longer than 1536 bytes, fails the listing with storage.ErrBackend and
the reason `invalid reply`, rather than hand out a token the store could
not open.

## Presigning

[Presigner.PresignUploadURL](#Presigner.PresignUploadURL) signs a PutObject, and
[Presigner.PresignDownloadURL](#Presigner.PresignDownloadURL) a GetObject, with SigV4 query
parameters; signing sends no request.

The URL holds the access key ID in X-Amz-Credential and, with temporary
credentials, the session token in X-Amz-Security-Token: anyone who holds
the URL holds the token for the URL's life.

The URL stops being valid when its TTL ends, or when the credentials
that signed it expire, whichever comes first, so a TTL longer than
temporary credentials last is cut short. PresignURLResponse.Expires is
the end of the TTL, from the X-Amz-Date of the URL, and so the latest
the URL is valid.

What a request signs, which the client must send as SignedHeader holds
it:

- An upload signs its ContentLength, its header fields, its metadata
and its condition.
- The AWS SDK signs no Content-Length of 0, so an upload of 0 bytes
signs a Content-MD5 of the empty body instead, which S3 checks the
body against. Either way a client cannot store more than was signed
for.
- The Content-Type is signed at every length, 0 included, as
`application/octet-stream` when ContentType is unset.
- Any other header field left unset, CacheControl,
ContentDisposition, ContentEncoding, ContentLanguage or Expires, is
not signed, so the client may send one of its own, which S3 stores
with the object.
- A download signs its Range, PartNumber, Version and response header
fields.

## Timeouts

Every request runs within its context and `optimeout`, whichever ends
first, the AWS SDK's retries and their backoff included. So a server
that stops answering cannot hold a call longer than `optimeout`, even
with a context that never ends.

Per operation:

- PutObject bounds each of its requests, each part, by `optimeout`, so
the whole write may take longer.
- CopyObject does the same, for each of its HeadObject, HeadBucket and
CopyObject.
- GetObject bounds the time until the response headers arrive. The
Body is then governed by GetObject's context alone, so that an
object of any size can be read, and a Close does not cut it short.
- Init retrieves the credentials within its context and `optimeout`.
- Signing a URL may refresh credentials, within its context and
`optimeout`.

The SDK retries a request after a network error, a throttle, such as
SlowDown, and a server error, up to `maxretries` times.

A request's own context that ends returns the context's error after the
operation, wrapped. Which of the two a timeout is depends on which
deadline was in force: a timeout of a request whose context's deadline
is no later than `optimeout`'s is the context's error.

## Errors of S3

An error reply of S3 that stands for a sentinel of the storage package
returns an error wrapping it, with its text, and nothing else:

- NoSuchBucket: storage.ErrBucketNotFound, and storage.ErrObjectNotFound
too from HeadObject and GetObject.
- NoSuchKey: storage.ErrObjectNotFound.
- NoSuchVersion: storage.ErrVersionNotFound.
- BucketAlreadyExists: storage.ErrBucketExists.
- BucketNotEmpty: storage.ErrBucketNotEmpty.
- PreconditionFailed: storage.ErrPreconditionFailed, but from
CopyObject, as under Copies.
- InvalidRange: storage.ErrRangeNotSatisfiable.
- The bodiless 404 of a HEAD: storage.ErrObjectNotFound from
HeadObject, and storage.ErrBucketNotFound from HeadBucket and
CheckReady.

Any other failure returns an error with a reason, told by the AWS error
code, and the code, in parentheses, when it is one S3 documents. The
error never keeps the reply's message, which can quote a key, a bucket
or an account, nor a code of any other text, nor the AWS SDK's error,
which holds the URL, and so the bucket and the key.

With `storage.rawerrors` set, the error's text ends with `: ` and the
AWS SDK's error, its operation, HTTP status, request ID, code and
message, and the error wraps it, so errors.As reaches the SDK's types,
such as smithy.APIError. It does the same for the failure of Init to
load the AWS configuration or to retrieve credentials. The reason, the
sentinels and the context's errors are as without it. Init logs a
warning when it is set.

The reasons, in the order they are told:

- `access denied`: AccessDenied, a 403 without a body,
InvalidAccessKeyId, SignatureDoesNotMatch, ExpiredToken and the
other codes of refused credentials or permissions, wrapping
storage.ErrAccessDenied.
- `throttled`: SlowDown and the other throttles, past the retries,
wrapping [ErrThrottled](#ErrThrottled) and storage.ErrBackend.
- `wrong region`: PermanentRedirect, a 301 without a body,
AuthorizationHeaderMalformed and IllegalLocationConstraintException,
wrapping [ErrWrongRegion](#ErrWrongRegion) and storage.ErrBackend: the bucket is in
another region than the client's.
- `conflict`: ConditionalRequestConflict and OperationAborted, and
PreconditionFailed from CopyObject, wrapping [ErrConflict](#ErrConflict) and
storage.ErrBackend.
- `failed`: any other reply, wrapping storage.ErrBackend.

A reply that a deadline cut the retries of short wraps
context.DeadlineExceeded too.

A request that got no reply returns an error wrapping [ErrConnect](#ErrConnect) and
storage.ErrBackend, with a reason:

- `timed out`: a deadline passed, in a context or on the network.
The error then wraps context.DeadlineExceeded too.
- `connection refused`: the endpoint refused the TCP connection.
- `unreachable`: any other network error, such as a failed name
lookup.

And with storage.ErrBackend alone:

- `invalid reply`: a reply S3 does not send. That is a body larger
than 16 MiB, but for the contents GetObject returns, which the client
Init builds stops reading at; a page of ListObjects whose IsTruncated
and continuation token disagree, or whose keys are not valid URL
encoding; a page of ListObjects or ListBuckets whose continuation
token is longer than 1536 bytes, too long to seal; a presigned URL
without its signing time; or a client of WithClient that returns
neither an output nor an error.
- `unexpected EOF`: a reply whose body ended before its length, also
wrapping io.ErrUnexpectedEOF.
- `failed`: anything else.

A read of a GetObject's Body that fails returns such an error, after
`read body: `, or the context's error, after the same, when GetObject's
context ended. io.EOF is returned as it is.

## Lifecycle

A successful Init publishes the client, and only then do the methods use
it. It logs `service initialized` at info level with:

- `region`;
- whether an `endpoint` was set;
- `path_style`;
- whether a `bucket` is configured and a `client` given;
- `credentials`, where the client Init built takes them from, `static`
or `default chain`, left out with a client given;
- `op_timeout`;
- for the store, `max_retries`.

It never logs the bucket's name. Init on an initialized store returns
storage.ErrAlreadyInitialized and changes nothing. A failed Init leaves
the store as it was.

[Provider.Close](#Provider.Close) runs these steps:

1. It makes the store closed, so that every later call returns
storage.ErrClosed.
2. It waits for every call in flight to return. While its context
lasts, it does not cut a call short.
3. It closes the idle connections of the client Init built.
4. It logs `service closed` at info level.

When its context ends first, Close cancels the calls in flight, which
return storage.ErrClosed, though S3 may have done what they asked. It
logs `service closed, calls in flight canceled` at warn level, and
returns an error wrapping the context's error; the store stays closed.

Out of the usual order:

- A Body GetObject returned is not a call in flight, and stays
readable.
- Close on a store that is not initialized, or on a nil Provider,
returns nil and logs nothing.
- Init after Close builds a new client, with a configuration resolved
afresh. The buckets and objects are S3's, and outlive it.

The presigner's Init and Close do the same.

[New](#New)'s constructor fails with:

- storage.ErrNilLogger for a nil logger;
- storage.ErrNilOption for a nil option, naming its position;
- storage.ErrNilConfig for WithConfig(nil).

A nil ConfigProvider is accepted, and Init then requires WithConfig. A
nil client given to WithClient fails Init with [ErrNilClient](#ErrNilClient).
[NewPresigner](#NewPresigner) does the same.

## Readiness

[Provider.CheckReady](#Provider.CheckReady) fails when the store is not initialized or its
context has ended. With a configured bucket, it then sends a HeadBucket
of it, within its context and `optimeout`, and returns its error, such
as storage.ErrBucketNotFound or storage.ErrAccessDenied. Without one it
does no I/O.

S3 authorizes a HeadBucket of a general purpose bucket as the
s3:ListBucket action on the bucket. Credentials that may read and write
the bucket's objects but not list it make CheckReady fail with
storage.ErrAccessDenied; grant s3:ListBucket on the configured bucket.

[Presigner.CheckReady](#Presigner.CheckReady) does no I/O.

## Client

[Provider.Client](#Provider.Client) returns the \*s3.Client, for the operations the store
has no method for, such as PutBucketLifecycleConfiguration. It returns
nil when the store is not initialized or was given another [API](#API).
[Presigner.PresignClient](#Presigner.PresignClient) returns the \*s3.PresignClient likewise.

Requests sent on them are bounded only by their own context, and return
the AWS SDK's errors as they are, which can hold the URL, a bucket, a
key and the server's message.

## Security

No error the store makes and no log it writes holds a key, a bucket
name, metadata, a header value, a body, the endpoint, a URL, an upload
ID, a continuation token or a credential, the access key ID included,
unless `storage.rawerrors` is set: then an error, and the warning of an
upload in parts not aborted, can hold what the AWS SDK's error holds,
such as the endpoint, a bucket, a key, an upload ID and the server's
message.

The logs carry the region, whether an endpoint, a client and a bucket
were given, the addressing, where the credentials come from, the names
of the static keys missing, the operation timeout and the retries.

The client Init builds gives the AWS SDK a logger that discards
everything, so the SDK writes nothing to standard error, where it logs
by default, such as a Date header of a reply it cannot parse. That
client reads no reply past 16 MiB but an object's contents, so an
endpoint cannot make it read without bound.

fmt, encoding/json, YAML encoders that use encoding.TextMarshaler, and
log/slog write the secret key and the session token of a [Settings](#Settings) or
a [Config](#Config) as `[redacted]`, and encoding/gob fails on them rather than
encode them.

Static credentials in configuration are a secret of the app's; prefer a
role, which the default chain finds. A presigned URL is a credential
until it expires, as under Presigning.

## Concurrency

A Provider and a Presigner are safe for concurrent use. Init and Close
run one at a time. Init swaps in what it built at once, so a call,
which takes no lock but to count itself in flight, uses one Init's
client throughout.

## Errors

The sentinel errors of this package, to be tested with [errors.Is](https://pkg.go.dev/errors#Is):

- [ErrNoRegion](#ErrNoRegion): Init found no region.
- [ErrNoCredentials](#ErrNoCredentials): Init could not retrieve credentials.
- [ErrNilClient](#ErrNilClient): WithClient or WithPresignClient was given a nil
client.
- [ErrConnect](#ErrConnect): a request got no reply, as under Errors of S3.
- [ErrThrottled](#ErrThrottled), [ErrWrongRegion](#ErrWrongRegion), [ErrConflict](#ErrConflict): replies of S3, as
under Errors of S3.
- [ErrUploadNotAborted](#ErrUploadNotAborted): a failed PutObject could not abort its upload
in parts.

The other errors wrap a sentinel of the storage package, as the storage
package describes. Every error starts with [ServiceName](#ServiceName), or
[PresignerName](#PresignerName), and the operation. A configuration that fails its tags
gets the validator's error after
`gas/storage/s3: init: invalid config: validate config: `, such as:

```go
Key: 'Config.Storage.OpTimeout' Error:Field validation for 'OpTimeout' failed on the 'gt' tag
```

The other errors, as returned, with `get object` and `put object`
standing for each operation that can fail the same way, `copy object`
included:

```go
gas/storage/s3: logger is nil
gas/storage/s3: option is nil: at position 0
gas/storage/s3: config is nil
gas/storage/s3: init: no config provider
gas/storage/s3: init: invalid config: Storage.Endpoint must be an http or https URL with a host and nothing more but /
gas/storage/s3: init: invalid config: Storage.Bucket must be a valid bucket name
gas/storage/s3: init: invalid config: AWS configuration failed to load
gas/storage/s3: init: no region
gas/storage/s3: init: no credentials: failed
gas/storage/s3: init: no credentials: timed out
gas/storage/s3: init: no credentials: timed out: context deadline exceeded
gas/storage/s3: init: client is nil
gas/storage/s3: init: service already initialized
gas/storage/s3: init: context canceled
gas/storage/s3: ready: service not initialized
gas/storage/s3: ready: bucket not found
gas/storage/s3: create bucket: bucket exists
gas/storage/s3: delete bucket: bucket not empty
gas/storage/s3: list buckets: invalid continuation token: not one this backend issued for this listing
gas/storage/s3: get object: input is nil
gas/storage/s3: get object: service not initialized
gas/storage/s3: get object: service closed
gas/storage/s3: get object: context canceled
gas/storage/s3: get object: context deadline exceeded
gas/storage/s3: get object: invalid key: empty
gas/storage/s3: get object: object not found
gas/storage/s3: get object: object not found: bucket not found
gas/storage/s3: get object: object not found: version not found
gas/storage/s3: get object: range not satisfiable
gas/storage/s3: get object: access denied (InvalidAccessKeyId)
gas/storage/s3: get object: throttled (SlowDown)
gas/storage/s3: get object: wrong region (PermanentRedirect)
gas/storage/s3: get object: failed (InternalError)
gas/storage/s3: get object: failed
gas/storage/s3: get object: timed out
gas/storage/s3: get object: connection refused
gas/storage/s3: get object: unreachable
gas/storage/s3: get object: read body: unexpected EOF
gas/storage/s3: get object: read body: context canceled
gas/storage/s3: list objects: invalid reply
gas/storage/s3: put object: content length mismatch: body has 3 bytes, ContentLength is 5
gas/storage/s3: put object: read body: disk gone
gas/storage/s3: put object: precondition failed
gas/storage/s3: put object: conflict (ConditionalRequestConflict)
gas/storage/s3: copy object: source: invalid key: empty
gas/storage/s3: copy object: invalid argument: copies an object onto itself without Replace
gas/storage/s3: copy object: invalid argument: source is longer than 5368709120 bytes
gas/storage/s3: copy object: conflict (PreconditionFailed)
gas/storage/s3: put object: context canceled; upload not aborted: failed (InternalError)
gas/storage/s3: close: context deadline exceeded
gas/storage/s3/presigner: presign upload: invalid argument: ContentLength is required
gas/storage/s3/presigner: presign upload: invalid reply
gas/storage/s3/presigner: presign download: service closed
```


## API reference

### Constants

<a id="DefaultOpTimeout"></a><a id="DefaultMaxRetries"></a><a id="MaxRetries"></a>

```go
const (
	// DefaultOpTimeout is the bound of each request of DefaultConfig, its
	// retries included: of each part of a PutObject, and of a GetObject
	// until its response headers arrive.
	DefaultOpTimeout = 10 * time.Second
	// DefaultMaxRetries is how many times DefaultConfig retries a failed
	// request: 2, so 3 attempts, as the AWS SDK's standard retryer makes by
	// default.
	DefaultMaxRetries = 2
	// MaxRetries is the most retries the MaxRetries setting allows.
	MaxRetries = 10
)
```

<a id="PresignerName"></a>

```go
const PresignerName = "gas/storage/s3/presigner"
```

PresignerName is the name of the S3 presigner, returned by
Presigner.Name and prefixed to every error the presigner returns.

<a id="ServiceName"></a>

```go
const ServiceName = "gas/storage/s3"
```

ServiceName is the name of the S3 store, returned by Provider.Name and
prefixed to every error the store returns.

### Variables

<a id="ErrNoRegion"></a><a id="ErrNoCredentials"></a><a id="ErrNilClient"></a><a id="ErrConnect"></a><a id="ErrThrottled"></a><a id="ErrWrongRegion"></a><a id="ErrConflict"></a><a id="ErrUploadNotAborted"></a>

```go
var (
	// ErrNoRegion indicates that Init found no AWS region, in the Region
	// setting or the AWS SDK's default chain.
	ErrNoRegion = errors.New("no region")
	// ErrNoCredentials indicates that Init could not retrieve AWS
	// credentials.
	ErrNoCredentials = errors.New("no credentials")
	// ErrNilClient indicates that WithClient or WithPresignClient was
	// given a nil client.
	ErrNilClient = errors.New("client is nil")
	// ErrConnect indicates that a request got no reply from S3: it timed
	// out, its connection was refused, or the endpoint was unreachable, as
	// described in the package documentation. It comes with
	// storage.ErrBackend.
	ErrConnect = errors.New("no reply")
	// ErrThrottled indicates that S3 throttled a request, past the
	// retries. It comes with storage.ErrBackend.
	ErrThrottled = errors.New("throttled")
	// ErrWrongRegion indicates that S3 refused a request sent to another
	// region than the bucket's. It comes with storage.ErrBackend.
	ErrWrongRegion = errors.New("wrong region")
	// ErrConflict indicates that S3 refused a request that conflicted with
	// another in flight, such as two conditional writes of one key. It
	// comes with storage.ErrBackend.
	ErrConflict = errors.New("conflict")
	// ErrUploadNotAborted indicates that a PutObject that failed after it
	// began an upload in parts could not abort the upload, whose parts S3
	// then keeps, as described in the package documentation.
	ErrUploadNotAborted = errors.New("upload not aborted")
)
```

<a id="New"></a>

#### func New

```go
func New(opts ...Option) func(gas.ConfigProvider, gas.Logger) (*Provider, error)
```

New returns the constructor of a Provider with opts applied, in the
shape the gas container expects. The constructor returns an error
wrapping storage.ErrNilLogger for a nil logger, storage.ErrNilOption for
a nil option, and storage.ErrNilConfig for WithConfig(nil); a nil
ConfigProvider is accepted, and Init then requires WithConfig.

<a id="NewPresigner"></a>

#### func NewPresigner

```go
func NewPresigner(
	opts ...PresignerOption,
) func(gas.ConfigProvider, gas.Logger) (*Presigner, error)
```

NewPresigner returns the constructor of a Presigner with opts applied,
in the shape the gas container expects. The constructor returns an
error wrapping storage.ErrNilLogger for a nil logger,
storage.ErrNilOption for a nil option, and storage.ErrNilConfig for
WithPresignerConfig(nil); a nil ConfigProvider is accepted, and Init
then requires WithPresignerConfig.

<a id="API"></a><a id="API.CreateBucket"></a><a id="API.HeadBucket"></a><a id="API.DeleteBucket"></a><a id="API.ListBuckets"></a><a id="API.HeadObject"></a><a id="API.GetObject"></a><a id="API.PutObject"></a><a id="API.DeleteObject"></a><a id="API.ListObjectsV2"></a><a id="API.CreateMultipartUpload"></a><a id="API.UploadPart"></a><a id="API.CompleteMultipartUpload"></a><a id="API.CopyObject"></a><a id="API.AbortMultipartUpload"></a>

### type API

```go
type API interface {
	// CreateBucket creates a bucket.
	CreateBucket(
		ctx context.Context,
		params *s3.CreateBucketInput,
		optFns ...func(*s3.Options),
	) (*s3.CreateBucketOutput, error)
	// HeadBucket describes a bucket.
	HeadBucket(
		ctx context.Context,
		params *s3.HeadBucketInput,
		optFns ...func(*s3.Options),
	) (*s3.HeadBucketOutput, error)
	// DeleteBucket deletes a bucket.
	DeleteBucket(
		ctx context.Context,
		params *s3.DeleteBucketInput,
		optFns ...func(*s3.Options),
	) (*s3.DeleteBucketOutput, error)
	// ListBuckets lists buckets.
	ListBuckets(
		ctx context.Context,
		params *s3.ListBucketsInput,
		optFns ...func(*s3.Options),
	) (*s3.ListBucketsOutput, error)
	// HeadObject describes an object.
	HeadObject(
		ctx context.Context,
		params *s3.HeadObjectInput,
		optFns ...func(*s3.Options),
	) (*s3.HeadObjectOutput, error)
	// GetObject reads an object.
	GetObject(
		ctx context.Context,
		params *s3.GetObjectInput,
		optFns ...func(*s3.Options),
	) (*s3.GetObjectOutput, error)
	// PutObject writes an object in one request.
	PutObject(
		ctx context.Context,
		params *s3.PutObjectInput,
		optFns ...func(*s3.Options),
	) (*s3.PutObjectOutput, error)
	// DeleteObject deletes an object.
	DeleteObject(
		ctx context.Context,
		params *s3.DeleteObjectInput,
		optFns ...func(*s3.Options),
	) (*s3.DeleteObjectOutput, error)
	// ListObjectsV2 lists the objects of a bucket.
	ListObjectsV2(
		ctx context.Context,
		params *s3.ListObjectsV2Input,
		optFns ...func(*s3.Options),
	) (*s3.ListObjectsV2Output, error)
	// CreateMultipartUpload begins an upload in parts.
	CreateMultipartUpload(
		ctx context.Context,
		params *s3.CreateMultipartUploadInput,
		optFns ...func(*s3.Options),
	) (*s3.CreateMultipartUploadOutput, error)
	// UploadPart uploads a part.
	UploadPart(
		ctx context.Context,
		params *s3.UploadPartInput,
		optFns ...func(*s3.Options),
	) (*s3.UploadPartOutput, error)
	// CompleteMultipartUpload completes an upload in parts.
	CompleteMultipartUpload(
		ctx context.Context,
		params *s3.CompleteMultipartUploadInput,
		optFns ...func(*s3.Options),
	) (*s3.CompleteMultipartUploadOutput, error)
	// CopyObject copies an object.
	CopyObject(
		ctx context.Context,
		params *s3.CopyObjectInput,
		optFns ...func(*s3.Options),
	) (*s3.CopyObjectOutput, error)
	// AbortMultipartUpload aborts an upload in parts.
	AbortMultipartUpload(
		ctx context.Context,
		params *s3.AbortMultipartUploadInput,
		optFns ...func(*s3.Options),
	) (*s3.AbortMultipartUploadOutput, error)
}
```

API is the part of the S3 client the store uses. \*s3.Client implements
it.

<a id="Config"></a>

### type Config

```go
type Config struct {
	// Storage holds the settings.
	Storage Settings
}
```

Config holds the S3 store's configuration, read from the `storage`
section.

<a id="DefaultConfig"></a>

#### func DefaultConfig

```go
func DefaultConfig() *Config
```

DefaultConfig returns the configuration a store starts from before
binding: no region, no endpoint, path-style addressing as the endpoint
decides, no configured bucket, no static credentials, DefaultOpTimeout
and DefaultMaxRetries.

<a id="Option"></a>

### type Option

```go
type Option func(*Provider)
```

Option configures a Provider. Options only record what they are given;
the constructor and Init check it.

<a id="WithClient"></a>

#### func WithClient

```go
func WithClient(client API) Option
```

WithClient makes the store send its requests with client, such as an
\*s3.Client built elsewhere or a fake, instead of building one, as
described in the package documentation. The Region, Endpoint,
UsePathStyle, credential and MaxRetries settings then have no effect
on the requests. A nil client, or a nil \*s3.Client, makes Init fail
with ErrNilClient.

Default: Init builds an \*s3.Client from the settings.

<a id="WithConfig"></a>

#### func WithConfig

```go
func WithConfig(cfg *Config) Option
```

WithConfig makes Init use a copy of cfg instead of binding the
configuration from the ConfigProvider. Init copies \*cfg each time it
runs and never changes it; the copy is checked against its validate
tags as a bound configuration is. A nil cfg makes the constructor fail
with storage.ErrNilConfig.

Default: bind the `storage` section from the ConfigProvider over
DefaultConfig.

<a id="PresignAPI"></a><a id="PresignAPI.PresignPutObject"></a><a id="PresignAPI.PresignGetObject"></a>

### type PresignAPI

```go
type PresignAPI interface {
	// PresignPutObject signs a PutObject.
	PresignPutObject(
		ctx context.Context,
		params *s3.PutObjectInput,
		optFns ...func(*s3.PresignOptions),
	) (*v4.PresignedHTTPRequest, error)
	// PresignGetObject signs a GetObject.
	PresignGetObject(
		ctx context.Context,
		params *s3.GetObjectInput,
		optFns ...func(*s3.PresignOptions),
	) (*v4.PresignedHTTPRequest, error)
}
```

PresignAPI is the part of the S3 presign client the presigner uses.
\*s3.PresignClient implements it.

<a id="Presigner"></a>

### type Presigner

```go
type Presigner struct {
	// contains filtered or unexported fields
}
```

Presigner issues presigned URLs of objects on Amazon S3, or a server
that speaks its API, as described in the package documentation. It is
safe for concurrent use.

<a id="Presigner.CheckReady"></a>

#### func (*Presigner) CheckReady

```go
func (p *Presigner) CheckReady(ctx context.Context) error
```

CheckReady reports readiness without I/O: an error wrapping
storage.ErrNotInitialized or storage.ErrClosed when the presigner is
not initialized, the context's error when ctx has ended, and else nil.

<a id="Presigner.Close"></a>

#### func (*Presigner) Close

```go
func (p *Presigner) Close(ctx context.Context) error
```

Close makes every later call but Init return an error wrapping
storage.ErrClosed, and waits for every call in flight to return, as a
Provider's Close does. Close on a presigner that is not initialized
returns nil and logs nothing.

<a id="Presigner.Init"></a>

#### func (*Presigner) Init

```go
func (p *Presigner) Init(ctx context.Context) error
```

Init resolves and checks the configuration, builds the presign client
and retrieves the credentials, as a Provider's Init does. On an
initialized presigner it returns storage.ErrAlreadyInitialized and
changes nothing. A failed Init leaves the presigner as it was.

<a id="Presigner.Name"></a>

#### func (*Presigner) Name

```go
func (*Presigner) Name() string
```

Name returns PresignerName.

<a id="Presigner.PresignClient"></a>

#### func (*Presigner) PresignClient

```go
func (p *Presigner) PresignClient() *s3.PresignClient
```

PresignClient returns the \*s3.PresignClient the presigner signs with,
or nil when the presigner is not initialized or was given another
PresignAPI with WithPresignClient.

<a id="Presigner.PresignDownloadURL"></a>

#### func (*Presigner) PresignDownloadURL

```go
func (p *Presigner) PresignDownloadURL(
	ctx context.Context,
	in *storage.PresignDownloadURLInput,
) (*storage.PresignURLResponse, error)
```

PresignDownloadURL returns a presigned GET of the object in, as
described in the package documentation, with the Range, PartNumber,
Version and response header fields it sets.

<a id="Presigner.PresignUploadURL"></a>

#### func (*Presigner) PresignUploadURL

```go
func (p *Presigner) PresignUploadURL(
	ctx context.Context,
	in *storage.PresignUploadURLInput,
) (*storage.PresignURLResponse, error)
```

PresignUploadURL returns a presigned PUT of the object in, as described
in the package documentation: the ContentLength, the header fields, the
metadata and the condition are signed, so the client must send them as
SignedHeader holds them.

<a id="PresignerOption"></a>

### type PresignerOption

```go
type PresignerOption func(*Presigner)
```

PresignerOption configures a Presigner. Options only record what they
are given; the constructor and Init check it.

<a id="WithPresignClient"></a>

#### func WithPresignClient

```go
func WithPresignClient(client PresignAPI) PresignerOption
```

WithPresignClient makes the Presigner sign with client, such as an
\*s3.PresignClient built elsewhere or a fake, instead of building one,
as described in the package documentation. The Region, Endpoint,
UsePathStyle and credential settings then have no effect on the URLs.
A nil client, or a nil \*s3.PresignClient, makes Init fail with
ErrNilClient.

Default: Init builds an \*s3.PresignClient from the settings.

<a id="WithPresignerConfig"></a>

#### func WithPresignerConfig

```go
func WithPresignerConfig(cfg *Config) PresignerOption
```

WithPresignerConfig makes the Presigner's Init use a copy of cfg, as
WithConfig does for a Provider. A nil cfg makes the constructor fail
with storage.ErrNilConfig.

Default: bind the `storage` section from the ConfigProvider over
DefaultConfig.

<a id="Provider"></a>

### type Provider

```go
type Provider struct {
	// contains filtered or unexported fields
}
```

Provider is an object store on Amazon S3, or a server that speaks its
API, as described in the package documentation. It is safe for
concurrent use.

<a id="Provider.CheckReady"></a>

#### func (*Provider) CheckReady

```go
func (p *Provider) CheckReady(ctx context.Context) error
```

CheckReady reports readiness: an error wrapping
storage.ErrNotInitialized or storage.ErrClosed when the store is not
initialized, the context's error when ctx has ended, and else, with a
configured bucket, the error of a HeadBucket of it, or nil. Without a
configured bucket it does no I/O.

<a id="Provider.Client"></a>

#### func (*Provider) Client

```go
func (p *Provider) Client() *s3.Client
```

Client returns the \*s3.Client the store sends its requests with, or nil
when the store is not initialized or was given another API with
WithClient, as described in the package documentation.

<a id="Provider.Close"></a>

#### func (*Provider) Close

```go
func (p *Provider) Close(ctx context.Context) error
```

Close makes every later call but Init return an error wrapping
storage.ErrClosed, and waits for every call in flight to return, as
described in the package documentation. When ctx ends first, it
cancels the calls in flight, which return storage.ErrClosed, and
returns an error wrapping ctx's error; the store stays closed. Close
then closes the idle connections of a client Init built. Close on a
store that is not initialized returns nil and logs nothing.

<a id="Provider.Config"></a>

#### func (*Provider) Config

```go
func (p *Provider) Config() Config
```

Config returns a deep copy of the configuration the last successful
Init bound, defaults included, as described in the package
documentation: the zero Config before the first successful Init, and
on a nil Provider, and the last one bound after Close. UsePathStyle,
when set, points to a bool of its own, so changing the copy changes
nothing in the store. The secrets keep their type, Secret. It is safe
to call at any time, while Init or Close runs included.

<a id="Provider.CopyObject"></a>

#### func (*Provider) CopyObject

```go
func (p *Provider) CopyObject(
	ctx context.Context,
	in *storage.CopyObjectInput,
) (*storage.CopyObjectOutput, error)
```

CopyObject copies the object in names, as described in the package
documentation: a HeadObject of the source gives its size and its ETag,
then a CopyObject with the source's ETag as its
x-amz-copy-source-if-match writes the copy, so that the size returned
is that of the contents copied. A HeadObject that finds no source is
followed by a HeadBucket of its bucket, which tells a missing bucket
from a missing key. A copy S3 refuses with a conflict, which writes
nothing, is sent again, from the HeadObject, up to copyAttempts times in
all. Each request is bounded by OpTimeout.

<a id="Provider.CreateBucket"></a>

#### func (*Provider) CreateBucket

```go
func (p *Provider) CreateBucket(ctx context.Context, in *storage.CreateBucketInput) error
```

CreateBucket creates the bucket in.Bucket, in the store's region, as
described in the package documentation. Creating one the caller owns
already is not an error; a name another account owns is an error
wrapping storage.ErrBucketExists.

<a id="Provider.DeleteBucket"></a>

#### func (*Provider) DeleteBucket

```go
func (p *Provider) DeleteBucket(ctx context.Context, in *storage.DeleteBucketInput) error
```

DeleteBucket deletes the bucket in.Bucket when it holds no object, as
described in the package documentation.

<a id="Provider.DeleteObject"></a>

#### func (*Provider) DeleteObject

```go
func (p *Provider) DeleteObject(ctx context.Context, in *storage.DeleteObjectInput) error
```

DeleteObject deletes the object in, or one version of it, as described
in the package documentation. Deleting a key that holds no object is not
an error.

<a id="Provider.GetObject"></a>

#### func (*Provider) GetObject

```go
func (p *Provider) GetObject(
	ctx context.Context,
	in *storage.GetObjectInput,
) (*storage.Object, error)
```

GetObject returns the object in, as described in the package
documentation. OpTimeout and a Close bound the call until the response
headers arrive; the Body is then governed by ctx alone.

<a id="Provider.HeadBucket"></a>

#### func (*Provider) HeadBucket

```go
func (p *Provider) HeadBucket(
	ctx context.Context,
	in *storage.HeadBucketInput,
) (*storage.Bucket, error)
```

HeadBucket returns the bucket in.Bucket with its name and the region S3
reports, or an error wrapping storage.ErrBucketNotFound. A bucket of
another account, or one the credentials may not read, is an error
wrapping storage.ErrAccessDenied.

<a id="Provider.HeadObject"></a>

#### func (*Provider) HeadObject

```go
func (p *Provider) HeadObject(
	ctx context.Context,
	in *storage.HeadObjectInput,
) (*storage.ObjectInfo, error)
```

HeadObject describes the object in, as described in the package
documentation.

<a id="Provider.Init"></a>

#### func (*Provider) Init

```go
func (p *Provider) Init(ctx context.Context) error
```

Init resolves and checks the configuration, builds the client and
retrieves the credentials, as described in the package documentation.
It sends no request to S3. On an initialized store it returns
storage.ErrAlreadyInitialized and changes nothing. A failed Init leaves
the store as it was.

<a id="Provider.ListBuckets"></a>

#### func (*Provider) ListBuckets

```go
func (p *Provider) ListBuckets(
	ctx context.Context,
	in *storage.ListBucketsInput,
) (*storage.ListBucketsOutput, error)
```

ListBuckets returns one page of the buckets the credentials own, as
described in the package documentation. S3's continuation token is
sealed in the token the page returns.

<a id="Provider.ListObjects"></a>

#### func (*Provider) ListObjects

```go
func (p *Provider) ListObjects(
	ctx context.Context,
	in *storage.ListObjectsInput,
) (*storage.ListObjectsOutput, error)
```

ListObjects returns one page of the objects of a bucket, as described
in the package documentation, with ListObjectsV2. It asks S3 to
URL-encode the keys, so that keys XML cannot carry are listed, and
decodes them; S3's continuation token is sealed in the token the page
returns.

<a id="Provider.Name"></a>

#### func (*Provider) Name

```go
func (*Provider) Name() string
```

Name returns ServiceName.

<a id="Provider.PutObject"></a>

#### func (*Provider) PutObject

```go
func (p *Provider) PutObject(
	ctx context.Context,
	in *storage.PutObjectInput,
) (*storage.PutObjectOutput, error)
```

PutObject writes the object in, as described in the package
documentation, with the AWS SDK's transfer manager: in one PutObject
when the body holds less than 16 MiB, else in parts of 8 MiB. Each
request is bounded by OpTimeout, and an upload in parts that fails is
aborted, on a context of its own bounded by OpTimeout, even when ctx
has ended.

<a id="Secret"></a>

### type Secret

```go
type Secret = gas.Secret
```

Secret is gas.Secret: a string that is never printed, marshaled or
logged, such as Settings.SecretAccessKey. Configuration binds into it as
into a string, and string(s) is the value.

<a id="Settings"></a>

### type Settings

```go
type Settings struct {
	// UsePathStyle sends the bucket in the URL's path rather than in its
	// host. Nil is true when Endpoint is set, and false otherwise.
	UsePathStyle *bool
	// Region is the AWS region; empty leaves it to the AWS SDK's default
	// chain.
	Region string
	// Endpoint is the URL requests go to, such as an emulator's; empty is
	// the region's S3 endpoint.
	Endpoint string
	// Bucket is the configured bucket, which an input with a nil Bucket
	// names; empty is none.
	Bucket string
	// AccessKeyID is the access key ID of static credentials, used only
	// with SecretAccessKey. It is not a secret, but an identifier that
	// maps to the AWS account ID, so the store never logs it.
	AccessKeyID string
	// SecretAccessKey is the secret access key of static credentials,
	// used only with AccessKeyID. It prints, marshals and logs as
	// `[redacted]`.
	SecretAccessKey Secret
	// SessionToken is the session token of temporary static credentials.
	// It prints, marshals and logs as `[redacted]`.
	SessionToken Secret
	// OpTimeout bounds each request, its retries included.
	OpTimeout time.Duration `validate:"gt=0"`
	// MaxRetries is how many times a failed request is retried.
	MaxRetries int `validate:"min=0,max=10"`
	// RawErrors makes a failed request's error also show and wrap the AWS
	// SDK's error, which can hold the endpoint, a bucket, a key, an upload
	// ID and the server's message. Off by default; use it to debug.
	RawErrors bool
}
```

Settings are the S3 store's settings, as described in the package
documentation.
