godrive

package module
v0.6.0 Latest Latest
Warning

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

Go to latest
Published: Aug 25, 2023 License: MIT Imports: 9 Imported by: 1

README

godrive - Cloud Storage Library

GoDoc Version

This library 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.

Install

go get github.com/bounoable/godrive

Usage

Read the GoDocs (pkg.go.dev)

Autowire from YAML configuration

  1. Create configuration
default: main # Default disk to use

disks:
  main: # Specify a disk name
    provider: s3 # The storage provider
    config: # Configuration for the storage provider
      region: us-east-2
      bucket: images
      accessKeyId: ${AWS_ACCESS_KEY_ID} # Use environment variable
      secretAccessKey: ${AWS_SECRET_ACCESS_KEY}
      public: true
  
  videos:
    provider: gcs
    config:
      serviceAccount: /path/to/service/account.json
      bucket: uploads
      public: true
  1. Create manager
package main

import (
  "github.com/bounoable/godrive"
  "github.com/bounoable/godrive/s3"
  "github.com/bounoable/godrive/gcs"
)

func main() {
    // Initialize autowire & register providers
    aw := godrive.NewAutoWire(
      s3.Register,
      gcs.Register,
    )

    // Load disk configuration
    err := aw.Load("/path/to/config.yml")
    if err != nil {
      panic(err)
    }

    // Create disk manager
    manager, err := aw.NewManager(context.Background())
    if err != nil {
      panic(err)
    }

    // Get disk by name and use it
    disk, _ := manager.Disk("videos")
    err = disk.Put(context.Background(), "path/on/disk.txt", []byte("Hi."))
    content, err := disk.Get(context.Background(), "path/on/disk.txt")
    err = disk.Delete(context.Background(), "path/on/disk.txt")

    // or use the default disk
    err = manager.Put(context.Background(), "path/on/disk.txt", []byte("Hi."))
    content, err := manager.Get(context.Background(), "path/on/disk.txt")
    err = manager.Delete(context.Background(), "path/on/disk.txt")
}

Use without autowire

package main

import (
  "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/s3"
  "github.com/bounoable/godrive/gcs"
  "google.golang.org/api/option"
)

func main() {
    // Create manager and add disks manually
    manager := godrive.New()

    s3client := awss3.New(aws.Config{})
    manager.Configure("main", s3.NewDisk(s3client, "REGION", "BUCKET", s3.Public(true)))

    gcsclient, err := storage.NewClient(context.Backgroud())
    manager.Configure("videos", gcs.NewDisk(gcsclient, "BUCKET", gcs.Public(true)))

    // Get disk by name and use it
    disk, _ := manager.Disk("videos")
    err = disk.Put(context.Background(), "path/on/disk.txt", []byte("Hi."))
    content, err := disk.Get(context.Background(), "path/on/disk.txt")
    err = disk.Delete(context.Background(), "path/on/disk.txt")

    // or use the default disk
    err = manager.Put(context.Background(), "path/on/disk.txt", []byte("Hi."))
    content, err := manager.Get(context.Background(), "path/on/disk.txt")
    err = manager.Delete(context.Background(), "path/on/disk.txt")
}

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

Examples

Constants

This section is empty.

Variables

View Source
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 Default

func Default() ConfigureOption

Default makes the Disk the default Disk.

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

type DiskCreatorConfig struct {
	Provider string
	Config   map[string]interface{}
}

DiskCreatorConfig is the configuration for the creation of a single storage disk.

type DiskCreatorFunc added in v0.3.0

type DiskCreatorFunc func(context.Context, map[string]interface{}) (Disk, error)

DiskCreatorFunc creates storage disks.

func (DiskCreatorFunc) CreateDisk added in v0.3.0

func (fn DiskCreatorFunc) CreateDisk(ctx context.Context, cfg map[string]interface{}) (Disk, error)

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

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

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

func (m *Manager) Disk(name string) (Disk, error)

Disk returns the Disk with the configured name. If no Disk with the name is configured, it returns an UnconfiguredDiskError.

func (*Manager) Get

func (m *Manager) Get(ctx context.Context, path string) ([]byte, error)

Get retrieves the file at the given path. If no default Disk is set, it returns ErrNoDefaultDisk.

func (*Manager) GetURL

func (m *Manager) GetURL(ctx context.Context, path string) (string, error)

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

func (m *Manager) Put(ctx context.Context, path string, b []byte) error

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

func (m *Manager) RemoveDisk(name string)

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

Directories

Path Synopsis
Package gcs provides the Google Cloud Storage disk implementation.
Package gcs provides the Google Cloud Storage disk implementation.
Package s3 provides the Amazon S3 disk implementation.
Package s3 provides the Amazon S3 disk implementation.

Jump to

Keyboard shortcuts

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