Documentation
¶
Index ¶
- type Client
- func (c *Client) CheckBucket(ctx context.Context) error
- func (c *Client) Get(ctx context.Context, key string) (*GetResult, error)
- func (c *Client) Head(ctx context.Context, key string) (*HeadResult, error)
- func (c *Client) List(ctx context.Context, prefix, continuationToken string) (*ListResult, error)
- func (c *Client) Put(ctx context.Context, key string, body io.Reader, contentType string, ...) (*PutResult, error)
- type GetResult
- type HeadResult
- type ListResult
- type ObjectInfo
- type PutOption
- type PutResult
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 New ¶
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
CheckBucket confirms the bucket exists and the caller can reach it. A missing bucket wraps ErrNotFound.
func (*Client) Get ¶
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
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 ¶
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 ¶
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
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
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.