gostorages

package module
v0.2.4 Latest Latest
Warning

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

Go to latest
Published: May 12, 2022 License: MIT Imports: 4 Imported by: 3

README

gostorages

A unified interface to manipulate storage backends in Go.

gostorages is used in picfit to allow us switching storage backends.

The following backends are supported:

  • Amazon S3
  • Google Cloud Storage
  • File system

Usage

It offers you a small API to manipulate your files on multiple storages.

// Storage is the storage interface.
type Storage interface {
	Save(ctx context.Context, content io.Reader, path string) error
	Stat(ctx context.Context, path string) (*Stat, error)
	Open(ctx context.Context, path string) (io.ReadCloser, error)
	Delete(ctx context.Context, path string) error
}

// Stat contains metadata about content stored in storage.
type Stat struct {
	ModifiedTime time.Time
	Size         int64
}

// ErrNotExist is a sentinel error returned by the Stat method.
var ErrNotExist = errors.New("does not exist")

If you are migrating from a File system storage to an Amazon S3, you don't need to migrate all your methods anymore!

Be lazy again!

File system

The file system backend requires a root directory.

dir := os.TempDir()
storage := fs.NewStorage(fs.Config{Root: dir})

// Saving a file named test
storage.Save(ctx, strings.NewReader("(╯°□°)╯︵ ┻━┻"), "test")

// Deleting the new file on the storage
storage.Delete(ctx, "test")

Amazon S3

The S3 backend requires AWS credentials, an AWS region and a S3 bucket name.

storage, _ := s3.NewStorage(s3.Config{
    AccessKeyID:     accessKeyID,
    SecretAccessKey: secretAccessKey,
    Region:          region,
    Bucket:          bucket,
})

// Saving a file named test
storage.Save(ctx, strings.NewReader("(>_<)"), "test")

// Deleting the new file on the storage
storage.Delete(ctx, "test")

Roadmap

See issues.

Don't hesitate to send patch or improvements.

Documentation

Index

Constants

This section is empty.

Variables

View Source
var ErrNotExist = errors.New("does not exist")

ErrNotExist is a sentinel error returned by the Open and the Stat methods.

Functions

This section is empty.

Types

type Stat added in v0.2.0

type Stat struct {
	ModifiedTime time.Time
	Size         int64
}

Stat contains metadata about content stored in storage.

type Storage

type Storage interface {
	Save(ctx context.Context, content io.Reader, path string) error
	Stat(ctx context.Context, path string) (*Stat, error)
	Open(ctx context.Context, path string) (io.ReadCloser, error)
	Delete(ctx context.Context, path string) error
}

Storage is the storage interface.

func NewNoop added in v0.2.0

func NewNoop() Storage

NewNoop returns a new no-op storage.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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