flysystem

package module
v0.5.2 Latest Latest
Warning

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

Go to latest
Published: Mar 15, 2024 License: Apache-2.0 Imports: 8 Imported by: 0

README

go-flysystem

Go Report Card Go.Dev reference Sourcegraph Release Goproxy.cn

About Flysystem

Flysystem is a file storage library for Golang. It provides one interface to interact with many types of filesystems. When you use Flysystem, you're not only protected from vendor lock-in, you'll also have a consistent experience for which ever storage is right for you.

Install

$ go get github.com/pkg6/go-flysystem

example

package main

import (
	"fmt"
	"github.com/pkg6/go-flysystem"
	"github.com/pkg6/go-flysystem/local"
	"strings"
)

func main() {
	//Define the root directory of the local adapter
	root := "./_example/test_data"
	// Create local adapter
	localAdapter := local.New(&local.Config{Root: root})
	//Initialize the adapter
	adapters := flysystem.NewAdapters(localAdapter)
	adapters.Extend(local.New(&local.Config{Root: "./_example/test_data/2"}), "local2")
	var err error
	_, err = adapters.WriteReader("4.txt", strings.NewReader("test"))
	fmt.Println(err)
	adapter, err := adapters.Adapter("local2")
	_, err = adapter.WriteReader("4.txt", strings.NewReader("test"))
	fmt.Println(err)
	//Write file
	_, err = adapters.Write("1.txt", []byte("test data"))
	fmt.Println(err)
	//Write data from resource file
	_, err = adapters.WriteStream("2.txt", root+"/1.txt")
	fmt.Println(err)
	//Update file
	_, err = adapters.Update("1.txt", []byte("test update data"))
	fmt.Println(err)
	//Update data from resource file
	_, err = adapters.UpdateStream("2.txt", root+"/1.txt")
	fmt.Println(err)
	exists, _ := adapters.Exists("2.txt")
	fmt.Println(exists)
	//Read file
	read, err := adapters.Read("2.txt")
	fmt.Println(read, err)
	//Get file mime type
	mimeType, err := adapters.MimeType("2.txt")
	fmt.Println(mimeType, err)
	//Get file size
	size, err := adapters.Size("2.txt")
	fmt.Println(size, err)
	//Move file
	_, err = adapters.Move("1.txt", "4.txt")
	fmt.Println(err)
	//Copy file
	_, err = adapters.Copy("2.txt", "5.txt")
	fmt.Println(err)
}

Associated Projects

gfs: github.com/pkg6/gfs

Documentation

Index

Constants

View Source
const (
	DiskNameLocal                   = gfs.DiskNameLocal
	DiskNameOSS                     = gfs.DiskNameOSS
	DiskNameCOS                     = gfs.DiskNameCOS
	DiskNameBOS                     = gfs.DiskNameBOS
	DiskNameGoogleCloudCloudStorage = gfs.DiskNameGoogleCloudCloudStorage
	DiskNameQiNiuKoDo               = gfs.DiskNameQiNiuKoDo
)

Variables

View Source
var (
	FileModes = gfs.FileModes
)

Functions

func DecodeBase64 added in v0.4.0

func DecodeBase64(s string) ([]byte, error)

func EncodeBase64 added in v0.4.0

func EncodeBase64(src []byte) string

func OpenFileBase64 added in v0.4.0

func OpenFileBase64(filePath string) (string, error)

OpenFileBase64 文件路径转base64

func SaveFileBase64 added in v0.4.0

func SaveFileBase64(path string, data []byte) error

SaveFileBase64 文件路径转base64

Types

type Flysystem

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

func New

func New() *Flysystem

func NewAdapters

func NewAdapters(adapters ...IAdapter) *Flysystem

func NewConfig added in v0.3.2

func NewConfig(config any) (*Flysystem, error)

func (*Flysystem) Adapter added in v0.3.0

func (f *Flysystem) Adapter(disk string) (IAdapter, error)

Adapter Find Adapter

func (*Flysystem) Copy

func (f *Flysystem) Copy(source, destination string) (bool, error)

func (*Flysystem) Delete

func (f *Flysystem) Delete(path string) (int64, error)

func (*Flysystem) Disk

func (f *Flysystem) Disk(disk string) string

func (*Flysystem) DiskExist added in v0.3.0

func (f *Flysystem) DiskExist(disk string) bool

DiskExist 判断驱动是否存在

func (*Flysystem) Disks added in v0.3.2

func (f *Flysystem) Disks() []string

Disks 获取注册所有的驱动

func (*Flysystem) Exists

func (f *Flysystem) Exists(path string) (bool, error)

func (*Flysystem) Extend

func (f *Flysystem) Extend(adapter IAdapter, names ...string) *Flysystem

Extend 扩展

func (*Flysystem) ExtendConfigPtr added in v0.3.2

func (f *Flysystem) ExtendConfigPtr(config any) error

func (*Flysystem) GFSAdapter added in v0.3.0

func (f *Flysystem) GFSAdapter(disk string) (gfs.IAdapter, error)

func (*Flysystem) MimeType

func (f *Flysystem) MimeType(path string) (string, error)

func (*Flysystem) Move

func (f *Flysystem) Move(source, destination string) (bool, error)

func (*Flysystem) Read

func (f *Flysystem) Read(path string) ([]byte, error)

func (*Flysystem) Size

func (f *Flysystem) Size(path string) (int64, error)

func (*Flysystem) URL added in v0.1.2

func (f *Flysystem) URL(path string) (*url.URL, error)

func (*Flysystem) Update

func (f *Flysystem) Update(path string, contents []byte) (string, error)

func (*Flysystem) UpdateStream

func (f *Flysystem) UpdateStream(path, resource string) (string, error)

func (*Flysystem) Write

func (f *Flysystem) Write(path string, contents []byte) (string, error)

func (*Flysystem) WriteReader added in v0.0.2

func (f *Flysystem) WriteReader(path string, reader io.Reader) (string, error)

func (*Flysystem) WriteStream

func (f *Flysystem) WriteStream(path, resource string) (string, error)

type IAdapter

type IAdapter interface {
	IFS
	// DiskName Default Disk Name
	DiskName() string

	GFSAdapter() gfs.IAdapter
}

type IBFS added in v0.0.3

type IBFS interface {
	// Exists Determine if the file exists
	Exists(path string) (bool, error)
	// WriteReader write file content and return full path
	WriteReader(path string, reader io.Reader) (string, error)
	// Write  file content and return full path
	Write(path string, contents []byte) (string, error)
	// WriteStream Resource file write returns full path
	WriteStream(path, resource string) (string, error)
	// Read Read file
	Read(path string) ([]byte, error)
	// Delete  Deleting files returns the number of deleted files
	Delete(path string) (int64, error)
}

type IConfig added in v0.3.2

type IConfig interface {
	New() IAdapter
}

type IFS

type IFS interface {
	IBFS
	// Size Get File Size
	Size(path string) (int64, error)
	// Update  the file content and return the updated full path
	Update(path string, contents []byte) (string, error)
	// UpdateStream Return the updated full path based on resource file updates
	UpdateStream(path, resource string) (string, error)
	// MimeType Get File MimeType
	MimeType(path string) (string, error)
	// Move move file
	Move(source, destination string) (bool, error)
	// Copy copy file
	Copy(source, destination string) (bool, error)
	URL(path string) (*url.URL, error)
}

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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