s3io

package module
v2.2.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: May 13, 2024 License: MIT Imports: 18 Imported by: 0

README

S3IO

Go Reference Go Report Card

An abstraction layer on top of the s3 sdk to do io read/write opperations on s3 objects. The s3io reader and writer stream the objects from and to your s3 instance while being memory efficient.

// Note the "WithBucket..." are options
bucket, err := s3io.OpenBucket(ctx, "my-bucket-name", s3io.WithBucketCredentials(accessKey, secretKey))
if err != nil {
  return err
}

// Note the "WithBucket..." are options specifically for this writer session
writer, err := bucket.NewWriter(ctx, "path/to/object.txt", s3io.WithWriterRetries(3))
if err != nil {
  return err
}

if _, err := io.WriteString(writer, "Hello world!"); err != nil {
  return err
}

if err := writer.Close(); err != nil {
  return err 
}

reader, err := bucket.NewReader(ctx, "path/to/object.txt")
if err != nil {
  return err
}

_, err := io.Copy(os.Stdout, reader)
...

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.OpenBucket(ctx, "my-bucket-name", s3io.WithBucketCredentials("access-key", "secret-key"))

There is an ObjectReader to preform read opperations on an s3 object.

rd, err := bucket.NewReader(ctx, "path/to/object.txt", s3io.WithReaderConcurrency(10))
if err != nil {
  return err
}

_, err := io.Copy(os.Stdout, rd)

And there is an ObjectWriter to preform write opperations on an s3 object. Note The writer MUST close to safe the object.

wr, err := bucket.NewWriter(ctx, "path/to/object.txt")
if err != nil
  return err
}

_, 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

Examples

Constants

View Source
const (
	MinChunkSize     int64 = 1024 * 1024 * 5
	DefaultChunkSize int64 = MinChunkSize
)

Variables

This section is empty.

Functions

func NewObjectReader

func NewObjectReader(ctx context.Context, s3 DownloadAPIClient, input *s3.GetObjectInput, opts ...ObjectReaderOption) io.Reader

NewObjectReader returns a new ObjectReader to do io.Reader opperations on your s3 object

func NewObjectWriter

func NewObjectWriter(ctx context.Context, s3 UploadAPIClient, input *s3.PutObjectInput, opts ...ObjectWriterOption) io.WriteCloser

NewObjectWriter returns a new ObjectWriter to do io.Writer opperations on your s3 object

func ReadAll added in v2.2.0

func ReadAll(ctx context.Context, s3 DownloadAPIClient, input *s3.GetObjectInput, opts ...ObjectReaderOption) ([]byte, error)

ReadAll reads all the bytes for a given object

func WriteAllBytes added in v2.2.0

func WriteAllBytes(ctx context.Context, s3 UploadAPIClient, input *s3.PutObjectInput, p []byte, opts ...ObjectWriterOption) (int, error)

WriteAllBytes writes the given bytes into hte given object

func WriteAllFromBody added in v2.2.0

func WriteAllFromBody(ctx context.Context, s3 UploadAPIClient, input *s3.PutObjectInput, opts ...ObjectWriterOption) (int64, error)

WriteAllFromBody writes to the given object using the input.Body

Types

type Bucket

type Bucket struct {
	// contains filtered or unexported fields
}

Bucket is an abstraction to interact with objects in your S3 bucket

func OpenBucket

func OpenBucket(ctx context.Context, name string, opts ...BucketOption) (*Bucket, error)

OpenBucket returns a bucket to interact with.

func OpenBucketkwithClient

func OpenBucketkwithClient(ctx context.Context, cli *s3.Client, name string, opts ...BucketOption) (*Bucket, error)

OpenBucketkwithClient returns a bucket to interact with.

func (*Bucket) Client

func (b *Bucket) Client() *s3.Client

Client returns the s3 client the Bucket uses

func (*Bucket) Delete

func (b *Bucket) Delete(ctx context.Context, keys ...string) error

Delete deletes the given object keys

func (*Bucket) Exists

func (b *Bucket) Exists(ctx context.Context, key string) (bool, error)

Exists returns a a boolean indicating whether the requested object exists.

func (*Bucket) List

func (b *Bucket) List(ctx context.Context, prefix string) ([]types.Object, error)

List returns a list of objects details.

Use the prefix "/" to list all the objects in the bucket.

func (*Bucket) Name

func (b *Bucket) Name() string

Name returns the specified bucket's name

func (*Bucket) NewReader

func (b *Bucket) NewReader(ctx context.Context, key string, opts ...ObjectReaderOption) io.Reader

