Skip to content

tbhaxor/gofsx

Repository files navigation

gofsx

A flexible, extensible filesystem abstraction for Go inspired by PHP's Flysystem.

Features

  • Unified interface for local, S3, and FTP filesystems
  • Easily add your own adapters (e.g., GCS, Azure, custom)
  • Consistent API for file operations: read, write, delete, move, copy, list, etc.
  • Supports advanced features: visibility, checksums, mime type detection, and more
  • Copy and move files between different adapters (even with different config types)
  • Well-tested and production-ready

Installation

go get gitlab.com/tbhaxor/gofsx

Usage Example

package main

import (
	"fmt"

	"gitlab.com/tbhaxor/gofsx"
	"gitlab.com/tbhaxor/gofsx/adapters"
)

// Local adapter
localAdapter, err := adapters.NewLocalAdapter("/tmp", nil)
if err != nil {
    panic(err)
}
// List files from local adapter
if files, err := localAdapter.ListContents(ctx, ".", true); err != nil {
    panic(err)
} else {
    fmt.Println(files)
}

Adapter Interface and Extensibility

gofsx is built on a set of small, composable interfaces:

type Readable interface {
	Read(ctx context.Context, path string) ([]byte, error)
}

type Writable[TWriteConfig any] interface {
	Write(ctx context.Context, path string, content []byte, config *TWriteConfig) error
}

type Deletable interface {
	Delete(ctx context.Context, path string, recurse bool) error
}

type Movable interface {
	Readable
	Deletable
}

type Adapter[TClient, TWriteConfig, TPublicUrlConfig, TTemporaryUrlConfig any] interface {
	Writable[TWriteConfig]
	Readable
	Deletable
	// ...plus advanced methods (see code)
}

To add your own adapter, implement the relevant interfaces for your backend.

Copying and Moving Between Adapters

You can copy or move files between any two adapters, even if their config types differ:

// Copy a file across adapters
err := gofsx.CopyBetweenAdapter(ctx, srcAdapter, "foo.txt", dstAdapter, "bar.txt", &dstAdapterWriteConfig)

// Move a file across adapters
err := gofsx.MoveBetweenAdapter(ctx, srcAdapter, "foo.txt", dstAdapter, "bar.txt", &dstAdapterWriteConfig)

Adapters

  • Local adapter
  • S3 adapter
  • FTP adapter
  • Google Cloud Storage adapter
  • Azure Blob Storage adapter

Contributing

Contributions are welcome! Please open issues or pull requests for new adapters, bug fixes, or improvements.

License

MIT License

About

No description, website, or topics provided.

Resources

License

Contributing

Stars

Watchers

Forks

Packages

No packages published

Languages