Documentation
¶
Overview ¶
Package s3 implements a lightweight client of the AWS S3 API.
The Reader type can be used to view S3 objects as an io.Reader or io.ReaderAt.
Index ¶
- Constants
- Variables
- func BucketRegion(k *aws.SigningKey, bucket string) (string, error)
- func DeriveForBucket(bucket string) aws.DeriveFn
- func URL(k *aws.SigningKey, bucket, object string) (string, error)
- func ValidBucket(bucket string) bool
- type Bucket
- func (b *Bucket) Compose(ctx context.Context, key string, parts []CopyPart) (string, error)
- func (b *Bucket) Delete(ctx context.Context, fullpath string) error
- func (b *Bucket) List(ctx context.Context, name string) iter.Seq2[fs.DirEntry, error]
- func (b *Bucket) Open(name string) (fs.File, error)
- func (b *Bucket) OpenContext(ctx context.Context, name string) (fs.File, error)
- func (b *Bucket) OpenRange(name, etag string, start, width int64) (io.ReadCloser, error)
- func (b *Bucket) OpenRangeContext(ctx context.Context, name, etag string, start, width int64) (io.ReadCloser, error)
- func (b *Bucket) ReadDir(name string) ([]fs.DirEntry, error)
- func (b *Bucket) Sub(dir string) (fs.FS, error)
- func (b *Bucket) VisitDir(name, seek, pattern string, walk fsutil.VisitDirFn) error
- func (b *Bucket) Write(ctx context.Context, key string, contents []byte) (string, error)
- func (b *Bucket) WriteFrom(ctx context.Context, key string, r io.ReaderAt, size int64) error
- func (b *Bucket) WriteIf(ctx context.Context, key string, contents []byte, condition Condition) (etag string, applied bool, err error)
- type Condition
- type CopyPart
- type File
- func (f *File) Close() error
- func (f *File) Info() (fs.FileInfo, error)
- func (f *File) IsDir() bool
- func (f *File) ModTime() time.Time
- func (f *File) Mode() fs.FileMode
- func (f *File) Name() string
- func (f *File) Open() (fs.File, error)
- func (f *File) Path() string
- func (f *File) Read(p []byte) (int, error)
- func (f *File) Seek(offset int64, whence int) (int64, error)
- func (f *File) Size() int64
- func (f *File) Stat() (fs.FileInfo, error)
- func (f *File) Sys() interface{}
- func (f *File) Type() fs.FileMode
- func (f *File) WriteTo(w io.Writer) (int64, error)
- type Prefix
- func (p *Prefix) Close() error
- func (p *Prefix) Info() (fs.FileInfo, error)
- func (p *Prefix) IsDir() bool
- func (p *Prefix) ModTime() time.Time
- func (p *Prefix) Mode() fs.FileMode
- func (p *Prefix) Name() string
- func (p *Prefix) Open(file string) (fs.File, error)
- func (p *Prefix) Read(_ []byte) (int, error)
- func (p *Prefix) ReadDir(n int) ([]fs.DirEntry, error)
- func (p *Prefix) Size() int64
- func (p *Prefix) Stat() (fs.FileInfo, error)
- func (p *Prefix) Sys() interface{}
- func (p *Prefix) Type() fs.FileMode
- func (p *Prefix) VisitDir(name, seek, pattern string, walk fsutil.VisitDirFn) error
- type Reader
Constants ¶
const ( MinPartSize = 5 * 1024 * 1024 MaxParts = 10000 // AWS limit )
Default upload configuration values
Variables ¶
var ( // ErrInvalidBucket is returned from calls that attempt // to use a bucket name that isn't valid according to // the S3 specification. ErrInvalidBucket = errors.New("invalid bucket name") // ErrETagChanged is returned from read operations where // the ETag of the underlying file has changed since // the file handle was constructed. (This package guarantees // that file read operations are always consistent with respect // to the ETag originally associated with the file handle.) ErrETagChanged = errors.New("file ETag changed") )
Functions ¶
func BucketRegion ¶
func BucketRegion(k *aws.SigningKey, bucket string) (string, error)
BucketRegion returns the region associated with the given bucket.
func DeriveForBucket ¶
DeriveForBucket can be passed to aws.AmbientCreds as a DeriveFn that automatically re-derives keys so that they apply to the region in which the given bucket lives.
func URL ¶
func URL(k *aws.SigningKey, bucket, object string) (string, error)
URL returns a signed URL for a bucket and object that can be used directly with http.Get.
func ValidBucket ¶
ValidBucket returns whether or not bucket is a valid bucket name.
See https://docs.aws.amazon.com/AmazonS3/latest/userguide/bucketnamingrules.html
Note: ValidBucket does not allow '.' characters, since bucket names containing dots are not accessible over HTTPS. (AWS docs say "not recommended for uses other than static website hosting.")
Types ¶
type Bucket ¶
type Bucket struct {
Lazy bool // If true, causes the initial Open call to use a HEAD operation rather than a GET operation.
// contains filtered or unexported fields
}
Bucket implements fs.FS, fs.ReadDirFS, and fs.SubFS.
func NewBucket ¶
func NewBucket(key *aws.SigningKey, bucket string) *Bucket
NewBucket creates a new Bucket instance.
func (*Bucket) Compose ¶ added in v0.1.2
Compose concatenates immutable source ranges using multipart server-side copy.
func (*Bucket) List ¶ added in v0.2.0
List lazily lists a prefix. It yields the first error and stops.
func (*Bucket) Open ¶
Open implements fs.FS.Open
The returned fs.File will be either a *File or a *Prefix depending on whether name refers to an object or a common path prefix that leads to multiple objects. If name does not refer to an object or a path prefix, then Open returns an error matching fs.ErrNotExist.
func (*Bucket) OpenContext ¶ added in v0.3.0
OpenContext opens name using ctx for object reads and directory listing.
func (*Bucket) OpenRange ¶
OpenRange produces an io.ReadCloser that reads data from the file given by [name] with the etag given by [etag] starting at byte [start] and continuing for [width] bytes. If [etag] does not match the ETag of the object, then ErrETagChanged will be returned.
func (*Bucket) OpenRangeContext ¶ added in v0.3.0
func (b *Bucket) OpenRangeContext(ctx context.Context, name, etag string, start, width int64) (io.ReadCloser, error)
OpenRangeContext reads a byte range using ctx.
func (*Bucket) VisitDir ¶
func (b *Bucket) VisitDir(name, seek, pattern string, walk fsutil.VisitDirFn) error
VisitDir implements fs.VisitDirFS
func (*Bucket) Write ¶ added in v0.0.2
Write performs a PutObject operation at the object key 'key' and returns the ETag of the newly-created object.
func (*Bucket) WriteFrom ¶ added in v0.0.2
WriteFrom performs a multipart upload of data from an io.ReaderAt to the specified key.
func (*Bucket) WriteIf ¶ added in v0.3.0
func (b *Bucket) WriteIf(ctx context.Context, key string, contents []byte, condition Condition) (etag string, applied bool, err error)
WriteIf performs a PutObject only when condition's HTTP preconditions hold. It returns applied=false and nil error when S3 rejects the precondition. Conditional requests are not retried; after a transport error the write may have committed, so callers must resolve its outcome before retrying.
type Condition ¶ added in v0.3.0
Condition adds HTTP conditional request headers to a write. Implementations must set one or more If-* headers.
func IfNoneMatch ¶ added in v0.3.0
IfNoneMatch writes only when the object does not exist. S3 PutObject requires etag to be "*".
type CopyPart ¶ added in v0.1.2
CopyPart describes an immutable byte range to copy into a composed object.
type File ¶
type File struct {
Reader // Reader is a reader that points to the associated s3 object.
// contains filtered or unexported fields
}
File implements fs.File
func NewFile ¶
func NewFile(k *aws.SigningKey, bucket, object, etag string, size int64) *File
NewFile constructs a File that points to the given bucket, object, etag, and file size. The caller is assumed to have correctly determined these attributes in advance; this call does not perform any I/O to verify that the provided object exists or has a matching ETag and size.
func (*File) ModTime ¶
ModTime implements fs.DirEntry.ModTime. This returns the same value as f.Reader.LastModified.
func (*File) Path ¶
Path returns the full path to the S3 object within its bucket. See also blockfmt.NamedFile
func (*File) Read ¶
Read implements fs.File.Read
Note: Read is not safe to call from multiple goroutines simultaneously. Use ReadAt for parallel reads.
Also note: the first call to Read performs an HTTP request to S3 to read the entire contents of the object starting at the current read offset (zero by default, or another offset set via Seek). If you need to read a sub-range of the object, consider using f.Reader.RangeReader
func (*File) Seek ¶
Seek implements io.Seeker
Seek rejects offsets that are beyond the size of the underlying object.
type Prefix ¶
type Prefix struct {
Key *aws.SigningKey `xml:"-"` // Key is the signing key used to sign requests.
Bucket string `xml:"-"` // Bucket is the bucket at the root of the "filesystem"
Path string `xml:"Prefix"` // Path is the path of this prefix, should always be a valid path (see fs.ValidPath) plus a trailing forward slash to indicate that this is a pseudo-directory prefix.
// contains filtered or unexported fields
}
Prefix implements fs.File, fs.ReadDirFile, and fs.DirEntry, and fs.FS.
func (*Prefix) ModTime ¶
ModTime implements fs.FileInfo.ModTime
Note: currently ModTime returns the zero time.Time, as S3 prefixes don't have a meaningful modification time.
func (*Prefix) Open ¶
Open opens the object or pseudo-directory at the provided path. If both exist, Open prefers the pseudo-directory. The returned fs.File will be a *File if the combined Prefix and path lead to an object; if the combind prefix and path produce another complete object prefix, then a *Prefix will be returned. If the combined prefix and path do not produce a prefix that is present within the target bucket, then an error matching fs.ErrNotExist is returned.
type Reader ¶
type Reader struct {
// Key is the sigining key that
// Reader uses to make HTTP requests.
// The key may have to be refreshed
// every so often (see aws.SigningKey.Expired)
Key *aws.SigningKey `xml:"-"`
// ETag is the ETag of the object in S3
// as returned by listing or a HEAD operation.
ETag string `xml:"ETag"`
// LastModified is the object's LastModified time
// as returned by listing or a HEAD operation.
LastModified time.Time `xml:"LastModified"`
// Size is the object size in bytes.
// It is populated on Open.
Size int64 `xml:"Size"`
// Bucket is the S3 bucket holding the object.
Bucket string `xml:"-"`
// Path is the S3 object key.
Path string `xml:"Key"`
// contains filtered or unexported fields
}
Reader presents a read-only view of an S3 object
func Stat ¶
func Stat(k *aws.SigningKey, bucket, object string) (*Reader, error)
Stat performs a HEAD on an S3 object and returns an associated Reader.
func (*Reader) RangeReader ¶
func (r *Reader) RangeReader(off, width int64) (io.ReadCloser, error)
RangeReader produces an io.ReadCloser that reads bytes in the range from [off, off+width)
It is the caller's responsibility to call Close() on the returned io.ReadCloser.