storage

package module
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Nov 4, 2020 License: MIT Imports: 6 Imported by: 0

README

Go-storage

This package provides abstractions to manage file storage in a number of providers.

Typically called from a server, db handler or http handler.

Example usage

Providers

See individual provider directories for the different parameters each can accept.

Usage

More examples in examples dir.

package main

import (
	"fmt"
    "strings"

	"github.com/djangulo/go-storage"
	_ "github.com/djangulo/go-storage/providers/aws-s3"
)

func main() {
	// upload to s3 bucket "my-bucket", under "my-prefix"
	// This assumes you've configured your credentials in ~/.aws/credentials
	drv, err := storage.Open("awss3://my-bucket/my-prefix?&accept=.txt")
	if err != nil {
		panic(err)
    }

    // file will be saved to .../my-prefix/my-file.txt
    url, err := drv.AddFile(strings.NewReader("my file contents"), "my-file.txt")
	// handle err
    fmt.Println(url)
    // Output: https://my-bucket.s3.us-east-1.amazonaws.com/my-prefix/my-file.txt
    err = drv.RemoveFile("my-file.txt")
    // handle err
}

Roadmap

Documentation

Overview

Package storage implements utilities to add/remove files from a filesystem. Typically used from a server or http.Handler.

Index

Constants

This section is empty.

Variables

View Source
var (
	// ErrAlreadyExists file already exists.
	ErrAlreadyExists = errors.New("file already exists")
	// ErrInvalidExtension invalid extension.
	ErrInvalidExtension = errors.New("invalid extension")
)

Functions

func Register

func Register(name string, driver Driver)

Register registers a driver on the package. Typically called from driver implementations.

func ResolveContentType

func ResolveContentType(path string) string

ResolveContentType resolves the content-type based on the extension of path.

Types

type DangerDriver

type DangerDriver interface {
	// Cleans out the container (bucket, space, etc.).
	EmptyContainer() error
	// DeleteContaineer empties the container, and deletes it.
	DeleteContainer() error
}

DangerDriver interface to be implemented by storage drivers. Most providers in this package implement this interface, but it's delivered as a separate interface (hidden behind a type conversion) due to the permanent nature of these methods.

type Driver

type Driver interface {
	// Open opens a connection and returns a Driver. Each implementation
	// should explain how to connect to it through its urlString.
	Open(url string) (Driver, error)
	Close() error
	// Accepts determines if this storage driver will allow ext.
	Accepts(ext string) bool
	// Path returns the root path of the storage driver.
	Path() string
	// NormalizePath returns the passed entries with the necessary prefix
	// e.g. driver.NormalizePath("path/to/file") == "/assets/path/to/file"
	NormalizePath(entries ...string) string
	// AddFile saves the contents of r to path.
	AddFile(r io.Reader, path string) (string, error)
	// GetFile returns an io.ReadCloser with the contents of the file.
	GetFile(path string) (io.ReadCloser, error)
	// RemoveFile removes the file on path from the driver (if found).
	RemoveFile(path string) error
}

Driver interface to be implemented by storage drivers.

func Open

func Open(urlString string) (Driver, error)

Open checks for a registered driver, and calls the underlying driver Open method.

Directories

Path Synopsis
examples
internal
util
Package awsabs has dependency injected abstractions for AWS-S3 services.
Package awsabs has dependency injected abstractions for AWS-S3 services.
providers
fs
Package fs implements a storage.Driver for a local filesystem.
Package fs implements a storage.Driver for a local filesystem.

Jump to

Keyboard shortcuts

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