...

Package storage

import "github.com/Shopify/go-storage"
Overview
Index
Subdirectories

Overview ▾

Package storage provides types and functionality for abstracting storage systems (local, in memory, Google Cloud storage) into a common interface.

Index ▾

Constants
Variables
func Exists(ctx context.Context, fs FS, path string) bool
func IsNotExist(err error) bool
func List(ctx context.Context, w Walker, path string) ([]string, error)
func Read(ctx context.Context, fs FS, path string, options *ReaderOptions) ([]byte, error)
func WalkN(ctx context.Context, w Walker, path string, n int, fn WalkFn) error
func Write(ctx context.Context, fs FS, path string, data []byte, options *WriterOptions) error
type Attributes
type CacheOptions
type FS
    func NewCacheWrapper(src, cache FS, options *CacheOptions) FS
    func NewCloudStorageFS(bucket string, credentials *google.Credentials) FS
    func NewHashWrapper(h hash.Hash, fs FS, gs GetSetter) FS
    func NewLocalFS(path string) FS
    func NewLoggerWrapper(fs FS, name string, l Logger) FS
    func NewMemoryFS() FS
    func NewPrefixWrapper(fs FS, prefix string) FS
    func NewSlowWrapper(fs FS, readDelay time.Duration, writeDelay time.Duration) FS
    func NewStatsWrapper(fs FS, name string) FS
    func NewTimeoutWrapper(fs FS, read time.Duration, write time.Duration) FS
type File
type GetSetter
type Logger
type MockFS
    func NewMockFS() *MockFS
    func (m *MockFS) Attributes(ctx context.Context, path string, options *ReaderOptions) (*Attributes, error)
    func (m *MockFS) Create(ctx context.Context, path string, options *WriterOptions) (io.WriteCloser, error)
    func (m *MockFS) Delete(ctx context.Context, path string) error
    func (m *MockFS) Open(ctx context.Context, path string, options *ReaderOptions) (*File, error)
    func (m *MockFS) URL(ctx context.Context, path string, options *SignedURLOptions) (string, error)
    func (m *MockFS) Walk(ctx context.Context, path string, fn WalkFn) error
type ReaderOptions
type Scope
    func ResolveCloudStorageScope(scope Scope) Scope
    func (s Scope) Has(s2 Scope) bool
    func (s Scope) String() string
type SignedURLOptions
type WalkFn
type Walker
type WriterOptions

Package files

cache_wrapper.go cloudstorage_fs.go errors.go fs.go hash_wrapper.go local_fs.go logger_wrapper.go memory_fs.go mock_fs.go prefix_wrapper.go scope.go slow_wrapper.go stats_wrapper.go timeout_wrapper.go utils.go walk.go

Constants

DefaultSignedURLExpiry is the default duration for SignedURLOptions.Expiry.

const (
    DefaultSignedURLExpiry = 1 * time.Hour
    DefaultSignedURLMethod = "GET"
)
const (
    StatOpenTotal    = "open.total"
    StatOpenErrors   = "open.errors"
    StatAttrsTotal   = "attrs.total"
    StatAttrsErrors  = "attrs.errors"
    StatCreateTotal  = "create.total"
    StatCreateErrors = "create.errors"
    StatDeleteTotal  = "delete.total"
    StatDeleteErrors = "delete.errors"
    StatURLTotal     = "url.total"
    StatURLErrors    = "url.errors"
)

DefaultLocalCreatePathMode is the default os.FileMode used when creating directories during a localFS.Create call.

const DefaultLocalCreatePathMode = os.FileMode(0o755)

Variables

var ErrNotImplemented = errors.New("not implemented")

LocalCreatePathMode is the os.FileMode used when creating directories via localFS.Create

var LocalCreatePathMode = DefaultLocalCreatePathMode

func Exists

func Exists(ctx context.Context, fs FS, path string) bool

func IsNotExist

func IsNotExist(err error) bool

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

func List

func List(ctx context.Context, w Walker, path string) ([]string, error)

List runs the Walker on the given path and returns the list of visited paths.

func Read

func Read(ctx context.Context, fs FS, path string, options *ReaderOptions) ([]byte, error)

func WalkN

func WalkN(ctx context.Context, w Walker, path string, n int, fn WalkFn) error

WalkN creates n workers which accept paths from the Walker. If a WalkFn returns non-nil error we wait for other running WalkFns to finish before returning.

func Write

func Write(ctx context.Context, fs FS, path string, data []byte, options *WriterOptions) error

type Attributes

Attributes represents the metadata of a File Inspired from gocloud.dev/blob.Attributes

type Attributes struct {
    // ContentType is the MIME type of the blob object. It will not be empty.
    ContentType string
    // ContentEncoding specifies the encoding used for the blob's content, if any.
    ContentEncoding string
    // Metadata holds key/value pairs associated with the blob.
    // Keys are guaranteed to be in lowercase, even if the backend provider
    // has case-sensitive keys (although note that Metadata written via
    // this package will always be lowercased). If there are duplicate
    // case-insensitive keys (e.g., "foo" and "FOO"), only one value
    // will be kept, and it is undefined which one.
    Metadata map[string]string
    // ModTime is the time the blob object was last modified.
    ModTime time.Time
    // CreationTime is the time the blob object was created.
    CreationTime time.Time
    // Size is the size of the object in bytes.
    Size int64
}

type CacheOptions

type CacheOptions struct {
    // MaxAge is the maximum time allowed since the underlying File's ModTime
    // This means that if the cache is older than MaxAge, the Cache will fetch from the src again.
    // If the expired File is still present on the src (i.e. not updated), it will be ignored.
    MaxAge time.Duration

    // DefaultExpired makes the cache treat a File as expired if its CreationTime cannot be checked.
    // By default, it is false, which means the cache will treat zero-CreationTime files as valid.
    // Only useful if MaxAge is set.
    DefaultExpired bool

    // NoData disables caching of the contents of the entries, it only stores the metadata.
    NoData bool
}

type FS

FS is an interface which defines a virtual filesystem.

type FS interface {
    Walker

    // Open opens an existing file at path in the filesystem.  Callers must close the
    // File when done to release all underlying resources.
    Open(ctx context.Context, path string, options *ReaderOptions) (*File, error)

    // Attributes returns attributes about a path
    Attributes(ctx context.Context, path string, options *ReaderOptions) (*Attributes, error)

    // Create makes a new file at path in the filesystem.  Callers must close the
    // returned WriteCloser and check the error to be sure that the file
    // was successfully written.
    Create(ctx context.Context, path string, options *WriterOptions) (io.WriteCloser, error)

    // Delete removes a path from the filesystem.
    Delete(ctx context.Context, path string) error

    // URL resolves a path to an addressable URL
    URL(ctx context.Context, path string, options *SignedURLOptions) (string, error)
}

func NewCacheWrapper

func NewCacheWrapper(src, cache FS, options *CacheOptions) FS

NewCacheWrapper creates an FS implementation which caches files opened from src into cache.

func NewCloudStorageFS

func NewCloudStorageFS(bucket string, credentials *google.Credentials) FS

NewCloudStorageFS creates a Google Cloud Storage FS credentials can be nil to use the default GOOGLE_APPLICATION_CREDENTIALS

func NewHashWrapper

func NewHashWrapper(h hash.Hash, fs FS, gs GetSetter) FS

NewHashWrapper creates a content addressable filesystem using hash.Hash to sum the content and store it using that name.

func NewLocalFS

func NewLocalFS(path string) FS

func NewLoggerWrapper

func NewLoggerWrapper(fs FS, name string, l Logger) FS

NewLoggerWrapper creates a new FS which logs all calls to FS.

func NewMemoryFS

func NewMemoryFS() FS

NewMemoryFS creates a a basic in-memory implementation of FS.

func NewPrefixWrapper

func NewPrefixWrapper(fs FS, prefix string) FS

NewPrefixWrapper creates a FS which wraps fs and prefixes all paths with prefix.

func NewSlowWrapper

func NewSlowWrapper(fs FS, readDelay time.Duration, writeDelay time.Duration) FS

NewSlowWrapper creates an artificially slow FS. Probably only useful for testing.

func NewStatsWrapper

func NewStatsWrapper(fs FS, name string) FS

NewStatsWrapper creates an FS which records accesses for an FS. To retrieve the stats: stats := expvar.Get(name).(*expvar.Map) total := stats.Get("open.total").(*expvar.Int).Value() // int64

func NewTimeoutWrapper

func NewTimeoutWrapper(fs FS, read time.Duration, write time.Duration) FS

NewTimeoutWrapper creates a FS which wraps fs and adds a timeout to most operations: read: Open, Attributes, URL write: Create, Delete

Note that the Open and Create methods are only for resolving the object, NOT actually reading or writing the contents. These operations should be fairly quick, on the same order as Attribute and Delete, respectively.

This depends on the underlying implementation to honour context's errors. It is at least supported on the CloudStorageFS.

Walk is not covered, since its duration is highly unpredictable.

type File

File contains the metadata required to define a file (for reading).

type File struct {
    io.ReadCloser // Underlying data.
    Attributes
}

type GetSetter

GetSetter implements a key-value store which is concurrency safe (can be used in multiple go-routines concurrently).

type GetSetter interface {
    Get(key string) (string, error)
    Set(key string, value string) error
    Delete(key string) error
}

type Logger

Logger can be a *log.Logger

type Logger interface {
    Print(v ...interface{})
}

type MockFS

type MockFS struct {
    mock.Mock
}

func NewMockFS

func NewMockFS() *MockFS

NewMockFS creates an FS where each method can be mocked. To be used in tests.

func (*MockFS) Attributes

func (m *MockFS) Attributes(ctx context.Context, path string, options *ReaderOptions) (*Attributes, error)

func (*MockFS) Create

func (m *MockFS) Create(ctx context.Context, path string, options *WriterOptions) (io.WriteCloser, error)

func (*MockFS) Delete

func (m *MockFS) Delete(ctx context.Context, path string) error

func (*MockFS) Open

func (m *MockFS) Open(ctx context.Context, path string, options *ReaderOptions) (*File, error)

func (*MockFS) URL

func (m *MockFS) URL(ctx context.Context, path string, options *SignedURLOptions) (string, error)

func (*MockFS) Walk

func (m *MockFS) Walk(ctx context.Context, path string, fn WalkFn) error

type ReaderOptions

ReaderOptions are used to modify the behaviour of read operations. Inspired from gocloud.dev/blob.ReaderOptions It is provided for future extensibility.

type ReaderOptions struct {
    // ReadCompressed controls whether the file must be uncompressed based on Content-Encoding.
    // Only respected by Google Cloud Storage: https://cloud.google.com/storage/docs/transcoding
    // Common pitfall: https://github.com/googleapis/google-cloud-go/issues/1743
    ReadCompressed bool
}

type Scope

type Scope int
const (
    ScopeRead Scope = 1 << iota
    ScopeWrite
    ScopeDelete
    ScopeSignURL

    ScopeRW  = ScopeRead | ScopeWrite
    ScopeRWD = ScopeRW | ScopeDelete
)

func ResolveCloudStorageScope

func ResolveCloudStorageScope(scope Scope) Scope

func (Scope) Has

func (s Scope) Has(s2 Scope) bool

func (Scope) String

func (s Scope) String() string

type SignedURLOptions

SignedURLOptions are used to modify the behaviour of write operations. Inspired from gocloud.dev/blob.SignedURLOptions Not all options are supported by all FS

type SignedURLOptions struct {
    // Expiry sets how long the returned URL is valid for.
    // Defaults to DefaultSignedURLExpiry.
    Expiry time.Duration
    // Method is the HTTP method that can be used on the URL; one of "GET", "PUT",
    // or "DELETE". Defaults to "GET".
    Method string
}

type WalkFn

WalkFn is a function type which is passed to Walk.

type WalkFn func(path string) error

type Walker

Walker is an interface which defines the Walk method.

type Walker interface {
    // Walk traverses a path listing by prefix, calling fn with each path.
    Walk(ctx context.Context, path string, fn WalkFn) error
}

type WriterOptions

WriterOptions are used to modify the behaviour of write operations. Inspired from gocloud.dev/blob.WriterOptions Not all options are supported by all FS

type WriterOptions struct {
    Attributes Attributes

    // BufferSize changes the default size in bytes of the chunks that
    // Writer will upload in a single request; larger blobs will be split into
    // multiple requests.
    //
    // This option may be ignored by some drivers.
    //
    // If 0, the driver will choose a reasonable default.
    //
    // If the Writer is used to do many small writes concurrently, using a
    // smaller BufferSize may reduce memory usage.
    BufferSize int
}

Subdirectories

Name Synopsis
..