storages

package module
v0.1.2 Latest Latest
Warning

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

Go to latest
Published: Oct 7, 2021 License: MIT Imports: 15 Imported by: 0

README

storages

A unified interface to manipulate storage backends in Go.

storages 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")

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type S3Storage

type S3Storage struct {
	*gostorages.BaseStorage
	// contains filtered or unexported fields
}

Storage is a S3 storage.

func NewS3Storage

func NewS3Storage(endpointUrl string) *S3Storage

func (*S3Storage) Delete

func (s *S3Storage) Delete(path string) error

Delete deletes path.

func (*S3Storage) Exists

func (s *S3Storage) Exists(filepath string) bool

Exists checks if the given file is in the bucket

func (*S3Storage) IsNotExist

func (s *S3Storage) IsNotExist(err error) bool

IsNotExist returns a boolean indicating whether the error is known to report that a file or directory does not exist.

func (*S3Storage) ModifiedTime

func (s *S3Storage) ModifiedTime(path string) (time.Time, error)

func (*S3Storage) Open

func (s *S3Storage) Open(filepath string) (gostorages.File, error)

Open returns the file content in a dedicated bucket

func (*S3Storage) Save

func (s *S3Storage) Save(path string, file gostorages.File) error

Save saves a file at the given path in the bucket

func (*S3Storage) Size

func (s *S3Storage) Size(path string) int64

Size returns the size of the given file

Jump to

Keyboard shortcuts

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