storage

package module
v0.3.3 Latest Latest
Warning

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

Go to latest
Published: Aug 2, 2026 License: MIT Imports: 16 Imported by: ? modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

README

GitHub go.mod Go version GitHub Actions Workflow Status

storage

This repository contains source code for file storage, supporting local file storage and cloud remote ones.

Install module
go get github.com/rovergulf/storage
Example
package main

import (
	"context"
	"log"
	
	"github.com/rovergulf/storage"
)

func main() {
    ctx := context.Background()

    storagePath := "/temp/my-storage"

    fs, err := storage.NewStorage(
        storage.WithBackends(storage.LocalBackends),
        storage.WithPrefix(storagePath),
    )
    if err != nil {
        log.Fatal(err)	
    }

    fileData := []byte(`{"example": "hello world"}`)
    fileName := "example.json"
	
    // check if file key already exists 
    if exists, _ := fs.Exists(ctx, fileName); exists {
        log.Printf("'%s' already exists", fileName)
        return
    }
	
	// upload data
    uploadErr := fs.Put(ctx, fileName, fileData)
    if uploadErr != nil {
        log.Fatalf("Unable to upload to '%s': %s", fileName, err)
        // handle uploadErr
    }
}
OpenTelemetry support

This package supports OpenTelemetry tracer spans.
Visit opentelemetry-go examples to install tracer provider and provide tracer as storage storage.WithTracer option

package main

import (
	...
	
	"go.opentelemetry.io/otel"
	
	"github.com/rovergulf/storage"
)

func main() {
	tracer := otel.Tracer("go-storage")
	
	fs, err := storage.NewStorage(
		...,
		storage.WithTracer(tracer),
	)
	if err != nil {
		// handle err	
	}

}

Documentation

Index

Constants

View Source
const (
	DigitalOceanRegion = `us-east-1`
)

Variables

View Source
var (
	ErrNoS3ConfigProvided       = errors.New("no S3 config provided")
	ErrUnsupportedBackends      = errors.New("unsupported backends")
	ErrLocalStorageUnsafePrefix = errors.New("path prefix must contain at least two segments")
)
View Source
var ErrNotExists = errors.New("file not exists")

Functions

This section is empty.

Types

type Backends added in v0.2.0

type Backends string
const (
	LocalBackends Backends = "file"
	S3Backends    Backends = "s3"
)

func (Backends) String added in v0.2.0

func (s Backends) String() string

type FileStorage

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

func NewFileStorage

func NewFileStorage(dir string) *FileStorage

func (*FileStorage) Delete

func (s *FileStorage) Delete(ctx context.Context, key string) error

func (*FileStorage) Exists

func (s *FileStorage) Exists(ctx context.Context, key string) (bool, error)

func (*FileStorage) Get

func (s *FileStorage) Get(ctx context.Context, key string) ([]byte, error)

func (*FileStorage) GetMultiple

func (s *FileStorage) GetMultiple(ctx context.Context, filePaths []string) ([]Object, error)

func (*FileStorage) List

func (s *FileStorage) List(ctx context.Context, prefix string) ([]Object, error)

func (*FileStorage) Purge

func (s *FileStorage) Purge(ctx context.Context) error

func (*FileStorage) Put

func (s *FileStorage) Put(ctx context.Context, key string, data []byte) error

type Object

type Object struct {
	Key  string `json:"key"`
	Size int64  `json:"size"`
	Data []byte `json:"data"`
}

type Option added in v0.2.0

type Option func(o *Options)

func WithBackends added in v0.2.0

func WithBackends(backends Backends) Option

func WithPrefix added in v0.2.0

func WithPrefix(prefix string) Option

func WithS3Config added in v0.2.0

func WithS3Config(s3opts *S3Options) Option

func WithTracer added in v0.2.0

func WithTracer(tracer trace.Tracer) Option

type Options added in v0.2.0

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

type S3Options

type S3Options struct {
	Endpoint   string `json:"endpoint"`
	Region     string `json:"region"`
	Key        string `json:"key"`
	Secret     string `json:"secret"`
	Bucket     string `json:"bucket"`
	PathPrefix string `json:"path_prefix"`
}

func (*S3Options) Validate

func (opts *S3Options) Validate() error

type S3Storage

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

func NewS3Storage

func NewS3Storage(opts *S3Options) (*S3Storage, error)

func (*S3Storage) Delete

func (s *S3Storage) Delete(ctx context.Context, key string) error

func (*S3Storage) Exists

func (s *S3Storage) Exists(ctx context.Context, key string) (bool, error)

func (*S3Storage) Get

func (s *S3Storage) Get(ctx context.Context, key string) ([]byte, error)

func (*S3Storage) GetMultiple added in v0.2.0

func (s *S3Storage) GetMultiple(ctx context.Context, keys []string) ([]Object, error)

func (*S3Storage) List

func (s *S3Storage) List(ctx context.Context, prefix string) ([]Object, error)

func (*S3Storage) Purge

func (s *S3Storage) Purge(ctx context.Context) error

func (*S3Storage) Put

func (s *S3Storage) Put(ctx context.Context, key string, data []byte) error

type Storage

type Storage interface {
	Put(ctx context.Context, key string, data []byte) error
	Get(ctx context.Context, key string) ([]byte, error)
	GetMultiple(ctx context.Context, keys []string) ([]Object, error)
	Exists(ctx context.Context, key string) (bool, error)
	Delete(ctx context.Context, key string) error
	List(ctx context.Context, prefix string) ([]Object, error)
	Purge(ctx context.Context) error
}

Storage represents main interface for file storage as key/value database

func NewStorage added in v0.2.0

func NewStorage(options ...Option) (Storage, error)

type TracingStorage added in v0.2.0

type TracingStorage interface {
	Storage

	GetMultiple(ctx context.Context, keys []string) ([]Object, error)
}

func NewTracingStorage added in v0.2.0

func NewTracingStorage(storage Storage, tracer trace.Tracer) TracingStorage

Jump to

Keyboard shortcuts

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