Documentation
¶
Overview ¶
Package s3io is an abstraction layer on the s3 sdk.
The s3io package provides a bucket that can interact with all elements within the bucket. And can be configured with the "WithBucket..." options
bucket, err := s3io.Open(ctx, "my-bucket-name", s3io.WithBucketCredentials("access-key", "secret-key"))
There is an Reader to preform read opperations on an s3 object.
rd := bucket.Get(ctx, "path/to/object.txt", s3io.WithReaderConcurrency(10)) _, err := io.Copy(os.Stdout, rd)
And there is an Writer to preform write opperations on an s3 object. Note The writer MUST close to safe the object.
wr := bucket.Put(ctx, "path/to/object.txt") defer wr.Close() _, err := io.WriteString(wr, "Hello world!") if err != nil { return err } if err := wr.Close(); err != nil { return err }
The s3io reader and writer stream the objects from and to your s3 instance while being memory efficient.
Index ¶
- Constants
- func NewReader(ctx context.Context, s3 DownloadAPIClient, input *s3.GetObjectInput, ...) io.Reader
- func NewWriter(ctx context.Context, s3 UploadAPIClient, input *s3.PutObjectInput, ...) io.WriteCloser
- type Bucket
- type BucketApiClient
- type BucketOption
- func BucketOptions(opts ...BucketOption) BucketOption
- func WithBucketCli(cli *s3.Client) BucketOption
- func WithBucketCliLoaderOptions(opts ...func(*config.LoadOptions) error) BucketOption
- func WithBucketConcurrency(size int) BucketOption
- func WithBucketCreateIfNotExists() BucketOption
- func WithBucketCredentials(accessKey, secretKey string) BucketOption
- func WithBucketHost(url, region string, usePathStyle bool) BucketOption
- func WithBucketLogger(logger *slog.Logger) BucketOption
- func WithBucketReadChunkSize(size int64) BucketOption
- func WithBucketRetries(i int) BucketOption
- func WithBucketS3Options(opts ...func(*s3.Options)) BucketOption
- func WithBucketWriteChunkSize(size int64) BucketOption
- type BucketURIError
- type DeleteBucketAPI
- type DeleteBucketBucketAPI
- type DownloadAPIClient
- type ExistsBucketAPI
- type GetBucketAPI
- type ListBucketAPI
- type PutBucketAPI
- type Reader
- type ReaderOption
- func ObjectReaderOptions(opts ...ReaderOption) ReaderOption
- func WithReaderChunkSize(size int64) ReaderOption
- func WithReaderConcurrency(i int) ReaderOption
- func WithReaderLogger(logger *slog.Logger) ReaderOption
- func WithReaderRetries(i int) ReaderOption
- func WithReaderS3Options(opts ...func(*s3.Options)) ReaderOption
- type UploadAPIClient
- type Writer
- type WriterOption
- func ObjectWriterOptions(opts ...WriterOption) WriterOption
- func WithWriteRetries(i int) WriterOption
- func WithWriterACL(acl types.ObjectCannedACL) WriterOption
- func WithWriterChunkSize(size int64) WriterOption
- func WithWriterClientOptions(opts ...func(*s3.Options)) WriterOption
- func WithWriterConcurrency(i int) WriterOption
- func WithWriterLogger(logger *slog.Logger) WriterOption
Examples ¶
Constants ¶
const ( MinChunkSize int64 = 1024 * 1024 * 5 DefaultChunkSize int64 = MinChunkSize )
Variables ¶
This section is empty.
Functions ¶
func NewReader ¶
func NewReader(ctx context.Context, s3 DownloadAPIClient, input *s3.GetObjectInput, opts ...ReaderOption) io.Reader
NewReader returns a new ObjectReader to do io.Reader opperations on your s3 object
func NewWriter ¶
func NewWriter(ctx context.Context, s3 UploadAPIClient, input *s3.PutObjectInput, opts ...WriterOption) io.WriteCloser
NewWriter returns a new ObjectWriter to do io.Writer opperations on your s3 object
Types ¶
type Bucket ¶
type Bucket interface { ExistsBucketAPI DeleteBucketBucketAPI DeleteBucketAPI ListBucketAPI GetBucketAPI PutBucketAPI fmt.Stringer }
func New ¶
func New( name string, readChunkSize, writeChunkSize int64, concurrency int, logger *slog.Logger, cli BucketApiClient, ) Bucket
New returns a new bucket instance. For normal operations use Open instead as this will connect and verify the bucket.
func OpenURL ¶
OpenURL opens the bucket with all the connection options in the url. The url is written as: s3://access-key:access-secret@host/bucketname?region=us-east&pathstyle=true
The url assumes the host has a https protocol unless the "insecure" query param is set to "true". To create the bucket if it doesn't exist set the "create" query param to "true". To use the pathstyle url set "pathstyle" to "true".
type BucketApiClient ¶
type BucketApiClient interface { DownloadAPIClient UploadAPIClient s3.HeadObjectAPIClient s3.ListObjectsV2APIClient DeleteObject(ctx context.Context, input *s3.DeleteObjectInput, optFns ...func(*s3.Options)) (*s3.DeleteObjectOutput, error) DeleteObjects(ctx context.Context, input *s3.DeleteObjectsInput, optFns ...func(*s3.Options)) (*s3.DeleteObjectsOutput, error) DeleteBucket(ctx context.Context, input *s3.DeleteBucketInput, optFns ...func(*s3.Options)) (*s3.DeleteBucketOutput, error) }
type BucketOption ¶
type BucketOption func(*bucketBuilder)
func BucketOptions ¶
func BucketOptions(opts ...BucketOption) BucketOption
BucketOptions bundles bucket options
func WithBucketCli ¶
func WithBucketCli(cli *s3.Client) BucketOption
WithCli directly sets the s3 client for the bucket.
func WithBucketCliLoaderOptions ¶
func WithBucketCliLoaderOptions(opts ...func(*config.LoadOptions) error) BucketOption
WithBucketCliLoaderOptions sets the config.LoaderOptions for the aws config. Only works if the cli is not already provided.
func WithBucketConcurrency ¶
func WithBucketConcurrency(size int) BucketOption
WithBucketConcurrency sets the default read/write concurrency
func WithBucketCreateIfNotExists ¶
func WithBucketCreateIfNotExists() BucketOption
WithBucketCreateIfNotExitsts will create the bucket if it doesn't already exist.
func WithBucketCredentials ¶
func WithBucketCredentials(accessKey, secretKey string) BucketOption
WithBucketCredentials sets the access key and secret key for the cli. Only works if the cli is not already provided.
func WithBucketHost ¶
func WithBucketHost(url, region string, usePathStyle bool) BucketOption
WithBucketHost sets the endpoint, region and if it uses a pathstyle for the cli. Only works if the cli is not already provided.
func WithBucketLogger ¶
func WithBucketLogger(logger *slog.Logger) BucketOption
WithBucketLogger sets the default logger for any opperation. Setting the logger provides debug logs.
func WithBucketReadChunkSize ¶
func WithBucketReadChunkSize(size int64) BucketOption
WithBucketReadChunkSize sets the default read chunksize
func WithBucketRetries ¶
func WithBucketRetries(i int) BucketOption
WithBucketRetries sets the default amount of retries for any given opperation
func WithBucketS3Options ¶
func WithBucketS3Options(opts ...func(*s3.Options)) BucketOption
WithBucketS3Options sets the s3 options for t.he s3 client. Only works if the cli is not already provided.
func WithBucketWriteChunkSize ¶
func WithBucketWriteChunkSize(size int64) BucketOption
WithBucketWriteChunkSize sets the default write chunksize
type BucketURIError ¶
type BucketURIError string
BucketURIError
var ( ErrInvalidScheme BucketURIError = "url doesn't start with s3 scheme" ErrNoBucketName BucketURIError = "no bucketname" ErrNoCredentials BucketURIError = "missing credentials" )
func (BucketURIError) Error ¶
func (b BucketURIError) Error() string
type DeleteBucketAPI ¶
type DeleteBucketBucketAPI ¶
type DownloadAPIClient ¶
type DownloadAPIClient interface {
GetObject(context.Context, *s3.GetObjectInput, ...func(*s3.Options)) (*s3.GetObjectOutput, error)
}
DownloadAPIClient is an S3 API client that can invoke the GetObject operation.
type ExistsBucketAPI ¶
type GetBucketAPI ¶
type ListBucketAPI ¶
type PutBucketAPI ¶
type PutBucketAPI interface {
Put(ctx context.Context, key string, opts ...WriterOption) io.WriteCloser
}
type Reader ¶
type Reader struct {
// contains filtered or unexported fields
}
Reader is an io.Reader implementation for an S3 Object.
You can open a new reader on it's own using the *s3.Client:
client := s3.NewFromConfig(cfg) rd := s3io.NewReader(ctx, client, s3io.WithReaderConcurrency(3))
Or you can open it using the BucketAPI:
bucket, err := s3io.Open(ctx, "my-bucket-name", s3io.WithBucketCredentials("access-key", "access-secret")) if err != nil { log.Fatalf("unable to open bucket: %w", err) } rd := bucket.Get(ctx, "path/to/object.txt")
func (*Reader) Read ¶
Read is the io.Reader implementation for the ObjectReader.
It returns an fs.ErrNotExists if the object doesn't exist in the given bucket. And returns an io.EOF when all bytes are read.
Example ¶
ctx := context.Background() bucket, err := Open(ctx, "bucket-name", WithBucketCredentials("access-key", "access-secret"), WithBucketCreateIfNotExists(), ) if err != nil { log.Fatal(err) } rd := bucket.Get(ctx, "path/to/object.txt") if err != nil { log.Fatal(err) } if _, err := io.Copy(os.Stdout, rd); err != nil { log.Fatal(err) }
type ReaderOption ¶
type ReaderOption func(*Reader)
ReaderOption is an option for the given read operation.
func ObjectReaderOptions ¶
func ObjectReaderOptions(opts ...ReaderOption) ReaderOption
ObjectReaderOptions is a collection of ObjectReaderOption's.
func WithReaderChunkSize ¶
func WithReaderChunkSize(size int64) ReaderOption
WithReaderChunkSize sets the chunksize for this reader
func WithReaderConcurrency ¶
func WithReaderConcurrency(i int) ReaderOption
WithReaderConcurrency set the concurency amount for this reader
func WithReaderLogger ¶
func WithReaderLogger(logger *slog.Logger) ReaderOption
WithReaderLogger sets the logger for this reader
func WithReaderRetries ¶
func WithReaderRetries(i int) ReaderOption
WithReaderRetries sets the retry count for this reader
func WithReaderS3Options ¶
func WithReaderS3Options(opts ...func(*s3.Options)) ReaderOption
WithReaderS3Options adds s3 options to the reader opperations
type UploadAPIClient ¶
type UploadAPIClient interface { PutObject(context.Context, *s3.PutObjectInput, ...func(*s3.Options)) (*s3.PutObjectOutput, error) UploadPart(context.Context, *s3.UploadPartInput, ...func(*s3.Options)) (*s3.UploadPartOutput, error) CreateMultipartUpload(context.Context, *s3.CreateMultipartUploadInput, ...func(*s3.Options)) (*s3.CreateMultipartUploadOutput, error) CompleteMultipartUpload(context.Context, *s3.CompleteMultipartUploadInput, ...func(*s3.Options)) (*s3.CompleteMultipartUploadOutput, error) AbortMultipartUpload(context.Context, *s3.AbortMultipartUploadInput, ...func(*s3.Options)) (*s3.AbortMultipartUploadOutput, error) }
UploadAPIClient is an S3 API client that can invoke PutObject, UploadPart, CreateMultipartUpload, CompleteMultipartUpload, and AbortMultipartUpload operations.
type Writer ¶
type Writer struct {
// contains filtered or unexported fields
}
Writer is an io.WriteCloser implementation for an s3 Object.
You can open a new writer on its own using the *s3.Client:
client := s3.NewFromConfig(cfg) wr := s3io.NewWriter(ctx, client, s3io.WithWriterConcurrency(5))
Or you can open it usign the BucketAPI:
bucket, err := s3io.Open(ctx, "my-bucket-name", s3io.WithBucketCredentials("access-key", "access-secret")) if err != nil { log.Fatalf("unable to open bucket: %w", err) } wr := bucket.Put(ctx, "path/to/object.txt")
func (*Writer) Close ¶
Close completes the write opperation.
If the byte size is less than writer's chunk size then a simply PutObject opperation is preformed. Otherwise a multipart upload complete opperation is preformed. The error returned is the error from this store opperation.
If an error occured while uploading parts this error might also be a upload part error joined with a AbortMultipartUpload error.
func (*Writer) Write ¶
Write is the io.Writer implementation of the ObjectWriter
The object is stored when the Close method is called.
Example ¶
ctx := context.Background() bucket, err := Open(ctx, "bucket-name", WithBucketCredentials("access-key", "access-secret"), WithBucketCreateIfNotExists(), ) if err != nil { log.Fatal(err) } wr := bucket.Put(ctx, "path/to/object.txt") if err != nil { log.Fatal(err) } if _, err := io.WriteString(wr, "hello world"); err != nil { log.Fatal(err) } // Note: you must close to finilize the upload if err := wr.Close(); err != nil { log.Fatal(err) }
type WriterOption ¶
type WriterOption func(*Writer)
WriterOption is an option for the given write operation
func ObjectWriterOptions ¶
func ObjectWriterOptions(opts ...WriterOption) WriterOption
ObjectWriterOptions is a collection of ObjectWriterOption's
func WithWriteRetries ¶
func WithWriteRetries(i int) WriterOption
WithWriterRetries sets the retry count for this writer
func WithWriterACL ¶
func WithWriterACL(acl types.ObjectCannedACL) WriterOption
Set ACL after giving the input
func WithWriterChunkSize ¶
func WithWriterChunkSize(size int64) WriterOption
WithWriterChunkSize sets the chunksize for this writer. If set below the minimal chunk size of 5Mb then it will be set to the minimal chunksize.
func WithWriterClientOptions ¶
func WithWriterClientOptions(opts ...func(*s3.Options)) WriterOption
WithWriterClientOptions adds s3 client options to the writer opperations
func WithWriterConcurrency ¶
func WithWriterConcurrency(i int) WriterOption
WithWriterConcurrency sets the concurrency amount for this writer.
func WithWriterLogger ¶
func WithWriterLogger(logger *slog.Logger) WriterOption
WithWriterLogger adds a logger for this writer.