Documentation
¶
Overview ¶
Package s3 is the S3-compatible blob store (AWS S3, MinIO, Ceph RGW and similar). Blobs live at <prefix>sha256/<ab>/<hex>.
Object stores cannot append or rename, so an upload session is shaped around those limits:
- Bytes accumulate in a pending buffer of at most PartSize. A blob that never fills one part is written with a single PutObject at commit; most package files are that small, and they never pay for a multipart upload.
- When the buffer fills, the session starts an S3 multipart upload (on the first full part) and uploads the part. S3 requires every part except the last to be at least 5 MiB, which is why appends are buffered rather than uploaded as they arrive: Docker clients PATCH in chunks of any size.
- The multipart upload ID, the part list, the pending tail and the hash state are saved through a session.Store after every append, so a session resumes in a later request (decision 0027; session.InUploads keeps it in the metadata database).
- At commit the content key is known. If the client declared the digest up front the multipart upload already targets the content key; otherwise it targets a temporary key and is copied into place, with a multipart copy above S3's 5 GiB single-copy limit.
- Every write of a content key is a must-not-exist write (If-None-Match: *, decision 0027), as the GCS and Azure backends write, so a blob that is already stored is never written again and two uploads of one blob cannot race. S3 answers 412 for a taken key, and the existing object is adopted. There is no HeadObject before the write.
Index ¶
- Constants
- Variables
- func NewClient(ctx context.Context, cfg ClientConfig) (*s3.Client, *s3.PresignClient, error)
- type API
- type ClientConfig
- type Options
- type PresignAPI
- type Store
- func (s *Store) BeginUpload(ctx context.Context, opts ...blob.UploadOption) (blob.Upload, error)
- func (s *Store) Delete(ctx context.Context, sha string) error
- func (s *Store) Exists(ctx context.Context, sha string) (bool, error)
- func (s *Store) ObjectKey(sha string) string
- func (s *Store) Open(ctx context.Context, sha string) (io.ReadSeekCloser, blob.Info, error)
- func (s *Store) PartSize() int64
- func (s *Store) ResumeUpload(ctx context.Context, id string) (blob.Upload, error)
- func (s *Store) SignURL(ctx context.Context, sha string, o blob.SignOptions) (string, error)
- func (s *Store) Signs(via blob.Via) bool
- func (s *Store) Stat(ctx context.Context, sha string) (blob.Info, error)
- func (s *Store) Walk(ctx context.Context, fn func(blob.Info) error) error
Constants ¶
const ( // DefaultPartSize balances request count against the memory one session // holds for its pending tail. DefaultPartSize = 8 << 20 )
Variables ¶
var ErrConfig = errors.New("blob/s3: invalid configuration")
ErrConfig reports an unusable store configuration.
var ErrSeek = errors.New("blob/s3: invalid seek")
ErrSeek reports an invalid Seek.
Functions ¶
func NewClient ¶
func NewClient(ctx context.Context, cfg ClientConfig) (*s3.Client, *s3.PresignClient, error)
NewClient builds the S3 client and presigner for cfg.
Types ¶
type API ¶
type API interface {
PutObject(
context.Context,
*s3.PutObjectInput,
...func(*s3.Options),
) (*s3.PutObjectOutput, error)
GetObject(
context.Context,
*s3.GetObjectInput,
...func(*s3.Options),
) (*s3.GetObjectOutput, error)
HeadObject(
context.Context,
*s3.HeadObjectInput,
...func(*s3.Options),
) (*s3.HeadObjectOutput, error)
DeleteObject(
context.Context,
*s3.DeleteObjectInput,
...func(*s3.Options),
) (*s3.DeleteObjectOutput, error)
CopyObject(
context.Context,
*s3.CopyObjectInput,
...func(*s3.Options),
) (*s3.CopyObjectOutput, error)
ListObjectsV2(
context.Context,
*s3.ListObjectsV2Input,
...func(*s3.Options),
) (*s3.ListObjectsV2Output, error)
CreateMultipartUpload(
context.Context, *s3.CreateMultipartUploadInput, ...func(*s3.Options),
) (*s3.CreateMultipartUploadOutput, error)
UploadPart(
context.Context,
*s3.UploadPartInput,
...func(*s3.Options),
) (*s3.UploadPartOutput, error)
UploadPartCopy(
context.Context,
*s3.UploadPartCopyInput,
...func(*s3.Options),
) (*s3.UploadPartCopyOutput, error)
CompleteMultipartUpload(
context.Context, *s3.CompleteMultipartUploadInput, ...func(*s3.Options),
) (*s3.CompleteMultipartUploadOutput, error)
AbortMultipartUpload(
context.Context, *s3.AbortMultipartUploadInput, ...func(*s3.Options),
) (*s3.AbortMultipartUploadOutput, error)
}
API is the subset of *s3.Client the store uses, so tests can substitute a fake that injects failures.
type ClientConfig ¶
type ClientConfig struct {
// Endpoint overrides the AWS endpoint, for MinIO and other S3-compatible
// stores, e.g. "http://minio:9000". Empty means AWS.
Endpoint string
// Region is required by the signer even for stores that ignore it.
Region string
// PathStyle addresses buckets as <endpoint>/<bucket> instead of
// <bucket>.<endpoint>; MinIO and most self-hosted stores need it.
PathStyle bool
// AccessKey and SecretKey are static credentials. Both empty means the
// AWS default chain (environment, shared config, instance role).
AccessKey string
SecretKey string
// Profile selects a named profile of the shared AWS configuration, so
// stores in one process can use different accounts. Empty means the
// default profile (or AWS_PROFILE).
Profile string
}
ClientConfig mirrors the blob.s3 section of the application configuration.
type Options ¶
type Options struct {
// Bucket must already exist.
Bucket string
// Prefix is prepended to every key; a trailing slash is added if missing.
Prefix string
// PartSize is the multipart part size; zero means DefaultPartSize. S3
// rejects parts under 5 MiB, so smaller values are refused.
PartSize int64
// Sessions holds upload sessions between requests; session.InUploads
// with a backend name such as "s3:<store name>" keeps them durable.
Sessions session.Store
// Presign signs storage URLs when set (blob.Signer); NewClient always
// returns one.
Presign PresignAPI
// CDN signs CDN URLs when set, such as a cdn.CloudFront in front of the
// bucket (decision 0029).
CDN cdn.Signer
}
Options configures New.
type PresignAPI ¶
type PresignAPI interface {
PresignGetObject(
context.Context, *s3.GetObjectInput, ...func(*s3.PresignOptions),
) (*v4.PresignedHTTPRequest, error)
}
PresignAPI is the subset of *s3.PresignClient the store uses.
type Store ¶
type Store struct {
// contains filtered or unexported fields
}
Store is a blob.Store on an S3 bucket.
func (*Store) BeginUpload ¶
BeginUpload starts a session.
func (*Store) ObjectKey ¶
ObjectKey is the key a blob is stored under, prefix included, for signers that address the bucket directly, such as a CDN. sha must be a valid digest (blob.ValidateSHA256).
func (*Store) ResumeUpload ¶
ResumeUpload reopens a session.
func (*Store) SignURL ¶
SignURL returns a time-limited GET URL, carrying o's filename and media type as response overrides.
func (*Store) Signs ¶
Signs reports whether the store signs URLs for via: storage URLs when it has a presign client, CDN URLs when it has a CDN signer.