storage

package
v1.0.1 Latest Latest
Warning

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

Go to latest
Published: May 17, 2026 License: MIT Imports: 35 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func MimeType

func MimeType(path string) string

MimeType 根据文件扩展名检测 MIME 类型,统一各存储实现行为

Types

type COSConfig

type COSConfig struct {
	SecretID   string `json:"secret-id"`
	SecretKey  string `json:"secret-key"`
	Region     string `json:"region"`
	Bucket     string `json:"bucket"`
	UseSSL     bool   `json:"use-ssl"`
	URLPrefix  string `json:"url-prefix"`
	DateFormat string `json:"date-format"`
}

COSConfig 腾讯云 COS 配置

type COSStorage

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

COSStorage 腾讯云 COS 存储实现

func NewCOSStorage

func NewCOSStorage(config *COSConfig) (*COSStorage, error)

NewCOSStorage 创建 COS 存储实例

func (*COSStorage) Copy

func (c *COSStorage) Copy(ctx context.Context, srcPath, dstPath string) error

Copy 复制文件

func (*COSStorage) Delete

func (c *COSStorage) Delete(ctx context.Context, path string) error

Delete 删除文件

func (*COSStorage) Exists

func (c *COSStorage) Exists(ctx context.Context, path string) (bool, error)

Exists 检查文件是否存在

func (*COSStorage) Get

func (c *COSStorage) Get(ctx context.Context, path string) (io.ReadCloser, error)

Get 获取文件

func (*COSStorage) List

func (c *COSStorage) List(ctx context.Context, prefix string) ([]*FileInfo, error)

List 列出文件

func (*COSStorage) Move

func (c *COSStorage) Move(ctx context.Context, srcPath, dstPath string) error

Move 移动文件

func (*COSStorage) Put

func (c *COSStorage) Put(ctx context.Context, path string, reader io.Reader, size int64) (*FileInfo, error)

Put 上传文件

func (*COSStorage) PutFile

func (c *COSStorage) PutFile(ctx context.Context, path string, localPath string) (*FileInfo, error)

PutFile 上传本地文件

func (*COSStorage) Size

func (c *COSStorage) Size(ctx context.Context, path string) (int64, error)

Size 获取文件大小

func (*COSStorage) URL

func (c *COSStorage) URL(ctx context.Context, path string) (string, error)

URL 获取文件访问 URL

type CacheConfig

type CacheConfig struct {
	Enabled bool   `json:"enabled"`
	Driver  string `json:"driver"`
	TTL     int    `json:"ttl"`
	Prefix  string `json:"prefix"`
}

CacheConfig 缓存配置

type FileHandle

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

func NewFileHandle

func NewFileHandle(storage *StorageManager) *FileHandle

func (*FileHandle) ModuleName

func (i *FileHandle) ModuleName() (en string, zh string)

func (*FileHandle) Upload

func (i *FileHandle) Upload(ctx *gin.Context)

type FileInfo

type FileInfo struct {
	Name        string            `json:"name"`
	Path        string            `json:"path"`
	Size        int64             `json:"size"`
	ContentType string            `json:"content_type"`
	Extension   string            `json:"extension"`
	URL         string            `json:"url"`
	Hash        string            `json:"hash"`
	UploadTime  time.Time         `json:"upload_time"`
	Metadata    map[string]string `json:"metadata"`
}

FileInfo 文件信息

type ImageConfig

type ImageConfig struct {
	Enabled    bool                       `json:"enabled"`
	Quality    int                        `json:"quality"`
	Formats    []string                   `json:"formats"`
	Thumbnails map[string]ThumbnailConfig `json:"thumbnails"`
	Watermark  WatermarkConfig            `json:"watermark"`
}

ImageConfig 图片处理配置

type LocalConfig

type LocalConfig struct {
	Root       string `json:"root"`
	URLPrefix  string `json:"url-prefix"`
	CreateDirs bool   `json:"create-dirs"`
	DirMode    uint32 `json:"dir-mode"`
	FileMode   uint32 `json:"file-mode"`
	DateFormat string `json:"date-format"`
}

LocalConfig 本地存储配置

type LocalStorage

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

LocalStorage 本地存储实现

func NewLocalStorage

func NewLocalStorage(config *LocalConfig) *LocalStorage

NewLocalStorage 创建本地存储实例

func (*LocalStorage) Copy

func (ls *LocalStorage) Copy(ctx context.Context, srcPath, dstPath string) error

Copy 复制文件

func (*LocalStorage) Delete

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

Delete 删除文件

func (*LocalStorage) Exists

func (ls *LocalStorage) Exists(ctx context.Context, path string) (bool, error)

Exists 检查文件是否存在

