oss

package module
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Jan 12, 2026 License: Apache-2.0 Imports: 24 Imported by: 0

README

framework-oss

Go framework package for oss.

Installation

go get github.com/go-anyway/framework-oss@v1.0.0

Usage

See documentation for usage examples.

License

Apache License 2.0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	// ErrBucketNotExist 存储桶不存在
	ErrBucketNotExist = errors.New("bucket not exist")
	// ErrObjectNotFound 对象不存在
	ErrObjectNotFound = errors.New("object not found")
	// ErrUnauthorized 未授权
	ErrUnauthorized = errors.New("unauthorized")
	// ErrAccessDenied 访问被拒绝
	ErrAccessDenied = errors.New("access denied")
	// ErrInvalidConfig 无效配置
	ErrInvalidConfig = errors.New("invalid config")
	// ErrStoreNotFound 存储适配器未找到
	ErrStoreNotFound = errors.New("store not found")
	// ErrNotSupported 不支持的操作
	ErrNotSupported = errors.New("operation not supported")
)

统一错误定义

Functions

This section is empty.

Types

type AWSConfig

type AWSConfig struct {
	Region          string `yaml:"region" env:"OSS_AWS_REGION" required:"true"`
	AccessKeyID     string `yaml:"access_key_id" env:"OSS_AWS_ACCESS_KEY_ID" required:"true"`
	SecretAccessKey string `yaml:"secret_access_key" env:"OSS_AWS_SECRET_ACCESS_KEY" required:"true"`
	BucketName      string `yaml:"bucket_name" env:"OSS_AWS_BUCKET_NAME" required:"true"`
	Endpoint        string `yaml:"endpoint,omitempty" env:"OSS_AWS_ENDPOINT"` // 可选,用于兼容 S3 的服务
	UseSSL          bool   `yaml:"use_ssl" env:"OSS_AWS_USE_SSL" default:"true"`
}

AWSConfig AWS S3 配置

type AWSStore

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

AWSStore AWS S3 适配器

func (*AWSStore) BucketExists

func (s *AWSStore) BucketExists(ctx context.Context, bucket string) (bool, error)

BucketExists 检查存储桶是否存在

func (*AWSStore) Close

func (s *AWSStore) Close() error

Close 关闭连接

func (*AWSStore) DeleteObject

func (s *AWSStore) DeleteObject(ctx context.Context, bucket, key string) error

DeleteObject 删除对象

func (*AWSStore) GetObject

func (s *AWSStore) GetObject(ctx context.Context, bucket, key string) (io.ReadCloser, error)

GetObject 下载对象

func (*AWSStore) ListObjects

func (s *AWSStore) ListObjects(ctx context.Context, bucket, prefix string, opts ...ListOption) ([]ObjectInfo, error)

ListObjects 列出对象

func (*AWSStore) PresignedGetObject

func (s *AWSStore) PresignedGetObject(ctx context.Context, bucket, key string, expiry time.Duration) (string, error)

PresignedGetObject 生成预签名下载 URL

func (*AWSStore) PresignedPutObject

func (s *AWSStore) PresignedPutObject(ctx context.Context, bucket, key string, expiry time.Duration, contentType string) (string, error)

PresignedPutObject 生成预签名上传 URL

func (*AWSStore) PutObject

func (s *AWSStore) PutObject(ctx context.Context, bucket, key string, data io.Reader, opts ...PutOption) error

PutObject 上传对象

func (*AWSStore) StatObject

func (s *AWSStore) StatObject(ctx context.Context, bucket, key string) (*ObjectInfo, error)

StatObject 获取对象信息

type AliyunConfig

type AliyunConfig struct {
	Endpoint        string `yaml:"endpoint" env:"OSS_ALIYUN_ENDPOINT" required:"true"`
	AccessKeyID     string `yaml:"access_key_id" env:"OSS_ALIYUN_ACCESS_KEY_ID" required:"true"`
	SecretAccessKey string `yaml:"secret_access_key" env:"OSS_ALIYUN_SECRET_ACCESS_KEY" required:"true"`
	BucketName      string `yaml:"bucket_name" env:"OSS_ALIYUN_BUCKET_NAME" required:"true"`
	UseSSL          bool   `yaml:"use_ssl" env:"OSS_ALIYUN_USE_SSL" default:"true"`
}

AliyunConfig 阿里云 OSS 配置

type AliyunStore

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

AliyunStore 阿里云 OSS 适配器

func (*AliyunStore) BucketExists

func (s *AliyunStore) BucketExists(ctx context.Context, bucket string) (bool, error)

BucketExists 检查存储桶是否存在

func (*AliyunStore) Close

func (s *AliyunStore) Close() error

Close 关闭连接

func (*AliyunStore) DeleteObject

func (s *AliyunStore) DeleteObject(ctx context.Context, bucket, key string) error

DeleteObject 删除对象

func (*AliyunStore) GetObject

func (s *AliyunStore) GetObject(ctx context.Context, bucket, key string) (io.ReadCloser, error)

GetObject 下载对象

func (*AliyunStore) ListObjects

func (s *AliyunStore) ListObjects(ctx context.Context, bucket, prefix string, opts ...ListOption) ([]ObjectInfo, error)

ListObjects 列出对象

func (*AliyunStore) PresignedGetObject

func (s *AliyunStore) PresignedGetObject(ctx context.Context, bucket, key string, expiry time.Duration) (string, error)

PresignedGetObject 生成预签名下载 URL

func (*AliyunStore) PresignedPutObject

func (s *AliyunStore) PresignedPutObject(ctx context.Context, bucket, key string, expiry time.Duration, contentType string) (string, error)

PresignedPutObject 生成预签名上传 URL

func (*AliyunStore) PutObject

func (s *AliyunStore) PutObject(ctx context.Context, bucket, key string, data io.Reader, opts ...PutOption) error

PutObject 上传对象

func (*AliyunStore) StatObject

func (s *AliyunStore) StatObject(ctx context.Context, bucket, key string) (*ObjectInfo, error)

StatObject 获取对象信息

type Config

type Config struct {
	// Enabled 是否启用 OSS(必填)
	Enabled bool `yaml:"enabled" env:"OSS_ENABLED" required:"true"`
	// Default 默认使用的厂商名称
	Default string `yaml:"default" env:"OSS_DEFAULT"`
	// Stores 各厂商配置映射
	Stores map[string]StoreConfig `yaml:"stores" json:"stores"`
}

Config 多厂商对象存储配置(用于从配置文件创建)

func (*Config) GetStoreConfig

func (c *Config) GetStoreConfig(name string) (*StoreConfig, error)

GetStoreConfig 获取指定厂商的配置

func (*Config) ToOptions

func (c *Config) ToOptions() (*Options, error)

ToOptions 将 Config 转换为 Options

func (*Config) Validate

func (c *Config) Validate() error

Validate 验证配置

type IObjectStore

type IObjectStore interface {
	// PutObject 上传对象
	PutObject(ctx context.Context, bucket, key string, data io.Reader, opts ...PutOption) error
	// GetObject 下载对象
	GetObject(ctx context.Context, bucket, key string) (io.ReadCloser, error)
	// DeleteObject 删除对象
	DeleteObject(ctx context.Context, bucket, key string) error
	// ListObjects 列出对象
	ListObjects(ctx context.Context, bucket, prefix string, opts ...ListOption) ([]ObjectInfo, error)
	// PresignedGetObject 生成预签名下载 URL
	PresignedGetObject(ctx context.Context, bucket, key string, expiry time.Duration) (string, error)
	// PresignedPutObject 生成预签名上传 URL
	// contentType 用于指定上传时的 Content-Type,如果为空则不在签名中包含 Content-Type
	PresignedPutObject(ctx context.Context, bucket, key string, expiry time.Duration, contentType string) (string, error)
	// BucketExists 检查存储桶是否存在
	BucketExists(ctx context.Context, bucket string) (bool, error)
	// StatObject 获取对象信息
	StatObject(ctx context.Context, bucket, key string) (*ObjectInfo, error)
	// Close 关闭连接
	Close() error
}

IObjectStore 对象存储统一接口

func NewAWSStore

func NewAWSStore(cfg *AWSConfig, enableTrace bool) (IObjectStore, error)

NewAWSStore 创建 AWS S3 适配器

func NewAliyunStore

func NewAliyunStore(cfg *AliyunConfig, enableTrace bool) (IObjectStore, error)

NewAliyunStore 创建阿里云 OSS 适配器

func NewMinIOStore

func NewMinIOStore(cfg *MinIOConfig, enableTrace bool) (IObjectStore, error)

NewMinIOStore 创建 MinIO 适配器

func NewTencentStore

func NewTencentStore(cfg *TencentConfig, enableTrace bool) (IObjectStore, error)

NewTencentStore 创建腾讯云 COS 适配器

type LifecycleManager

type LifecycleManager interface {
	// SetLifecycle 设置生命周期规则
	SetLifecycle(ctx context.Context, bucket string, rules []LifecycleRule) error
}

LifecycleManager 生命周期管理接口(可选扩展)

type LifecycleRule

type LifecycleRule struct {
	ID     string
	Status string
	Prefix string
	Expiry int // 过期天数
}

LifecycleRule 生命周期规则

type ListOption

type ListOption struct {
	MaxKeys int
	Marker  string
}

ListOption 列表选项

type MinIOConfig

