fsoss

package module
v0.0.1 Latest Latest
Warning

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

Go to latest
Published: Jan 17, 2026 License: MIT Imports: 9 Imported by: 1

README

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

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type OssFile

type OssFile struct {
	*os.File // The temporary local file
	// contains filtered or unexported fields
}

func NewOssFile

func NewOssFile(fs *OssFs, name string, flag int, perm os.FileMode) (*OssFile, error)

func (*OssFile) Close

func (f *OssFile) Close() error

func (*OssFile) Name

func (f *OssFile) Name() string

func (*OssFile) Readdir

func (f *OssFile) Readdir(count int) ([]os.FileInfo, error)

func (*OssFile) Readdirnames

func (f *OssFile) Readdirnames(n int) ([]string, error)

Readdirnames reads the directory

func (*OssFile) Sync

func (f *OssFile) Sync() error

func (*OssFile) Write

func (f *OssFile) Write(p []byte) (n int, err error)

func (*OssFile) WriteAt

func (f *OssFile) WriteAt(p []byte, off int64) (n int, err error)

func (*OssFile) WriteString

func (f *OssFile) WriteString(s string) (ret int, err error)

type OssFileInfo

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

func (*OssFileInfo) IsDir

func (fi *OssFileInfo) IsDir() bool

func (*OssFileInfo) ModTime

func (fi *OssFileInfo) ModTime() time.Time

func (*OssFileInfo) Mode

func (fi *OssFileInfo) Mode() os.FileMode

func (*OssFileInfo) Name

func (fi *OssFileInfo) Name() string

func (*OssFileInfo) Size

func (fi *OssFileInfo) Size() int64

func (*OssFileInfo) Sys

func (fi *OssFileInfo) Sys() interface{}

type OssFs

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

OssFs implements afero.Fs for Aliyun OSS

func NewOssFs

func NewOssFs(endpoint, accessKeyID, accessKeySecret, bucketName string) (*OssFs, error)

NewOssFs creates a new OssFs instance

func (*OssFs) Chmod

func (fs *OssFs) Chmod(name string, mode os.FileMode) error

func (*OssFs) Chown

func (fs *OssFs) Chown(name string, uid, gid int) error

func (*OssFs) Chtimes

func (fs *OssFs) Chtimes(name string, atime time.Time, mtime time.Time) error

func (*OssFs) Create

func (fs *OssFs) Create(name string) (afero.File, error)

func (*OssFs) Mkdir

func (fs *OssFs) Mkdir(name string, perm os.FileMode) error

func (*OssFs) MkdirAll

func (fs *OssFs) MkdirAll(name string, perm os.FileMode) error

func (*OssFs) Name

func (fs *OssFs) Name() string

func (*OssFs) Open

func (fs *OssFs) Open(name string) (afero.File, error)

func (*OssFs) OpenFile

func (fs *OssFs) OpenFile(name string, flag int, perm os.FileMode) (afero.File, error)

func (*OssFs) Remove

func (fs *OssFs) Remove(name string) error

func (*OssFs) RemoveAll

func (fs *OssFs) RemoveAll(path string) error

func (*OssFs) Rename

func (fs *OssFs) Rename(oldname, newname string) error

func (*OssFs) Stat

func (fs *OssFs) Stat(name string) (os.FileInfo, error)

Jump to

Keyboard shortcuts

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