Documentation
¶
Overview ¶
Package s3 is a storage.Store on Amazon S3 and S3-compatible object stores.
It is a library package: nothing in the server uses it yet.
Stored format ¶
Every object this package writes is the caller's bytes followed by a 32-byte trailer: the 8 bytes "SKYLS3T1" and 24 bytes from crypto/rand that are new for every write. The same 24 bytes, as 48 lowercase hex digits, are stored in the object's user metadata under "skylite-id" (the header x-amz-meta-skylite-id). An object's size through the interface is its stored size minus 32. Reads never return trailer bytes, and Head, Get, GetRange and List never count them. Head, Get and GetRange reject an object without that metadata or shorter than the trailer with an error that wraps none of the storage sentinels, and List fails on an object shorter than the trailer; nothing else may write under the bucket.
Version tokens and conditions ¶
The version token is the object's ETag, as S3 returns it. S3 derives an ETag from the stored bytes, so on its own it would repeat when the same content is written twice. The random trailer makes the stored bytes, and so the ETag, new on every write. Create sends PutObject with If-None-Match: * and Replace sends it with If-Match: <token>, so each is one conditional request that S3 evaluates atomically, and the token a Replace is conditioned on names the same per-write identity that the token was read from. A Replace whose version is empty or not a quoted entity tag (such as "*", which would match any object) fails its precondition without a request. S3 answers If-Match on a missing key with 404, which Replace reports as a precondition failure.
Upload path ¶
A write copies the caller's body to a spool file in Config.SpoolDir, then sends it with one PutObject. The spool file is unlinked as soon as it is created where the operating system allows it (Linux and macOS), so a killed process leaves nothing behind, and it is removed when the call returns everywhere else. Memory per write is bounded by the 32 KiB copy buffer and the SDK's fixed per-request buffers; disk per write is the body size plus 32 bytes. Nothing is sent before the body has been read to its end, and S3 never stores part of a PutObject, so a write that fails before its request is complete leaves nothing behind, at the key or anywhere else. No staged (multipart) upload is used, so a body is limited to S3's single-request limit of 5 GiB minus the trailer, 5,368,709,088 bytes. A larger body fails with storage.ErrInvalidArgument and no effect, after at most that many bytes plus one have been read.
Retries and unknown outcomes ¶
The SDK's standard retryer runs for every operation, up to 3 attempts, with its client-side retry quota disabled and S3's 409 ConditionalRequestConflict added to the codes it retries. Each attempt of a mutation is classified as it ends: an HTTP response below 500 other than 2xx is a definite answer with no effect, and so is a transport error before the whole request was written (seen through net/http/httptrace); a 2xx that failed to decode, a 5xx, or a transport error after the whole request was written may have taken effect. The SDK retries a mutation only while every attempt so far definitely had no effect, so when a call returns, none of its attempts can still be pending unless the call reports an unknown outcome. Reads are retried as the SDK decides.
A write that fails definitely returns an error wrapping storage.ErrPreconditionFailed for a 412 (and for Replace a 404 NoSuchKey), or the SDK's error. A write whose attempt may have taken effect is resolved by its per-write identity: while its context is live it sends a HeadObject, and if the object carries this write's identity in its metadata the write succeeded, with that object's ETag as its token. Anything else wraps storage.ErrOutcomeUnknown. Delete is unconditional: if its attempt may have taken effect, it returns an error wrapping storage.ErrOutcomeUnknown.
Reads ¶
Get and GetRange send one ranged GetObject and return the size and ETag of the response the body comes from, so the body and its Info belong to one version. A range that starts in the trailer returns an empty body. S3 answers a range that starts past the stored object with 416; GetRange then sends a HeadObject and returns an empty body if the offset is at or past the end of the version it finds, and tries again if the object has grown in between. GetRange of length 0 sends only a HeadObject. HeadObject cannot tell a missing bucket from a missing key, so Head reports both as storage.ErrNotFound.
Listing ¶
List sends ListObjectsV2 with the prefix, StartAfter and MaxKeys set to the entries still missing, and keeps requesting from the last key it got until it has limit entries or the service reports no more, so short pages are filled. A prefix that contains a byte keys cannot contain, or is longer than storage.MaxKeyLen, matches nothing and is answered without a request. A startAfter that keys cannot contain is replaced by one made of key characters that selects the same keys, so the service never sees bytes it may reject.
Limits ¶
- The bucket must exist. Nothing else may write under it.
- Strong read-after-write consistency and conditional writes are taken from the service; S3 has documented both since 2024. An S3-compatible store that ignores If-None-Match or If-Match, or does not apply them atomically, breaks the storage contract and is not supported.
- Bucket versioning is not needed. If it is enabled, old versions are kept as the bucket's lifecycle rules decide.
- Tested against the RustFS emulator only (see the documentation site), which rejects keys with a segment longer than 255 bytes; S3 does not.
Index ¶
- type Config
- type Store
- func (s *Store) Create(ctx context.Context, key string, body io.Reader) (string, error)
- func (s *Store) Delete(ctx context.Context, key string) error
- func (s *Store) Get(ctx context.Context, key string) (io.ReadCloser, storage.Info, error)
- func (s *Store) GetRange(ctx context.Context, key string, offset, length int64) (io.ReadCloser, storage.Info, error)
- func (s *Store) Head(ctx context.Context, key string) (storage.Info, error)
- func (s *Store) List(ctx context.Context, prefix, startAfter string, limit int) ([]storage.Entry, error)
- func (s *Store) Replace(ctx context.Context, key, version string, body io.Reader) (string, error)
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Config ¶
type Config struct {
// Bucket is the bucket that holds the objects. It must exist.
Bucket string
// Region is the bucket's region, used for request signing.
Region string
// Credentials signs every request.
Credentials aws.CredentialsProvider
// Endpoint is the base URL of an S3-compatible service or emulator, such
// as http://127.0.0.1:9000. Empty means Amazon S3's own endpoints.
Endpoint string
// UsePathStyle addresses the bucket in the URL path instead of the host
// name, as most emulators need.
UsePathStyle bool
// SpoolDir is the directory where write bodies are spooled before they
// are sent. Empty means os.TempDir().
SpoolDir string
}
Config configures a Store.
type Store ¶
type Store struct {
// contains filtered or unexported fields
}
Store is a storage.Store on one S3 bucket.
func (*Store) GetRange ¶
func (s *Store) GetRange(ctx context.Context, key string, offset, length int64) (io.ReadCloser, storage.Info, error)
GetRange implements storage.Store.