type MinIOConfig struct {
	Endpoint        string `yaml:"endpoint" env:"OSS_MINIO_ENDPOINT" required:"true"`
	AccessKeyID     string `yaml:"access_key_id" env:"OSS_MINIO_ACCESS_KEY_ID" required:"true"`
	SecretAccessKey string `yaml:"secret_access_key" env:"OSS_MINIO_SECRET_ACCESS_KEY" required:"true"`
	BucketName      string `yaml:"bucket_name" env:"OSS_MINIO_BUCKET_NAME" required:"true"`
	UseSSL          bool   `yaml:"use_ssl" env:"OSS_MINIO_USE_SSL" default:"false"`
	Region          string `yaml:"region,omitempty" env:"OSS_MINIO_REGION"` // 可选,默认 us-east-1
}

MinIOConfig MinIO 配置

type MinIOStore

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

MinIOStore MinIO 适配器(使用 S3 兼容协议)

func (*MinIOStore) BucketExists

func (s *MinIOStore) BucketExists(ctx context.Context, bucket string) (bool, error)

BucketExists 检查存储桶是否存在

func (*MinIOStore) Close

func (s *MinIOStore) Close() error

Close 关闭连接

func (*MinIOStore) DeleteObject

func (s *MinIOStore) DeleteObject(ctx context.Context, bucket, key string) error

DeleteObject 删除对象

func (*MinIOStore) GetObject

func (s *MinIOStore) GetObject(ctx context.Context, bucket, key string) (io.ReadCloser, error)

GetObject 下载对象

func (*MinIOStore) ListObjects

func (s *MinIOStore) ListObjects(ctx context.Context, bucket, prefix string, opts ...ListOption) ([]ObjectInfo, error)

ListObjects 列出对象

func (*MinIOStore) PresignedGetObject

func (s *MinIOStore) PresignedGetObject(ctx context.Context, bucket, key string, expiry time.Duration) (string, error)

PresignedGetObject 生成预签名下载 URL

func (*MinIOStore) PresignedPutObject

func (s *MinIOStore) PresignedPutObject(ctx context.Context, bucket, key string, expiry time.Duration, contentType string) (string, error)

PresignedPutObject 生成预签名上传 URL

func (*MinIOStore) PutObject

func (s *MinIOStore) PutObject(ctx context.Context, bucket, key string, data io.Reader, opts ...PutOption) error

PutObject 上传对象

func (*MinIOStore) StatObject

func (s *MinIOStore) StatObject(ctx context.Context, bucket, key string) (*ObjectInfo, error)

StatObject 获取对象信息

type MultipartUploader

type MultipartUploader interface {
	// CreateMultipartUpload 创建分块上传
	CreateMultipartUpload(ctx context.Context, bucket, key string) (string, error)
	// UploadPart 上传分块
	UploadPart(ctx context.Context, bucket, key, uploadId string, partNumber int, data io.Reader) (PartInfo, error)
	// CompleteMultipartUpload 完成分块上传
	CompleteMultipartUpload(ctx context.Context, bucket, key, uploadId string, parts []PartInfo) error
}

MultipartUploader 大文件分块上传接口(可选扩展)

type ObjectInfo

type ObjectInfo struct {
	Key          string
	Size         int64
	LastModified time.Time
	ContentType  string
	ETag         string
}

ObjectInfo 对象信息(统一接口,不依赖 AWS SDK 类型)

type Options

type Options struct {
	// Enabled 是否启用 OSS
	Enabled bool
	// Default 默认使用的厂商名称
	Default string
	// Stores 各厂商配置映射
	Stores map[string]StoreOptions
}

Options 统一的 OSS 配置选项(内部使用)

type PartInfo

type PartInfo struct {
	PartNumber int
	ETag       string
}

PartInfo 分块信息

type Provider

type Provider string

Provider 对象存储服务提供商

const (
	// ProviderAWS AWS S3
	ProviderAWS Provider = "aws"
	// ProviderAliyun 阿里云 OSS
	ProviderAliyun Provider = "aliyun"
	// ProviderTencent 腾讯云 COS
	ProviderTencent Provider = "tencent"
	// ProviderMinIO MinIO 服务器
	ProviderMinIO Provider = "minio"
	// ProviderCustom 自定义端点
	ProviderCustom Provider = "custom"
)

type PutOption

type PutOption struct {
	ContentType   string
	ContentLength int64
	Metadata      map[string]string
}

PutOption 上传选项

type StoreConfig

