filestore

package
v0.0.0-...-981c705 Latest Latest
Warning

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

Go to latest
Published: May 16, 2025 License: GPL-3.0 Imports: 14 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var MimeTypes = map[string]string{
	".jpg":  "image/jpeg",
	".jpeg": "image/jpeg",
	".png":  "image/png",
	".gif":  "image/gif",
	".webp": "image/webp",
	".svg":  "image/svg+xml",
	".bmp":  "image/bmp",
	".pdf":  "application/pdf",
	".doc":  "application/msword",
	".docx": "application/vnd.openxmlformats-officedocument.wordprocessingml.document",
	".xls":  "application/vnd.ms-excel",
	".xlsx": "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet",
	".ppt":  "application/vnd.ms-powerpoint",
	".pptx": "application/vnd.openxmlformats-officedocument.presentationml.presentation",
	".mp3":  "audio/mpeg",
	".mp4":  "video/mp4",
	".zip":  "application/zip",
	".rar":  "application/x-rar-compressed",
	".txt":  "text/plain",
	".html": "text/html",
	".css":  "text/css",
	".js":   "application/javascript",
	".json": "application/json",
	".xml":  "application/xml",
}

常用文件类型的MIME映射

Module 文件存储模块

Functions

func GeneratePath

func GeneratePath(ctx context.Context, baseDir, fileName string) string

GeneratePath 生成存储路径

func GetMimeType

func GetMimeType(filename string) string

GetMimeType 获取文件MIME类型

Types

type Config

type Config struct {
	Type      types.StorageType `json:"type"`       // 存储类型
	Path      string            `json:"path"`       // 本地存储路径
	URL       string            `json:"url"`        // 访问URL
	AccessKey string            `json:"access_key"` // 访问密钥
	SecretKey string            `json:"secret_key"` // 访问密钥
	Region    string            `json:"region"`     // 区域
	Bucket    string            `json:"bucket"`     // 桶名称
	Endpoint  string            `json:"endpoint"`   // 端点
}

Config 存储配置

type FileStorage

type FileStorage interface {
	// Save 保存文件
	Save(ctx context.Context, path string, data []byte) error

	// Load 加载文件
	Load(ctx context.Context, path string) ([]byte, error)

	// Delete 删除文件
	Delete(ctx context.Context, path string) error

	// List 列出文件
	List(ctx context.Context, prefix string) ([]string, error)

	// URL 获取文件URL
	URL(path string) string

	// URLWithContext 使用上下文获取文件URL
	URLWithContext(ctx context.Context, path string) string
}

FileStorage 文件存储接口

func NewFileStorage

func NewFileStorage(lc fx.Lifecycle, cfg *configs.Config) (FileStorage, error)

NewFileStorage 创建文件存储

type LocalStorage

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

LocalStorage 本地文件存储

func NewLocalStorage

func NewLocalStorage(cfg *configs.Config) (*LocalStorage, error)

NewLocalStorage 创建本地文件存储

func (*LocalStorage) Delete

func (s *LocalStorage) Delete(ctx context.Context, path string) error

Delete 删除文件

func (*LocalStorage) List

func (s *LocalStorage) List(ctx context.Context, prefix string) ([]string, error)

List 列出文件

func (*LocalStorage) Load

func (s *LocalStorage) Load(ctx context.Context, path string) ([]byte, error)

Load 加载文件

func (*LocalStorage) Save

func (s *LocalStorage) Save(ctx context.Context, path string, data []byte) error

Save 保存文件

func (*LocalStorage) URL

func (s *LocalStorage) URL(path string) string

URL 获取文件URL

func (*LocalStorage) URLWithContext

func (s *LocalStorage) URLWithContext(ctx context.Context, path string) string

URLWithContext 使用上下文获取文件URL

type S3Storage

type S3Storage struct {
}

S3Storage S3文件存储

func NewS3Storage

func NewS3Storage(cfg *configs.Config) (*S3Storage, error)

NewS3Storage 创建S3文件存储

func (*S3Storage) Delete

func (s *S3Storage) Delete(ctx context.Context, path string) error

Delete 删除文件

func (*S3Storage) List

func (s *S3Storage) List(ctx context.Context, prefix string) ([]string, error)

List 列出文件

func (*S3Storage) Load

func (s *S3Storage) Load(ctx context.Context, path string) ([]byte, error)

Load 加载文件

func (*S3Storage) Save

func (s *S3Storage) Save(ctx context.Context, path string, data []byte) error

Save 保存文件

func (*S3Storage) URL

func (s *S3Storage) URL(path string) string

URL 获取文件URL

func (*S3Storage) URLWithContext

func (s *S3Storage) URLWithContext(ctx context.Context, path string) string

URLWithContext 使用上下文获取文件URL

type Storage

type Storage struct {
	Config Config
	// contains filtered or unexported fields
}

Storage 存储服务

func New

func New(config Config) (*Storage, error)

New 创建存储服务

func NewStorage

func NewStorage(lc fx.Lifecycle, cfg *configs.Config) (*Storage, error)

NewStorage 创建存储服务

func (*Storage) Delete

func (s *Storage) Delete(ctx context.Context, path string) error

Delete 删除文件

func (*Storage) Get

func (s *Storage) Get(ctx context.Context, path string) (*os.File, error)

Get 获取文件

func (*Storage) GetEndpoint

func (s *Storage) GetEndpoint() string

GetEndpoint 获取存储端点

func (*Storage) GetEndpointWithContext

func (s *Storage) GetEndpointWithContext(ctx context.Context) string

GetEndpointWithContext 使用上下文获取存储端点

func (*Storage) GetStream

func (s *Storage) GetStream(ctx context.Context, path string) (io.ReadCloser, error)

GetStream 获取文件流

func (*Storage) GetURL

func (s *Storage) GetURL(ctx context.Context, path string) (string, error)

GetURL 获取文件URL

func (*Storage) List

func (s *Storage) List(ctx context.Context, path string) ([]*oss.Object, error)

List 列出目录下的文件

func (*Storage) Put

func (s *Storage) Put(ctx context.Context, path string, reader io.Reader) error

Put 上传文件

Jump to

Keyboard shortcuts

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