Documentation ¶
Overview ¶
Package godrive provides a uniform access to multiple storage providers and a central configuration for all storage disks. It autowires the storage disks from a single YAML configuration file.
Index ¶
- Variables
- type AutoWireConfig
- func (cfg *AutoWireConfig) Configure(diskname, provider string, config map[string]interface{})
- func (cfg *AutoWireConfig) Load(path string) error
- func (cfg *AutoWireConfig) LoadYAML(path string) error
- func (cfg *AutoWireConfig) LoadYAMLReader(r io.Reader) error
- func (cfg *AutoWireConfig) NewManager(ctx context.Context) (*Manager, error)
- func (cfg *AutoWireConfig) RegisterProvider(name string, creator DiskCreator)
- type AutoWireOption
- type ConfigureOption
- type Disk
- type DiskCreator
- type DiskCreatorConfig
- type DiskCreatorFunc
- type DuplicateDiskConfigError
- type DuplicateNameError
- type InvalidConfigValueError
- type Manager
- func (m *Manager) Configure(name string, disk Disk, options ...ConfigureOption) error
- func (m *Manager) Delete(ctx context.Context, path string) error
- func (m *Manager) Disk(name string) (Disk, error)
- func (m *Manager) Get(ctx context.Context, path string) ([]byte, error)
- func (m *Manager) GetURL(ctx context.Context, path string) (string, error)
- func (m *Manager) Put(ctx context.Context, path string, b []byte) error
- func (m *Manager) RemoveDisk(name string)
- type URLProvider
- type UnconfiguredDiskError
- type UnimplementedError
- type UnregisteredProviderError
Examples ¶
Constants ¶
This section is empty.
Variables ¶
var ( // ErrNoDefaultDisk is returned on Disk operations when no default Disk is set. ErrNoDefaultDisk = errors.New("no default disk specified") )
Functions ¶
This section is empty.
Types ¶
type AutoWireConfig ¶ added in v0.3.0
type AutoWireConfig struct { Disks map[string]DiskCreatorConfig Creators map[string]DiskCreator DefaultDiskName string }
AutoWireConfig contains the configuration for the disk autowire.
func NewAutoWire ¶ added in v0.3.0
func NewAutoWire(options ...AutoWireOption) *AutoWireConfig
NewAutoWire returns a new autowire configuration.
Example ¶
package main import ( "context" "github.com/bounoable/godrive" "github.com/bounoable/godrive/gcs" "github.com/bounoable/godrive/s3" ) func main() { aw := godrive.NewAutoWire( s3.Register, // Register Amazon S3 gcs.Register, // Register Google Cloud Storage ) err := aw.Load("/path/to/config.yml") // Load the autowire configuration if err != nil { panic(err) } manager, err := aw.NewManager(context.Background()) // Build the storage manager if err != nil { panic(err) } disk, _ := manager.Disk("videos") // Get disk by name (as defined by the YAML configuration) disk.Put(context.Background(), "path/on/disk.txt", []byte("Hi.")) // Use the disk // or use the default disk (as defined by the YAML configuration) manager.Put(context.Background(), "path/on/disk.txt", []byte("Hi.")) }
Output:
func (*AutoWireConfig) Configure ¶ added in v0.3.0
func (cfg *AutoWireConfig) Configure(diskname, provider string, config map[string]interface{})
Configure adds a disk to the configuration.
func (*AutoWireConfig) Load ¶ added in v0.3.0
func (cfg *AutoWireConfig) Load(path string) error
Load loads the disk configuration from a file. It checks against provided file extensions and returns an error if the filetype is unsupported.
func (*AutoWireConfig) LoadYAML ¶ added in v0.3.0
func (cfg *AutoWireConfig) LoadYAML(path string) error
LoadYAML loads the disk configuration from a YAML file.
func (*AutoWireConfig) LoadYAMLReader ¶ added in v0.3.0
func (cfg *AutoWireConfig) LoadYAMLReader(r io.Reader) error
LoadYAMLReader loads the disk configuration from the YAML in r.
func (*AutoWireConfig) NewManager ¶ added in v0.3.0
func (cfg *AutoWireConfig) NewManager(ctx context.Context) (*Manager, error)
NewManager creates a new Manager with the initialized storage disks.
func (*AutoWireConfig) RegisterProvider ¶ added in v0.3.0
func (cfg *AutoWireConfig) RegisterProvider(name string, creator DiskCreator)
RegisterProvider registers a storage disk creator.
type AutoWireOption ¶ added in v0.4.0
type AutoWireOption func(*AutoWireConfig)
AutoWireOption is an autowire option.
type ConfigureOption ¶
type ConfigureOption func(*configureConfig)
ConfigureOption is a Disk configuration option.
func Replace ¶
func Replace() ConfigureOption
Replace will replace the previously configured Disk with the same name.
type Disk ¶
type Disk interface { // Put writes b to the file at the given path. Put(ctx context.Context, path string, b []byte) error // Get retrieves the file at the given path. Get(ctx context.Context, path string) ([]byte, error) // Delete deletes the file at the given path. Delete(ctx context.Context, path string) error }
Disk provides the base cloud storage functions.
type DiskCreator ¶ added in v0.3.0
type DiskCreator interface {
CreateDisk(ctx context.Context, cfg map[string]interface{}) (Disk, error)
}
DiskCreator creates storage disks.
type DiskCreatorConfig ¶ added in v0.3.0
DiskCreatorConfig is the configuration for the creation of a single storage disk.
type DiskCreatorFunc ¶ added in v0.3.0
DiskCreatorFunc creates storage disks.
func (DiskCreatorFunc) CreateDisk ¶ added in v0.3.0
CreateDisk creates a storage disk.
type DuplicateDiskConfigError ¶ added in v0.3.0
type DuplicateDiskConfigError struct {
DiskName string
}
DuplicateDiskConfigError means the YAML configuration contains multiple configurations for a disk name.
func (DuplicateDiskConfigError) Error ¶ added in v0.3.0
func (err DuplicateDiskConfigError) Error() string
type DuplicateNameError ¶
type DuplicateNameError struct {
Name string
}
DuplicateNameError is returned when a Disk is added to a Manager with a name that was already used.
func (DuplicateNameError) Error ¶
func (err DuplicateNameError) Error() string
type InvalidConfigValueError ¶ added in v0.3.0
type InvalidConfigValueError struct { DiskName string ConfigKey string Expected interface{} Provided interface{} }
InvalidConfigValueError means a configuration value for a disk has a wrong type.
func (InvalidConfigValueError) Error ¶ added in v0.3.0
func (err InvalidConfigValueError) Error() string
type Manager ¶
type Manager struct {
// contains filtered or unexported fields
}
Manager is a container for multiple Disks and is itself also a Disk. Disk operations are delegated to the configured default Disk. Manager is thread-safe (but the Disk implementations may not).
func New ¶
func New() *Manager
New returns a new disk manager. The disk manager is a container for multiple storage disks and also implements the Disk interface, so it can be directly used to access the configured default disk.
Normally you don't instantiate the manager with New() but through the AutoWire config.
Example ¶
package main import ( "context" "cloud.google.com/go/storage" "github.com/aws/aws-sdk-go-v2/aws" awss3 "github.com/aws/aws-sdk-go-v2/service/s3" "github.com/bounoable/godrive" "github.com/bounoable/godrive/gcs" "github.com/bounoable/godrive/s3" "google.golang.org/api/option" ) func main() { // Manually configure disks manager := godrive.New() s3client := awss3.NewFromConfig(aws.Config{}) manager.Configure("main", s3.NewDisk(s3client, "REGION", "BUCKET", s3.Public(true)), godrive.Default()) // make it the default disk gcsclient, _ := storage.NewClient(context.Background(), option.WithCredentialsFile("/path/to/service_account.json")) manager.Configure("videos", gcs.NewDisk(gcsclient, "BUCKET", gcs.Public(true))) disk, _ := manager.Disk("videos") // Get disk by name disk.Put(context.Background(), "path/on/disk.txt", []byte("Hi.")) // Use the disk // or use the manager as a disk (uses the "main" disk) manager.Put(context.Background(), "path/on/disk.txt", []byte("Hi.")) }
Output:
func (*Manager) Configure ¶
func (m *Manager) Configure(name string, disk Disk, options ...ConfigureOption) error
Configure adds a Disk to the Manager. If the name is already in use, it returns a DuplicateNameError unless the Replace option is used. The first Disk will automatically be made the default Disk, even if the Default option is not used.
func (*Manager) Delete ¶
Delete deletes the file at the given path. If no default Disk is set, it returns ErrNoDefaultDisk.
func (*Manager) Disk ¶ added in v0.2.0
Disk returns the Disk with the configured name. If no Disk with the name is configured, it returns an UnconfiguredDiskError.
func (*Manager) Get ¶
Get retrieves the file at the given path. If no default Disk is set, it returns ErrNoDefaultDisk.
func (*Manager) GetURL ¶
GetURL returns the public URL for the file at the given path. If no default Disk is set, it returns ErrNoDefaultDisk. If the default Disk does not implement URLProvider, it returns an UnimplementedError.
func (*Manager) Put ¶
Put writes b to the file at the given path on the default Disk. If no default Disk is set, it returns ErrNoDefaultDisk.
func (*Manager) RemoveDisk ¶
RemoveDisk removes the Disk with the configured name from the Manager.
type URLProvider ¶
type URLProvider interface { // GetURL returns the public URL for the file at the given path. GetURL(ctx context.Context, path string) (string, error) }
URLProvider generates public URLs for files.
type UnconfiguredDiskError ¶
type UnconfiguredDiskError struct {
Name string
}
UnconfiguredDiskError is returned when no Disk can be found for a name.
func (UnconfiguredDiskError) Error ¶
func (err UnconfiguredDiskError) Error() string
type UnimplementedError ¶
type UnimplementedError struct { DiskName string Interface interface{} }
UnimplementedError means a Disk does not implement a specific feature.
func (UnimplementedError) Error ¶
func (err UnimplementedError) Error() string
type UnregisteredProviderError ¶ added in v0.3.0
type UnregisteredProviderError struct {
Provider string
}
UnregisteredProviderError means the configuration contains a disk with an unregistered provider.
func (UnregisteredProviderError) Error ¶ added in v0.3.0
func (err UnregisteredProviderError) Error() string