gostorages

A unified interface to manipulate storage engine (file system, s3, etc.) for Go

  • 所有者: ulule/gostorages
  • 平台:
  • 许可证: MIT License
  • 分类:
  • 主题:
  • 喜欢:
    0
      比较:

Github星跟踪图

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.

主要指标

概览
名称与所有者ulule/gostorages
主编程语言Go
编程语言Go (语言数: 1)
平台
许可证MIT License
所有者活动
创建于2014-12-06 16:58:18
推送于2025-04-16 23:18:27
最后一次提交2025-03-13 08:37:27
发布数7
最新版本名称v0.2.5 (发布于 )
第一版名称v0.1.0 (发布于 )
用户参与
星数69
关注者数12
派生数20
提交数73
已启用问题?
问题数10
打开的问题数4
拉请求数17
打开的拉请求数2
关闭的拉请求数3
项目设置
已启用Wiki?
已存档?
是复刻?
已锁定?
是镜像?
是私有?