fsoss is a Go library that implements the afero filesystem interface for Aliyun OSS (Object Storage Service).
It allows you to use Aliyun OSS as a backend for any application that uses afero, enabling easy switching between local filesystem, memory filesystem, and OSS.
go get github.com/techquest-tech/fsosspackage main
import (
"fmt"
"log"
"os"
"github.com/spf13/afero"
"github.com/techquest-tech/fsoss"
)
func main() {
endpoint := "oss-cn-hangzhou.aliyuncs.com"
accessKeyID := "your-access-key-id"
accessKeySecret := "your-access-key-secret"
bucketName := "your-bucket-name"
// Create a new OSS filesystem instance
ossFs, err := fsoss.NewOssFs(endpoint, accessKeyID, accessKeySecret, bucketName)
if err != nil {
log.Fatal(err)
}
// Use it like any other afero.Fs
var AppFs afero.Fs = ossFs
// Create a file
f, err := AppFs.Create("test.txt")
if err != nil {
log.Fatal(err)
}
f.WriteString("Hello, Aliyun OSS!")
f.Close()
// Read a file
f, err = AppFs.Open("test.txt")
if err != nil {
log.Fatal(err)
}
defer f.Close()
b := make([]byte, 100)
n, _ := f.Read(b)
fmt.Println(string(b[:n]))
// Check file info
fi, err := AppFs.Stat("test.txt")
if err == nil {
fmt.Printf("Size: %d, ModTime: %s\n", fi.Size(), fi.ModTime())
}
}- Implements
afero.Fsinterface. - Uses local temporary files for read/write operations to ensure full compatibility (seeking, partial writes, etc.).
- Uploads changes to OSS only on
Close()orSync(). - Supports directory simulation (OSS prefixes).
Chmod,Chown,Chtimesare no-ops as OSS metadata is different from POSIX attributes.- Performance: Opening a file downloads it entirely to a local temporary file. This ensures compatibility but may be slow for large files if you only need a small part.
- Atomicity: File writes are atomic (put object), but directory operations are not fully atomic.
MIT