flysystem

package module
v0.0.3 Latest Latest
Warning

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

Go to latest
Published: Jul 5, 2023 License: Apache-2.0 Imports: 5 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

Officially supported adapters

example

package main

import (
	"fmt"
	"github.com/pkg6/go-flysystem"
	"github.com/pkg6/go-flysystem/fsoss"
	"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(root)
	ossAdapter := fsoss.New(fsoss.Config{
		Bucket:          "test",
		Endpoint:        "oss-cn-hangzhou.aliyuncs.com",
		AccessKeyID:     "*******************",
		AccessKeySecret: "**************",
		PathPrefix:      "shop",
	})
	//Initialize the adapter
	adapters := flysystem.NewAdapters(localAdapter)
	adapters.Extend(ossAdapter)
	adapters.Extend(local.New("./_example/test_data/2"), "local2")
	var err error
	_, err = adapters.WriteReader("4.txt", strings.NewReader("test"))
	fmt.Println(err)
	_, err = adapters.Disk("local2").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")
	if err != nil {
		return
	}
	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)
	//Create directory
	err = adapters.CreateDirectory("test1/test12")
	fmt.Println(err)
	//Delete directory
	_, err = adapters.DeleteDirectory("test1/test12")
	fmt.Println(err)
}

If Disk is not specified, it will be executed using the first registered driver

You can always create an adapter yourself.

Documentation

Index

Constants

View Source
const (
	DiskNameOSS   = "oss"
	DiskNameLocal = "local"

	PathTypeFile      = "file"
	PathTypeDirectory = "directory"
	ModePublicString  = "public"
	ModePrivateString = "private"
	ModeFilePublic    = 0644
	ModeFilePrivate   = 0600
	ModeDirPublic     = 0755
	ModeDirPrivate    = 0700
)

Variables

Functions

This section is empty.

Types

type AbstractAdapter

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

func (*AbstractAdapter) ApplyPathPrefix

func (a *AbstractAdapter) ApplyPathPrefix(path string) string

func (*AbstractAdapter) SetPathPrefix

func (a *AbstractAdapter) SetPathPrefix(prefix string)

type Flysystem

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

func (*Flysystem) Copy

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

func (*Flysystem) CreateDirectory

func (f *Flysystem) CreateDirectory(dirname string) error

func (*Flysystem) Delete

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

func (*Flysystem) DeleteDirectory

func (f *Flysystem) DeleteDirectory(dirname string) (int64, error)

func (*Flysystem) Disk

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

func (*Flysystem) Exists

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

func (*Flysystem) Extend

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

Extend 扩展

func (*Flysystem) FindAdapter

func (f *Flysystem) FindAdapter() IAdapter

FindAdapter Find Adapter

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) 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
	// Clone Initialization parameters
	Clone() 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 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)
	// DeleteDirectory Number of files deleted from the deleted directory
	DeleteDirectory(dirname string) (int64, error)
	// CreateDirectory create directory
	CreateDirectory(dirname 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)
}

type IFlysystem

type IFlysystem interface {
	IFS
	Extend(adapter IAdapter, names ...string) IFlysystem
	Disk(disk string) IFlysystem
	FindAdapter() IAdapter
}

func New

func New() IFlysystem

func NewAdapters

func NewAdapters(adapters ...IAdapter) IFlysystem

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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