fsys

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

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

Go to latest
Published: Oct 23, 2024 License: MIT Imports: 20 Imported by: 3

README

⚠️ This project is still experimental. Do not use this in production.

Fsys

Fsys is a Go library for dealing with file systems.

Installation

Use the Go package manager to install foobar.

go get -u github.com/lemmego/fsys

Usage

fsys.NewMemoryStorage(...)
fsys.NewLocalStorage(...)
fsys.NewGCSStorage(...)
fsys.NewS3Storage(...)

Contributing

Pull requests are welcome. For major changes, please open an issue first to discuss what you would like to change.

Please make sure to update tests as appropriate.

License

MIT

Documentation

Index

Constants

View Source
const (
	DRIVER_MEMORY = "memory"
	DRIVER_LOCAL  = "local"
	DRIVER_GCS    = "gcs"
	DRIVER_S3     = "s3"
)

Variables

This section is empty.

Functions

This section is empty.

Types

type FS

type FS interface {
	// Driver returns the name of the current driver
	Driver() string

	// Read a file from storage.
	Read(path string) (io.ReadCloser, error)

	// Write a file to storage.
	Write(path string, contents []byte) error

	// Delete a file from storage.
	Delete(path string) error

	// Exists checks if a file exists in storage.
	Exists(path string) (bool, error)

	// Rename a file in storage.
	Rename(oldPath, newPath string) error

	// Copy a file in storage.
	Copy(sourcePath, destinationPath string) error

	// CreateDirectory creates a new directory if doesn't already exist for the given path
	CreateDirectory(path string) error

	// GetUrl gets the URL for a file in storage (optional).
	// This method may not be applicable to all storage systems.
	// For example, local storage may return a file path, while cloud storage may return a URL.
	GetUrl(path string) (string, error)

	// Open opens a file
	Open(path string) (*os.File, error)

	// Upload uploads a file to the implemented driver
	Upload(file multipart.File, header *multipart.FileHeader, dir string) (*os.File, error)
}

FS defines the methods that any storage system must implement.

type File

type File struct {
	Name    string
	Content *bytes.Buffer
}

File represents an in-memory file.

type GCSStorage

type GCSStorage struct {
	// GCS bucket name
	BucketName string

	// GCS client
	Client *storage.Client
}

GCSStorage is an implementation of StorageInterface for Google Cloud Storage.

func NewGCSStorage

func NewGCSStorage(projectID, bucket, serviceAccountKey string) (*GCSStorage, error)

func (*GCSStorage) Copy

func (gcs *GCSStorage) Copy(sourcePath, destPath string) error

func (*GCSStorage) CreateDirectory

func (gcs *GCSStorage) CreateDirectory(path string) error

func (*GCSStorage) Delete

func (gcs *GCSStorage) Delete(path string) error

func (*GCSStorage) Driver

func (gcs *GCSStorage) Driver() string

func (*GCSStorage) Exists

func (gcs *GCSStorage) Exists(path string) (bool, error)

func (*GCSStorage) GetUrl

func (gcs *GCSStorage) GetUrl(path string) (string, error)

func (*GCSStorage) Open

func (gcs *GCSStorage) Open(path string) (*os.File, error)

func (*GCSStorage) Read

func (gcs *GCSStorage) Read(path string) (io.ReadCloser, error)

func (*GCSStorage) Rename

func (gcs *GCSStorage) Rename(oldPath, newPath string) error

func (*GCSStorage) Upload

func (gcs *GCSStorage) Upload(file multipart.File, header *multipart.FileHeader, dir string) (*os.File, error)

func (*GCSStorage) Write

func (gcs *GCSStorage) Write(path string, contents []byte) error

type LocalStorage

type LocalStorage struct {
	// Root directory of the storage.
	RootDirectory string
}

LocalStorage is an implementation of StorageInterface for local file system.

func NewLocalStorage

func NewLocalStorage(basePath string) *LocalStorage

func (*LocalStorage) Copy

func (ls *LocalStorage) Copy(sourcePath, destinationPath string) error

func (*LocalStorage) CreateDirectory

func (ls *LocalStorage) CreateDirectory(path string) error

func (*LocalStorage) Delete

func (ls *LocalStorage) Delete(path string) error

func (*LocalStorage) Driver

func (ls *LocalStorage) Driver() string

func (*LocalStorage) Exists

func (ls *LocalStorage) Exists(path string) (bool, error)

func (*LocalStorage) GetUrl

