Skip to content

techquest-tech/fsoss

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

3 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

fsoss

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.

Installation

go get github.com/techquest-tech/fsoss

Usage

package 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())
	}
}

Features

  • Implements afero.Fs interface.
  • Uses local temporary files for read/write operations to ensure full compatibility (seeking, partial writes, etc.).
  • Uploads changes to OSS only on Close() or Sync().
  • Supports directory simulation (OSS prefixes).

Limitations

  • Chmod, Chown, Chtimes are 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.

License

MIT

About

No description, website, or topics provided.

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages