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) Delete(ctx context.Context, fullpath string) error
- func (b *Bucket) Open(name string) (fs.File, error)
- func (b *Bucket) OpenRange(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
- 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
- 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") )
var DefaultClient = http.Client{ Transport: &http.Transport{ ResponseHeaderTimeout: 60 * time.Second, MaxIdleConnsPerHost: 5, DisableCompression: true, DialContext: (&net.Dialer{ Timeout: 2 * time.Second, }).DialContext, }, }
DefaultClient is the default HTTP client used for requests made from this package.
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 {
Client *http.Client // HTTP client used for requests, if nil then DefaultClient is used
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) 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) 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) VisitDir ¶
func (b *Bucket) VisitDir(name, seek, pattern string, walk fsutil.VisitDirFn) error
VisitDir implements fs.VisitDirFS
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
type Prefix ¶
type Prefix struct {
Key *aws.SigningKey `xml:"-"` // Key is the signing key used to sign requests.
Client *http.Client `xml:"-"` // Client is the HTTP client used to make requests. If it is nil, then DefaultClient will be used.
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. 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:"-"`
// Client is the HTTP client used to
// make HTTP requests. By default it is
// populated with DefaultClient, but
// it may be set to any reasonable http client
// implementation.
Client *http.Client `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"`
}
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.