func (*LocalStorage) Get

func (ls *LocalStorage) Get(ctx context.Context, path string) (io.ReadCloser, error)

Get 获取文件

func (*LocalStorage) List

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

List 列出文件

func (*LocalStorage) Move

func (ls *LocalStorage) Move(ctx context.Context, srcPath, dstPath string) error

Move 移动文件

func (*LocalStorage) Put

func (ls *LocalStorage) Put(ctx context.Context, path string, reader io.Reader, size int64) (*FileInfo, error)

Put 上传文件

func (*LocalStorage) PutFile

func (ls *LocalStorage) PutFile(ctx context.Context, path string, localPath string) (*FileInfo, error)

PutFile 上传本地文件

func (*LocalStorage) Size

func (ls *LocalStorage) Size(ctx context.Context, path string) (int64, error)

Size 获取文件大小

func (*LocalStorage) URL

func (ls *LocalStorage) URL(ctx context.Context, path string) (string, error)

URL 获取文件访问 URL

type LoggingConfig

type LoggingConfig struct {
	Enabled     bool   `json:"enabled"`
	Level       string `json:"level"`
	LogUpload   bool   `json:"log-upload"`
	LogDownload bool   `json:"log-download"`
	LogDelete   bool   `json:"log-delete"`
}

LoggingConfig 日志配置

type MinIOConfig

type MinIOConfig struct {
	Endpoint        string `json:"endpoint"`
	AccessKeyID     string `json:"access-key-id"`
	SecretAccessKey string `json:"secret-access-key"`
	UseSSL          bool   `json:"use-ssl"`
	Bucket          string `json:"bucket"`
	Region          string `json:"region"`
	URLPrefix       string `json:"url-prefix"`
	DateFormat      string `json:"date-format"`
}

MinIOConfig MinIO 配置

type MinIOStorage

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

MinIOStorage MinIO 存储实现

func NewMinIOStorage

func NewMinIOStorage(config *MinIOConfig) (*MinIOStorage, error)

NewMinIOStorage 创建 MinIO 存储实例

func (*MinIOStorage) Copy

func (m *MinIOStorage) Copy(ctx context.Context, srcPath, dstPath string) error

Copy 复制文件

func (*MinIOStorage) Delete

func (m *MinIOStorage) Delete(ctx context.Context, path string) error

Delete 删除文件

func (*MinIOStorage) Exists

func (m *MinIOStorage) Exists(ctx context.Context, path string) (bool, error)

Exists 检查文件是否存在

func (*MinIOStorage) Get

func (m *MinIOStorage) Get(ctx context.Context, path string) (io.ReadCloser, error)

Get 获取文件

func (*MinIOStorage) List

func (m *MinIOStorage) List(ctx context.Context, prefix string) ([]*FileInfo, error)

List 列出文件

func (*MinIOStorage) Move

func (m *MinIOStorage) Move(ctx context.Context, srcPath, dstPath string) error

Move 移动文件

func (*MinIOStorage) Put

func (m *MinIOStorage) Put(ctx context.Context, path string, reader io.Reader, size int64) (*FileInfo, error)

Put 上传文件

func (*MinIOStorage) PutFile

func (m *MinIOStorage) PutFile(ctx context.Context, path string, localPath string) (*FileInfo, error)

PutFile 上传本地文件

func (*MinIOStorage) Size

func (m *MinIOStorage) Size(ctx context.Context, path string) (int64, error)

Size 获取文件大小

func (*MinIOStorage) URL

func (m *MinIOStorage) URL(ctx context.Context, path string) (string, error)

URL 获取文件访问 URL

type MonitoringConfig

type MonitoringConfig struct {
	Enabled      bool `json:"enabled"`
	Interval     int  `json:"interval"`
	MonitorSpace bool `json:"monitor-space"`
	MonitorFiles bool `json:"monitor-files"`
	MonitorSpeed bool `json:"monitor-speed"`
}

MonitoringConfig 监控配置

type OSSConfig

type OSSConfig struct {
	AccessKeyID     string `json:"access-key-id"`
	SecretAccessKey string `json:"secret-access-key"`
	Endpoint        string `json:"endpoint"`
	Bucket          string `json:"bucket"`
	UseSSL          bool   `json:"use-ssl"`
	URLPrefix       string `json:"url-prefix"`
	DateFormat      string `json:"date-format"`
}

OSSConfig 阿里云 OSS 配置

type Options

type Options struct {
	Driver     string           `json:"driver"`
	Local      LocalConfig      `json:"local"`
	S3         S3Config         `json:"s3"`
	MinIO      MinIOConfig      `json:"minio"`
	OSS        OSSConfig        `json:"oss"`
	COS        COSConfig        `json:"cos"`
	Qiniu      QiniuConfig      `json:"qiniu"`
	Upload     UploadConfig     `json:"upload"`
	Image      ImageConfig      `json:"image"`
	Security   SecurityConfig   `json:"security"`
	Cache      CacheConfig      `json:"cache"`
	Logging    LoggingConfig    `json:"logging"`
	Monitoring MonitoringConfig `json:"monitoring"`
}

Options 存储配置选项

type QiniuConfig

type QiniuConfig struct {
	AccessKey  string `json:"access-key"`
	SecretKey  string `json:"secret-key"`
	Bucket     string `json:"bucket"`
	Domain     string `json:"domain"`
	UseSSL     bool   `json:"use-ssl"`
	Zone       string `json:"zone"`
	DateFormat string `json:"date-format"`
}

QiniuConfig 七牛云配置

type QiniuStorage

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

QiniuStorage 七牛云存储实现(上传走 storagev2;列举/元数据/删除等走 v1 BucketManager,避免 storagev2/objects 在部分 Go 版本下无法编译)

func NewQiniuStorage

func NewQiniuStorage(config *QiniuConfig) (*QiniuStorage, error)

NewQiniuStorage 创建七牛云存储实例

func (*QiniuStorage) Copy

func (q *QiniuStorage) Copy(ctx context.Context, srcPath, dstPath string) error

Copy 复制文件

func (*QiniuStorage) Delete

func (q *QiniuStorage) Delete(ctx context.Context, path string) error

Delete 删除文件

func (*QiniuStorage) Exists

func (q *QiniuStorage) Exists(ctx context.Context, path string) (bool, error)

Exists 检查文件是否存在

func (*QiniuStorage) Get

func (q *QiniuStorage) Get(ctx context.Context, path string) (io.ReadCloser, error)

Get 获取文件

func (*QiniuStorage) List

func (q *QiniuStorage) List(ctx context.Context, prefix string) ([]*FileInfo, error)

List 列出文件

func (*QiniuStorage) Move

func (q *QiniuStorage) Move(ctx context.Context, srcPath, dstPath string) error

Move 移动文件

func (*QiniuStorage) Put

func (q *QiniuStorage) Put(ctx context.Context, path string, reader io.Reader, _ int64) (*FileInfo, error)

Put 上传文件

func (*QiniuStorage) PutFile

func (q *QiniuStorage) PutFile(ctx context.Context, path string, localPath string) (*FileInfo, error)

PutFile 上传本地文件

func (*QiniuStorage) Size

func (q *QiniuStorage) Size(ctx context.Context, path string) (int64, error)

Size 获取文件大小

func (*QiniuStorage) URL

func (q *QiniuStorage) URL(ctx context.Context, path string) (string, error)

URL 获取文件访问 URL

type S3Config

type S3Config struct {
	AccessKeyID     string `json:"access-key-id"`
	SecretAccessKey string `json:"secret-access-key"`
	Region          string `json:"region"`
	Bucket          string `json:"bucket"`
	Endpoint        string `json:"endpoint"`
	UseSSL          bool   `json:"use-ssl"`
	PathStyle       bool   `json:"path-style"`
	ACL             string `json:"acl"`
	URLPrefix       string `json:"url-prefix"`
	DateFormat      string `json:"date-format"`
}

S3Config Amazon S3 配置

type S3Storage

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

S3Storage S3 存储实现

func NewS3Storage

func NewS3Storage(config *S3Config) (*S3Storage, error)

NewS3Storage 创建 S3 存储实例

func (*S3Storage) Copy

func (s *S3Storage) Copy(ctx context.Context, srcPath, dstPath string) error

Copy 复制文件

func (*S3Storage) Delete

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

Delete 删除文件

func (*S3Storage) Exists

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

Exists 检查文件是否存在

func (*S3Storage) Get

func (s *S3Storage) Get(ctx context.Context, path string) (io.ReadCloser, error)

Get 获取文件

func (*S3Storage) List

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

List 列出文件

func (*S3Storage) Move

func (s *S3Storage) Move(ctx context.Context, srcPath, dstPath string) error

Move 移动文件

func (*S3Storage) Put

func (s *S3Storage) Put(ctx context.Context, path string, reader io.Reader, size int64) (*FileInfo, error)

Put 上传文件

func (*S3Storage) PutFile

func (s *S3Storage) PutFile(ctx context.Context, path string, localPath string) (*FileInfo, error)

PutFile 上传本地文件

func (*S3Storage) Size

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

Size 获取文件大小

func (*S3Storage) URL

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

URL 获取文件访问 URL

type SecurityConfig

type SecurityConfig struct {
	VirusScan           bool     `json:"virus-scan"`
	ScanCommand         string   `json:"scan-command"`
	ContentCheck        bool     `json:"content-check"`
	ForbiddenTypes      []string `json:"forbidden-types"`
	ForbiddenExtensions []string `json:"forbidden-extensions"`
}

SecurityConfig 安全配置

type Storage

type Storage interface {
	// Put 上传文件
	Put(ctx context.Context, path string, reader io.Reader, size int64) (*FileInfo, error)

	// PutFile 上传本地文件
	PutFile(ctx context.Context, path string, localPath string) (*FileInfo, error)

	// Get 获取文件
	Get(ctx context.Context, path string) (io.ReadCloser, error)

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

	// Exists 检查文件是否存在
	Exists(ctx context.Context, path string) (bool, error)

	// Size 获取文件大小
	Size(ctx context.Context, path string) (int64, error)

	// URL 获取文件访问 URL
	URL(ctx context.Context, path string) (string, error)

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

	// Copy 复制文件
	Copy(ctx context.Context, srcPath, dstPath string) error

	// Move 移动文件
	Move(ctx context.Context, srcPath, dstPath string) error
}

Storage 存储接口

type StorageManager

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

StorageManager 存储管理器

func NewStorageManager

func NewStorageManager() *StorageManager

NewStorageManager 创建存储管理器

func (*StorageManager) AddDriver

func (sm *StorageManager) AddDriver(name string, driver Storage)

AddDriver 添加存储驱动

func (*StorageManager) Default

func (sm *StorageManager) Default() (Storage, error)

Default 获取默认驱动

func (*StorageManager) Delete

func (sm *StorageManager) Delete(ctx context.Context, path string) error

Delete 使用默认驱动删除文件

func (*StorageManager) Exists

func (sm *StorageManager) Exists(ctx context.Context, path string) (bool, error)

Exists 使用默认驱动检查文件是否存在

func (*StorageManager) Get

func (sm *StorageManager) Get(ctx context.Context, path string) (io.ReadCloser, error)

Get 使用默认驱动获取文件

func (*StorageManager) GetDriver

func (sm *StorageManager) GetDriver(name string) (Storage, error)

GetDriver 获取指定驱动

func (*StorageManager) Put

func (sm *StorageManager) Put(ctx context.Context, path string, reader io.Reader, size int64) (*FileInfo, error)

Put 使用默认驱动上传文件

func (*StorageManager) SetDefaultDriver

func (sm *StorageManager) SetDefaultDriver(name string) error

SetDefaultDriver 设置默认驱动

func (*StorageManager) URL

func (sm *StorageManager) URL(ctx context.Context, path string) (string, error)

URL 使用默认驱动获取文件 URL

type StorageServiceProvider

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

StorageServiceProvider 存储服务提供者

func (*StorageServiceProvider) Boot

func (s *StorageServiceProvider) Boot()

Boot 启动服务

func (*StorageServiceProvider) Conf

func (s *StorageServiceProvider) Conf() map[string]string

func (*StorageServiceProvider) Description

func (s *StorageServiceProvider) Description() string

func (*StorageServiceProvider) Register

func (s *StorageServiceProvider) Register()

Register 注册服务

type ThumbnailConfig

type ThumbnailConfig struct {
	Width  int    `json:"width"`
	Height int    `json:"height"`
	Mode   string `json:"mode"`
}

ThumbnailConfig 缩略图配置

type UploadConfig

type UploadConfig struct {
	MaxFileSize       int64    `json:"max-file-size"`
	AllowedTypes      []string `json:"allowed-types"`
	AllowedExtensions []string `json:"allowed-extensions"`
	CheckFileType     bool     `json:"check-file-type"`
	UniqueFilename    bool     `json:"unique-filename"`
	FilenameStrategy  string   `json:"filename-strategy"`
	KeepOriginalName  bool     `json:"keep-original-name"`
}

UploadConfig 上传配置

type UploadResult

type UploadResult struct {
	FileInfo *FileInfo `json:"file_info"`
	Success  bool      `json:"success"`
	Message  string    `json:"message"`
	Error    error     `json:"error,omitempty"`
}

UploadResult 上传结果

type WatermarkConfig

type WatermarkConfig struct {
	Enabled  bool    `json:"enabled"`
	Image    string  `json:"image"`
	Position string  `json:"position"`
	Opacity  float64 `json:"opacity"`
	Margin   int     `json:"margin"`
}

WatermarkConfig 水印配置

Source Files

  • cos.go
  • file_handle.go
  • local.go
  • minio.go
  • qiniu.go
  • s3.go
  • storage.go
  • storage_service_provider.go

Jump to

Keyboard shortcuts

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