storage

package
v0.3.44 Latest Latest
Warning

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

Go to latest
Published: Apr 20, 2026 License: MIT Imports: 12 Imported by: 0

Documentation

Index

Constants

View Source
const (
	TypeLocal  = "local"
	TypeAliyun = "aliyun"
	TypeMinio  = "minio"
)

StorageType 存储类型常量

Variables

This section is empty.

Functions

func InitDefaultStorage

func InitDefaultStorage(config *Config) error

InitDefaultStorage 初始化默认存储

Types

type AliyunOSS

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

AliyunOSS 阿里云OSS存储

func NewAliyunOSS

func NewAliyunOSS(config *Config) (*AliyunOSS, error)

NewAliyunOSS 创建阿里云OSS存储 使用前需要安装: go get github.com/aliyun/aliyun-oss-go-sdk/oss

func (*AliyunOSS) Delete

func (s *AliyunOSS) Delete(path string) error

Delete 删除文件

func (*AliyunOSS) Exists

func (s *AliyunOSS) Exists(path string) (bool, error)

Exists 检查文件是否存在

func (*AliyunOSS) GetInfo

func (s *AliyunOSS) GetInfo(path string) (*FileInfo, error)

GetInfo 获取文件信息

func (*AliyunOSS) GetURL

func (s *AliyunOSS) GetURL(path string, expires time.Duration) (string, error)

GetURL 获取文件访问URL

func (*AliyunOSS) Upload

func (s *AliyunOSS) Upload(path string, file io.Reader, contentType string) (string, error)

Upload 上传文件

func (*AliyunOSS) UploadWithOptions

func (s *AliyunOSS) UploadWithOptions(path string, file io.Reader, opts *UploadOptions) (string, error)

UploadWithOptions 带选项的上传

type Config

type Config struct {
	Type      string `mapstructure:"type"`      // 存储类型:local, aliyun
	Endpoint  string `mapstructure:"endpoint"`  // 端点地址
	Bucket    string `mapstructure:"bucket"`    // 存储桶名称
	AccessKey string `mapstructure:"accessKey"` // 访问密钥ID
	SecretKey string `mapstructure:"secretKey"` // 访问密钥Secret
	Region    string `mapstructure:"region"`    // 区域
	Domain    string `mapstructure:"domain"`    // 自定义域名(CDN)
	IsPrivate bool   `mapstructure:"isPrivate"` // 是否私有(影响URL生成)
	BasePath  string `mapstructure:"basePath"`  // 基础路径
}

Config 存储配置

type FileInfo

type FileInfo struct {
	Path         string    // 文件路径
	Size         int64     // 文件大小(字节)
	ContentType  string    // MIME类型
	LastModified time.Time // 最后修改时间
	ETag         string    // ETag
	URL          string    // 访问URL
}

FileInfo 文件信息

type LocalStorage

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

LocalStorage 本地存储

func NewLocalStorage

func NewLocalStorage(basePath, baseURL string) *LocalStorage

NewLocalStorage 创建本地存储

func (*LocalStorage) Delete

func (s *LocalStorage) Delete(path string) error

Delete 删除文件

func (*LocalStorage) Exists

func (s *LocalStorage) Exists(path string) (bool, error)

Exists 检查文件是否存在

func (*LocalStorage) GetInfo

func (s *LocalStorage) GetInfo(path string) (*FileInfo, error)

GetInfo 获取文件信息

func (*LocalStorage) GetURL

func (s *LocalStorage) GetURL(path string, expires time.Duration) (string, error)

GetURL 获取文件访问URL

func (*LocalStorage) Upload

func (s *LocalStorage) Upload(path string, file io.Reader, contentType string) (string, error)

Upload 上传文件

func (*LocalStorage) UploadWithOptions

func (s *LocalStorage) UploadWithOptions(path string, file io.Reader, opts *UploadOptions) (string, error)

UploadWithOptions 带选项的上传

type MinIoConf added in v0.3.43

type MinIoConf struct {
	MinIOAccessKeyID     string `json:"minIOAccessKeyID"`     //admin
	MinIOAccessSecretKey string `json:"minIOAccessSecretKey"` //MinIOAccessSecretKey
	MinIOEndpoint        string `json:"minIOEndpoint"`        //localhost:9000
	MinIOBucketLocation  string `json:"minIOBucketLocation"`  //cn-north-1
	MinIOSSLBool         bool   `json:"minIOSSLBool"`
	MinIOBucket          string `json:"minIOBucket"` //mymusic
	MinIOBasePath        string `json:"minIoBasePath"`
}

type MinioOSS added in v0.3.43

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

func NewMinioOSS added in v0.3.43

func NewMinioOSS(conf MinIoConf) *MinioOSS

func (*MinioOSS) DeleteFile added in v0.3.43

func (m *MinioOSS) DeleteFile(key string) error

func (*MinioOSS) UploadFile added in v0.3.43

func (m *MinioOSS) UploadFile(file multipart.File, fileHeader *multipart.FileHeader) (string, error)

type Storage

type Storage interface {
	// Upload 上传文件
	// path: 存储路径(如:uploads/2024/01/01/file.jpg)
	// file: 文件内容
	// contentType: 文件MIME类型
	Upload(path string, file io.Reader, contentType string) (string, error)

	// UploadWithOptions 带选项的上传
	UploadWithOptions(path string, file io.Reader, opts *UploadOptions) (string, error)

	// Delete 删除文件
	Delete(path string) error

	// GetURL 获取文件访问URL
	// path: 存储路径
	// expires: 过期时间(0表示永久,>0表示生成临时签名URL)
	GetURL(path string, expires time.Duration) (string, error)

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

	// GetInfo 获取文件信息
	GetInfo(path string) (*FileInfo, error)
}

Storage 对象存储接口(支持多种云服务商)

var DefaultStorage Storage

DefaultStorage 全局默认存储实例

func NewStorage

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

NewStorage 创建存储实例(工厂函数)

type UploadOptions

type UploadOptions struct {
	ContentType        string            // MIME类型
	CacheControl       string            // 缓存控制
	ContentDisposition string            // 内容处置
	Metadata           map[string]string // 自定义元数据
	ACL                string            // 访问控制(public-read, private等)
}

UploadOptions 上传选项

Jump to

Keyboard shortcuts

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