NewReader returns a new ObjectReader to do io.Reader opperations with your s3 object

func (*Bucket) NewWriter

func (b *Bucket) NewWriter(ctx context.Context, key string, opts ...ObjectWriterOption) io.WriteCloser

NewWriter returns a new ObjectWriter to do io.Write opparations with your s3 object

func (*Bucket) ReadAll added in v2.2.0

func (b *Bucket) ReadAll(ctx context.Context, key string, opts ...ObjectReaderOption) ([]byte, error)

ReadAll reads all the bytes of the given object

func (*Bucket) WriteAll added in v2.2.0

func (b *Bucket) WriteAll(ctx context.Context, key string, p []byte, opts ...ObjectWriterOption) (int, error)

WriteAll writes all the given bytes into the given object

func (*Bucket) WriteFrom added in v2.2.0

func (b *Bucket) WriteFrom(ctx context.Context, key string, from io.Reader, opts ...ObjectWriterOption) (int64, error)

WriteFrom writes all the bytes from the reader into the given object

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 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 ObjectReader

type ObjectReader struct {
	// contains filtered or unexported fields
}

ObjectReader is an io.Reader implementation for an S3 Object

func (*ObjectReader) Read

func (r *ObjectReader) Read(p []byte) (int, error)

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 := OpenBucket(ctx, "bucket-name",
	WithBucketCredentials("access-key", "access-secret"),
	WithBucketCreateIfNotExists(),
)
if err != nil {
	log.Fatal(err)
}

rd := bucket.NewReader(ctx, "path/to/object.txt")
if err != nil {
	log.Fatal(err)
}

if _, err := io.Copy(os.Stdout, rd); err != nil {
	log.Fatal(err)
}
Output:

type ObjectReaderOption

type ObjectReaderOption func(*ObjectReader)

ObjectReaderOption is an option for the given read operation

func ObjectReaderOptions

func ObjectReaderOptions(opts ...ObjectReaderOption) ObjectReaderOption

ObjectReaderOptions is a collection of ObjectReaderOption's

func WithReaderChunkSize

func WithReaderChunkSize(size int64) ObjectReaderOption

WithReaderChunkSize sets the chunksize for this reader

func WithReaderConcurrency

func WithReaderConcurrency(i int) ObjectReaderOption

WithReaderConcurrency set the concurency amount for this reader

func WithReaderLogger

func WithReaderLogger(logger *slog.Logger) ObjectReaderOption

WithReaderLogger sets the logger for this reader

func WithReaderRetries

func WithReaderRetries(i int) ObjectReaderOption

WithReaderRetries sets the retry count for this reader

func WithReaderS3Options

func WithReaderS3Options(opts ...func(*s3.Options)) ObjectReaderOption

WithReaderS3Options adds s3 options to the reader opperations

type ObjectWriter

type ObjectWriter struct {
	// contains filtered or unexported fields
}

ObjectWriter is an io.WriteCloser implementation for an s3 Object

func (*ObjectWriter) Close

func (w *ObjectWriter) Close() error

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 (*ObjectWriter) Write

func (w *ObjectWriter) Write(p []byte) (int, error)

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 := OpenBucket(ctx, "bucket-name",
	WithBucketCredentials("access-key", "access-secret"),
	WithBucketCreateIfNotExists(),
)
if err != nil {
	log.Fatal(err)
}

wr := bucket.NewWriter(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)
}
Output:

type ObjectWriterOption

type ObjectWriterOption func(*ObjectWriter)

ObjectWriterOption is an option for the given write operation

func ObjectWriterOptions

func ObjectWriterOptions(opts ...ObjectWriterOption) ObjectWriterOption

ObjectWriterOptions is a collection of ObjectWriterOption's

func WithWriteRetries

func WithWriteRetries(i int) ObjectWriterOption

WithWriterRetries sets the retry count for this writer

func WithWriterACL added in v2.2.1

func WithWriterACL(acl types.ObjectCannedACL) ObjectWriterOption

Set ACL after giving the input

func WithWriterChunkSize

func WithWriterChunkSize(size int64) ObjectWriterOption

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)) ObjectWriterOption

WithWriterClientOptions adds s3 client options to the writer opperations

func WithWriterConcurrency

func WithWriterConcurrency(i int) ObjectWriterOption

WithWriterConcurrency sets the concurrency amount for this writer.

func WithWriterLogger

func WithWriterLogger(logger *slog.Logger) ObjectWriterOption

WithWriterLogger adds a logger for this writer.

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.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL