Path: blob/main/vendor/github.com/spf13/afero/README.md
2875 views

Afero: The Universal Filesystem Abstraction for Go
Afero is a powerful and extensible filesystem abstraction system for Go. It provides a single, unified API for interacting with diverse filesystemsβincluding the local disk, memory, archives, and network storage.
Afero acts as a drop-in replacement for the standard os package, enabling you to write modular code that is agnostic to the underlying storage, dramatically simplifies testing, and allows for sophisticated architectural patterns through filesystem composition.
Why Afero?
Afero elevates filesystem interaction beyond simple file reading and writing, offering solutions for testability, flexibility, and advanced architecture.
π Key Features:
Universal API: Write your code once. Run it against the local OS, in-memory storage, ZIP/TAR archives, or remote systems (SFTP, GCS).
Ultimate Testability: Utilize
MemMapFs, a fully concurrent-safe, read/write in-memory filesystem. Write fast, isolated, and reliable unit tests without touching the physical disk or worrying about cleanup.Powerful Composition: Afero's hidden superpower. Layer filesystems on top of each other to create sophisticated behaviors:
Sandboxing: Use
CopyOnWriteFsto create temporary scratch spaces that isolate changes from the base filesystem.Caching: Use
CacheOnReadFsto automatically layer a fast cache (like memory) over a slow backend (like a network drive).Security Jails: Use
BasePathFsto restrict application access to a specific subdirectory (chroot).
osPackage Compatibility: Afero mirrors the functions in the standardospackage, making adoption and refactoring seamless.io/fsCompatibility: Fully compatible with the Go standard library'sio/fsinterfaces.
Installation
Quick Start: The Power of Abstraction
The core of Afero is the afero.Fs interface. By designing your functions to accept this interface rather than calling os.* functions directly, your code instantly becomes more flexible and testable.
1. Refactor Your Code
Change functions that rely on the os package to accept afero.Fs.
2. Usage in Production
In your production environment, inject the OsFs backend, which wraps the standard operating system calls.
3. Usage in Testing
In your tests, inject MemMapFs. This provides a blazing-fast, isolated, in-memory filesystem that requires no disk I/O and no cleanup.
Afero's Superpower: Composition
Afero's most unique feature is its ability to combine filesystems. This allows you to build complex behaviors out of simple components, keeping your application logic clean.
Example 1: Sandboxing with Copy-on-Write
Create a temporary environment where an application can "modify" system files without affecting the actual disk.
Example 2: Caching a Slow Filesystem
Improve performance by layering a fast cache (like memory) over a slow backend (like a network drive or cloud storage).
Example 3: Security Jails (chroot)
Restrict an application component's access to a specific subdirectory.
Real-World Use Cases
Build Cloud-Agnostic Applications
Write applications that seamlessly work with different storage backends:
Treating Archives as Filesystems
Read files directly from .zip or .tar archives without unpacking them to disk first.
Serving Any Filesystem over HTTP
Use HttpFs to expose any Afero filesystemβeven one created dynamically in memoryβthrough a standard Go web server.
Testing Made Simple
One of Afero's greatest strengths is making filesystem-dependent code easily testable:
Benefits of testing with Afero:
β‘ Fast - No disk I/O, tests run in memory
π Reliable - Each test starts with a clean slate
π§Ή No cleanup - Memory is automatically freed
π Safe - Can't accidentally modify real files
π Parallel - Tests can run concurrently without conflicts
Backend Reference
| Type | Backend | Constructor | Description | Status |
|---|---|---|---|---|
| Core | OsFs | afero.NewOsFs() | Interacts with the real operating system filesystem. Use in production. | β Official |
| MemMapFs | afero.NewMemMapFs() | A fast, atomic, concurrent-safe, in-memory filesystem. Ideal for testing. | β Official | |
| Composition | CopyOnWriteFs | afero.NewCopyOnWriteFs(base, overlay) | A read-only base with a writable overlay. Ideal for sandboxing. | β Official |
| CacheOnReadFs | afero.NewCacheOnReadFs(base, cache, ttl) | Lazily caches files from a slow base into a fast layer on first read. | β Official | |
| BasePathFs | afero.NewBasePathFs(source, path) | Restricts operations to a subdirectory (chroot/jail). | β Official | |
| ReadOnlyFs | afero.NewReadOnlyFs(source) | Provides a read-only view, preventing any modifications. | β Official | |
| RegexpFs | afero.NewRegexpFs(source, regexp) | Filters a filesystem, only showing files that match a regex. | β Official | |
| Utility | HttpFs | afero.NewHttpFs(source) | Wraps any Afero filesystem to be served via http.FileServer. | β Official |
| Archives | ZipFs | zipfs.New(zipReader) | Read-only access to files within a ZIP archive. | β Official |
| TarFs | tarfs.New(tarReader) | Read-only access to files within a TAR archive. | β Official | |
| Network | GcsFs | gcsfs.NewGcsFs(...) | Google Cloud Storage backend. | β‘ Experimental |
| SftpFs | sftpfs.New(...) | SFTP backend. | β‘ Experimental | |
| 3rd Party Cloud | S3Fs | fclairamb/afero-s3 | Production-ready S3 backend built on official AWS SDK. | πΉ 3rd Party |
| MinioFs | cpyun/afero-minio | MinIO object storage backend with S3 compatibility. | πΉ 3rd Party | |
| DriveFs | fclairamb/afero-gdrive | Google Drive backend with streaming support. | πΉ 3rd Party | |
| DropboxFs | fclairamb/afero-dropbox | Dropbox backend with streaming support. | πΉ 3rd Party | |
| 3rd Party Specialized | GitFs | tobiash/go-gitfs | Git repository filesystem (read-only, Afero compatible). | πΉ 3rd Party |
| DockerFs | unmango/aferox | Docker container filesystem access. | πΉ 3rd Party | |
| GitHubFs | unmango/aferox | GitHub repository and releases filesystem. | πΉ 3rd Party | |
| FilterFs | unmango/aferox | Filesystem filtering with predicates. | πΉ 3rd Party | |
| IgnoreFs | unmango/aferox | .gitignore-aware filtering filesystem. | πΉ 3rd Party | |
| FUSEFs | JakWai01/sile-fystem | Generic FUSE implementation using any Afero backend. | πΉ 3rd Party |
Afero vs. io/fs (Go 1.16+)
Go 1.16 introduced the io/fs package, which provides a standard abstraction for read-only filesystems.
Afero complements io/fs by focusing on different needs:
Use
io/fswhen: You only need to read files and want to conform strictly to the standard library interfaces.Use Afero when:
Your application needs to create, write, modify, or delete files.
You need to test complex read/write interactions (e.g., renaming, concurrent writes).
You need advanced compositional features (Copy-on-Write, Caching, etc.).
Afero is fully compatible with io/fs. You can wrap any Afero filesystem to satisfy the fs.FS interface using afero.NewIOFS:
Third-Party Backends & Ecosystem
The Afero community has developed numerous backends and tools that extend the library's capabilities. Below are curated, well-maintained options organized by maturity and reliability.
Featured Community Backends
These are mature, reliable backends that we can confidently recommend for production use:
Amazon S3 - fclairamb/afero-s3
Production-ready S3 backend built on the official AWS SDK for Go.
MinIO - cpyun/afero-minio
MinIO object storage backend providing S3-compatible object storage with deduplication and optimization features.
Community & Specialized Backends
Cloud Storage
Google Drive -
fclairamb/afero-gdriveStreaming support; no write-seeking or POSIX permissions; no files listing cacheDropbox -
fclairamb/afero-dropboxStreaming support; no write-seeking or POSIX permissions
Version Control Systems
Git Repositories -
tobiash/go-gitfsRead-only filesystem abstraction for Git repositories. Works with bare repositories and provides filesystem view of any git reference. Uses go-git for repository access.
Container and Remote Systems
Docker Containers -
unmango/aferoxAccess Docker container filesystems as if they were local filesystemsGitHub API -
unmango/aferoxTurn GitHub repositories, releases, and assets into browsable filesystems
FUSE Integration
Generic FUSE -
JakWai01/sile-fystemMount any Afero filesystem as a FUSE filesystem, allowing any Afero backend to be used as a real mounted filesystem
Specialized Filesystems
FAT32 Support -
aligator/GoFATPure Go FAT filesystem implementation (currently read-only)
Interface Adapters & Utilities
Cross-Interface Compatibility:
jfontan/go-billy-desfacer- Adapter between Afero and go-billy interfaces (for go-git compatibility)Maldris/go-billy-afero- Alternative wrapper for using Afero with go-billyc4milo/afero2billy- Another Afero to billy filesystem adapter
Working Directory Management:
carolynvs/aferox- Working directory-aware filesystem wrapper
Advanced Filtering:
unmango/aferoxincludes multiple specialized filesystems:FilterFs - Predicate-based file filtering
IgnoreFs - .gitignore-aware filtering
WriterFs - Dump writes to io.Writer for debugging
Developer Tools & Utilities
nhatthm Utility Suite - Essential tools for Afero development:
nhatthm/aferocopy- Copy files between any Afero filesystemsnhatthm/aferomock- Mocking toolkit for testingnhatthm/aferoassert- Assertion helpers for filesystem testing
Ecosystem Showcase
Windows Virtual Drives - balazsgrill/potatodrive
Mount any Afero filesystem as a Windows drive letter. Brilliant demonstration of Afero's power!
Modern Asset Embedding (Go 1.16+)
Instead of third-party tools, use Go's native //go:embed with Afero:
Contributing
We welcome contributions! The project is mature, but we are actively looking for contributors to help implement and stabilize network/cloud backends.
π₯ Microsoft Azure Blob Storage
π Modern Encryption Backend - Built on secure, contemporary crypto (not legacy EncFS)
π Canonical go-git Adapter - Unified solution for Git integration
π‘ SSH/SCP Backend - Secure remote file operations
Stabilization of existing experimental backends (GCS, SFTP)
To contribute:
Fork the repository
Create your feature branch (
git checkout -b my-new-feature)Commit your changes (
git commit -am 'Add some feature')Push to the branch (
git push origin my-new-feature)Create a new Pull Request
π License
Afero is released under the Apache 2.0 license. See LICENSE.txt for details.
π Additional Resources
Afero comes from the Latin roots Ad-Facere, meaning "to make" or "to do" - fitting for a library that empowers you to make and do amazing things with filesystems.