type StoreConfig struct {
	// Enabled 是否启用此存储(必填)
	Enabled bool `yaml:"enabled" env:"OSS_STORE_ENABLED" required:"true"`
	// Type 厂商类型:aliyun, aws, tencent, minio
	Type string `yaml:"type" env:"OSS_STORE_TYPE" required:"true"`
	// Aliyun 阿里云 OSS 配置
	Aliyun *AliyunConfig `yaml:"aliyun,omitempty" json:"aliyun,omitempty"`
	// AWS AWS S3 配置
	AWS *AWSConfig `yaml:"aws,omitempty" json:"aws,omitempty"`
	// Tencent 腾讯云 COS 配置
	Tencent *TencentConfig `yaml:"tencent,omitempty" json:"tencent,omitempty"`
	// MinIO MinIO 配置
	MinIO *MinIOConfig `yaml:"minio,omitempty" json:"minio,omitempty"`
	// EnableTrace 是否启用追踪
	EnableTrace bool `yaml:"enable_trace" env:"OSS_ENABLE_TRACE" default:"true"`
}

StoreConfig 单个厂商的配置

type StoreFactory

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

StoreFactory 存储工厂,管理多个对象存储适配器

func NewFromConfig

func NewFromConfig(cfg *Config) (*StoreFactory, error)

NewFromConfig 从配置创建存储工厂(便捷方法)

func NewStoreFactory

func NewStoreFactory(cfg *Config) (*StoreFactory, error)

NewStoreFactory 创建新的存储工厂

func (*StoreFactory) Close

func (f *StoreFactory) Close() error

Close 关闭所有存储适配器

func (*StoreFactory) Get

func (f *StoreFactory) Get(name string) (IObjectStore, error)

Get 获取指定名称的存储适配器

func (*StoreFactory) GetDefault

func (f *StoreFactory) GetDefault() (IObjectStore, error)

GetDefault 获取默认存储适配器

func (*StoreFactory) List

func (f *StoreFactory) List() []string

List 列出所有已注册的存储适配器名称

func (*StoreFactory) Register

func (f *StoreFactory) Register(name string, store IObjectStore)

Register 注册存储适配器

type StoreOptions

type StoreOptions struct {
	// Enabled 是否启用此存储
	Enabled bool
	// Type 厂商类型
	Type Provider
	// Aliyun 阿里云 OSS 配置
	Aliyun *AliyunConfig
	// AWS AWS S3 配置
	AWS *AWSConfig
	// Tencent 腾讯云 COS 配置
	Tencent *TencentConfig
	// MinIO MinIO 配置
	MinIO *MinIOConfig
	// EnableTrace 是否启用追踪
	EnableTrace bool
}

StoreOptions 单个厂商的配置选项(内部使用)

type TencentConfig

type TencentConfig struct {
	Region     string `yaml:"region" env:"OSS_TENCENT_REGION" required:"true"`
	SecretID   string `yaml:"secret_id" env:"OSS_TENCENT_SECRET_ID" required:"true"`
	SecretKey  string `yaml:"secret_key" env:"OSS_TENCENT_SECRET_KEY" required:"true"`
	BucketName string `yaml:"bucket_name" env:"OSS_TENCENT_BUCKET_NAME" required:"true"`
	UseSSL     bool   `yaml:"use_ssl" env:"OSS_TENCENT_USE_SSL" default:"true"`
}

TencentConfig 腾讯云 COS 配置

type TencentStore

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

TencentStore 腾讯云 COS 适配器

func (*TencentStore) BucketExists

func (s *TencentStore) BucketExists(ctx context.Context, bucket string) (bool, error)

BucketExists 检查存储桶是否存在

func (*TencentStore) Close

func (s *TencentStore) Close() error

Close 关闭连接

func (*TencentStore) DeleteObject

func (s *TencentStore) DeleteObject(ctx context.Context, bucket, key string) error

DeleteObject 删除对象

func (*TencentStore) GetObject

func (s *TencentStore) GetObject(ctx context.Context, bucket, key string) (io.ReadCloser, error)

GetObject 下载对象

func (*TencentStore) ListObjects

func (s *TencentStore) ListObjects(ctx context.Context, bucket, prefix string, opts ...ListOption) ([]ObjectInfo, error)

ListObjects 列出对象

func (*TencentStore) PresignedGetObject

func (s *TencentStore) PresignedGetObject(ctx context.Context, bucket, key string, expiry time.Duration) (string, error)

PresignedGetObject 生成预签名下载 URL

func (*TencentStore) PresignedPutObject

func (s *TencentStore) PresignedPutObject(ctx context.Context, bucket, key string, expiry time.Duration, contentType string) (string, error)

PresignedPutObject 生成预签名上传 URL

func (*TencentStore) PutObject

func (s *TencentStore) PutObject(ctx context.Context, bucket, key string, data io.Reader, opts ...PutOption) error

PutObject 上传对象

func (*TencentStore) StatObject

func (s *TencentStore) StatObject(ctx context.Context, bucket, key string) (*ObjectInfo, error)

StatObject 获取对象信息

Jump to

Keyboard shortcuts

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