func (ls *LocalStorage) GetUrl(path string) (string, error)

func (*LocalStorage) Open

func (ls *LocalStorage) Open(path string) (*os.File, error)

func (*LocalStorage) Read

func (ls *LocalStorage) Read(path string) (io.ReadCloser, error)

func (*LocalStorage) Rename

func (ls *LocalStorage) Rename(oldPath, newPath string) error

func (*LocalStorage) Upload

func (ls *LocalStorage) Upload(file multipart.File, header *multipart.FileHeader, dir string) (*os.File, error)

func (*LocalStorage) Write

func (ls *LocalStorage) Write(path string, contents []byte) error

type MemoryStorage

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

MemoryStorage is an in-memory implementation of the FS interface.

func NewMemoryStorage

func NewMemoryStorage() *MemoryStorage

NewMemoryStorage returns a new MemoryStorage instance.

func (*MemoryStorage) Copy

func (fs *MemoryStorage) Copy(sourcePath, destinationPath string) error

Copy copies a file in memory.

func (*MemoryStorage) CreateDirectory

func (fs *MemoryStorage) CreateDirectory(path string) error

CreateDirectory is a no-op for memory storage but can simulate directory creation.

func (*MemoryStorage) Delete

func (fs *MemoryStorage) Delete(path string) error

Delete deletes a file from memory.

func (*MemoryStorage) Driver

func (fs *MemoryStorage) Driver() string

Driver returns the name of the current driver.

func (*MemoryStorage) Exists

func (fs *MemoryStorage) Exists(path string) (bool, error)

Exists checks if a file exists in memory.

func (*MemoryStorage) GetUrl

func (fs *MemoryStorage) GetUrl(path string) (string, error)

GetUrl returns a mock URL for memory-stored files.

func (*MemoryStorage) Open

func (fs *MemoryStorage) Open(path string) (*os.File, error)

Open opens a file (not fully applicable for memory but returns a mock).

func (*MemoryStorage) Read

func (fs *MemoryStorage) Read(path string) (io.ReadCloser, error)

Read reads a file from memory.

func (*MemoryStorage) Rename

func (fs *MemoryStorage) Rename(oldPath, newPath string) error

Rename renames a file in memory.

func (*MemoryStorage) Upload

func (fs *MemoryStorage) Upload(file multipart.File, header *multipart.FileHeader, dir string) (*os.File, error)

Upload simulates file upload by storing the uploaded file's content.

func (*MemoryStorage) Write

func (fs *MemoryStorage) Write(path string, contents []byte) error

Write writes a file to memory.

type S3Storage

type S3Storage struct {
	// S3 bucket name
	BucketName string

	// AWS session
	Session *session.Session

	// AWS S3 client
	S3Client *s3.S3
}

S3Storage is an implementation of StorageInterface for Amazon S3.

func NewS3Storage

func NewS3Storage(bucket, region, accessKey, secretKey string, baseEndpoint string) (*S3Storage, error)

func (*S3Storage) Copy

func (s3s *S3Storage) Copy(sourcePath, destPath string) error

Copy copies the file from the source path to the destination path in S3 storage.

func (*S3Storage) CreateDirectory

func (s3s *S3Storage) CreateDirectory(path string) error

func (*S3Storage) Delete

func (s3s *S3Storage) Delete(path string) error

Delete deletes the file at the specified path from S3 storage.

func (*S3Storage) Driver

func (s3s *S3Storage) Driver() string

func (*S3Storage) Exists

func (s3s *S3Storage) Exists(path string) (bool, error)

Exists checks if the file exists at the specified path in S3 storage.

func (*S3Storage) GetUrl

func (s3s *S3Storage) GetUrl(path string) (string, error)

GetUrl returns the URL of the file at the specified path in S3 storage.

func (*S3Storage) Open

func (s3s *S3Storage) Open(path string) (*os.File, error)

func (*S3Storage) Read

func (s3s *S3Storage) Read(path string) (io.ReadCloser, error)

func (*S3Storage) Rename

func (s3s *S3Storage) Rename(oldPath, newPath string) error

Rename renames the file from the oldPath to the newPath in S3 storage.

func (*S3Storage) Upload

func (s3s *S3Storage) Upload(file multipart.File, header *multipart.FileHeader, dir string) (*os.File, error)

func (*S3Storage) Write

func (s3s *S3Storage) Write(path string, contents []byte) error

Write writes the given contents to the specified path in S3 storage.

Jump to

Keyboard shortcuts

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