storage

package module
v0.0.0-...-e013615 Latest Latest
Warning

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

Go to latest
Published: Feb 22, 2023 License: MIT Imports: 1 Imported by: 0

README

Build status codecov

evolidev/storage

A simple storage wrapper with some basic operations. If you have some basic operations like put, get, delete, copy, move etc. then this package may be something for you. If you need more special operations in a desired storage type then this package may not be for you.

How to use

Setup
s3Config := disk.S3Config{Client: disk.NewMemoryClient()}
localConfig := disk.LocalConfig{}
memoryConfig := disk.MemoryConfig{}

adapters := map[string]fs.Disk{
    "memory": disk.NewMemory(memoryConfig),
    "local":  disk.NewLocal(localConfig),
    "s3":     disk.NewS3(s3Config),
}

// first added adapter is default
storage := storage.New(adapters)

// change default
storage.Default("local")
Writing

To create file you can simple call the storage put method or create a file struct and call its put method. If the file exists the content will be overridden. All necessary directories will be created for you automatically.

// use storage
err := storage.Put("path/to/file.txt", []byte("hello world"), fs.PUBLIC)

// use file
file := fs.NewFile(Storage::Disk("local"), "path/to/file", "test.txt")
err := file.Put([]byte("test"), fs.PUBLIC)

It is possible to append or prepend content.

// use storage
err := storage.Append("path/to/file.txt", []byte("appended content"))
err := storage.Prepend("path/to/file.txt", []byte("prepended content"))

// use file
file := fs.NewFile(Storage::Disk("local"), "path/to/file", "test.txt")
err := file.Append([]byte("appended content"))
err := file.Prepend([]byte("prepended content"))

The copy/move commands can be used to copy/move files to a new location on the same disk

// use storage
err := storage.Move("source/file.txt", "destination/file.txt")
err := storage.Copy("source/file.txt", "destination/file.txt")

// use file
file := fs.NewFile(Storage::Disk("local"), "source/path", "file.txt")
err := file.Move("destination/path")
err := file.Copy("destination/path")

To create a directory use MakeDirectory. Since S3 does not have directories it will be an empty object.

err := storage.MakeDirectory("directory")
Retrieving

Directories will return a slice of fs.Disk interface. It is always the adapter of calling storage. To get all directories including subdirectories use AllDirectories.

// files are a slice of fs.Disk
// the type of the default disk
dirs := storage.Directories("path/to/directroy")
// given you have following structure
// | path
// |- to
// |-- directory
// |--- subdir
// |---- subsubdir
// then you will get a slice only with subdir

dirs := storage.AllDirectories("path/to/directroy")
// based on the example above you will get a slice with subdir and subsubdir

// now dirs will be a slice of *disk.Local  
dirs := storage.Disk("local").Directories("path/to/directroy")

For getting files in a directory use Files. To get all files including files of subdirectories use AllFiles

// files are a slice of *fs.File
files := storage.Files("path/to/directroy")
files := storage.AllFiles("path/to/directroy")
// the result will be as in the example of directories

Cwd (current working directory) will return the path to directory

dirs := storage.AllDirectories("path/to/directroy")
dir := dirs[0]
// will print "path/to/directory/subdir"
fmt.Println(dir.Cwd())

To get the content of file use the Get method

// use storage
content, err := storage.Get("path/to/file.txt")

// if you have a file struct from Files() or AllFiles()
content, err := file.Get()

Use Prefix to get a sub storage of calling storage. The resulting storages of Directories will prefix the storages.

// given you have following structure
// | path
// |- to
// |-- directory
// |--- subdir
// |---- subsubdir
// you can now traverse it like following
s := storage.Prefx("path")
s = s.Prefix("to")
s = s.Prefix("directory")
// will hold a slice with "subdir" as storage
dirs := s.Directories()
s = dirs[0].Prefix("subsubdir")
Attributes

Checking for existence or missing files can be done with Exists and Missing

if(storage.Exists("path/to/file.txt")) {
	fmt.Println("file exists")
}

if(storage.Missing("path/to/file.txt")) {
    fmt.Println("file does not exists")
}

There are Size and LastModified to get desired information. Also Attributes could be used to get the information.

// use helper functions
size := storage.Size("path/to/file.txt")
lastModified := storage.LastModified("path/to/file.txt")

// use attributes
size := storage.Attributes("path/to/file.txt").Size
lastModified := storage.Attributes("path/to/file.txt").LastModified

// if you have a file struct in your hand
// use helper functions
size := file.Size()
lastModified := file.LastModified()

// use attributes
size := storage.Attributes().Size
lastModified := storage.Attributes().LastModified
Deleting

Delete will delete a single file. To delete a directory use DeleteDirectory. DeleteDirectory will remove everything inside!

err := storage.Delete("path/to/file.txt")
// the file also got a delete method
err := file.Delete()

err := storage.DeleteDirectory("path/to/dir")

TODO

  • Visibility of files in S3 adapter
  • Embed adapter
  • Streaming

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Storage

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

func New

func New(disks map[string]fs.Disk) *Storage

func (*Storage) AddDisk

func (s *Storage) AddDisk(name string, disk fs.Disk)

func (*Storage) AllDirectories

func (s *Storage) AllDirectories(dir string) []fs.Disk

func (*Storage) AllFiles

func (s *Storage) AllFiles(dir string) []*fs.File

func (*Storage) Append

func (s *Storage) Append(file string, content []byte) error

func (*Storage) Attributes

func (s *Storage) Attributes(file string) fs.Attributes

func (*Storage) Copy

func (s *Storage) Copy(source string, destination string) error

func (*Storage) Cwd

func (s *Storage) Cwd() string

func (*Storage) Default

func (s *Storage) Default(name string)

func (*Storage) Delete

func (s *Storage) Delete(files ...string) error

func (*Storage) DeleteDirectory

func (s *Storage) DeleteDirectory(dir string) error

func (*Storage) Directories

func (s *Storage) Directories(dir string) []fs.Disk

func (*Storage) Disk

func (s *Storage) Disk(name string) fs.Disk

func (*Storage) Exists

func (s *Storage) Exists(file string) bool

func (*Storage) File

func (s *Storage) File(file string) *fs.File

func (*Storage) Files

func (s *Storage) Files(dir string) []*fs.File

func (*Storage) Get

func (s *Storage) Get(file string) ([]byte, error)

func (*Storage) LastModified

func (s *Storage) LastModified(file string) int64

func (*Storage) MakeDirectory

func (s *Storage) MakeDirectory(dir string, visibility fs.Visibility) error

func (*Storage) Missing

func (s *Storage) Missing(file string) bool

func (*Storage) Move

func (s *Storage) Move(source string, destination string) error

func (*Storage) Path

func (s *Storage) Path(file string) string

func (*Storage) Prefix

func (s *Storage) Prefix(prefix string) fs.Disk

func (*Storage) Prepend

func (s *Storage) Prepend(file string, content []byte) error

func (*Storage) Put

func (s *Storage) Put(file string, content []byte, visibility fs.Visibility) error

func (*Storage) Size

func (s *Storage) Size(file string) int64

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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