Documentation
¶
Overview ¶
Package s3 提供文件网关:按 ProviderCategory + Region 缓存 Provider 实例, 通过 ProviderFactory 构建实例,Gateway.Provider 获取客户端执行文件操作。// 用法概要:
gw, _ := s3.NewGateway(myFactory)
gw.StartWatch(ctx)
p, _ := gw.Provider(s3.Fs, s3.Region("/data/uploads"))
p.Save(ctx, "path/to/file", reader)
_ = p.Open(ctx, "path/to/file", func(r io.Reader) error { _, err := io.ReadAll(r); return err })
内置实现:
- ProviderFs:本地文件系统,Region 为根目录
- ProviderS3:S3 兼容对象存储(minio-go 客户端)
可选 Registry 提供参考版 Factory(RegisterFsRoot / RegisterObject + Factory())。
Index ¶
- Variables
- func GenSts()
- func IsObjectCategory(c ProviderCategory) bool
- type Auth
- type BaseProvider
- type Endpoint
- type EndpointTpl
- type Gateway
- type PresignPostResult
- type PresignProvider
- type Provider
- type ProviderCategory
- type ProviderConfig
- type ProviderFactory
- type ProviderFs
- type ProviderS3
- func (p *ProviderS3) Open(ctx context.Context, key string, fn ReadFn) error
- func (p *ProviderS3) PresignGet(ctx context.Context, key string, expiry time.Duration) (string, error)
- func (p *ProviderS3) PresignPost(ctx context.Context, key string, expiry time.Duration) (PresignPostResult, error)
- func (p *ProviderS3) Remove(ctx context.Context, key string) error
- func (p *ProviderS3) Save(ctx context.Context, key string, r io.Reader) (written int64, err error)
- type ProviderWrapper
- type ReadFn
- type Region
- type Registry
- type Sts
Constants ¶
This section is empty.
Variables ¶
var ( ErrProviderNotFound = errors.New("provider not exist") ErrBuilderNil = errors.New("builder is nil") ErrInvalidObjectKey = errors.New("invalid object key") ErrFsRootRequired = errors.New("fs region(root dir) required") ErrFsRootNotAllowed = errors.New("fs root not registered") ErrS3ConfigNotFound = errors.New("s3 provider config not found") ErrS3BucketRequired = errors.New("s3 bucket required") ErrUnsupportedCategory = errors.New("unsupported provider category") ErrReadFnNil = errors.New("read callback is nil") )
var ( // DefaultProviderExpired Provider 缓存过期时间(供 Watch 刷新) DefaultProviderExpired = time.Hour // DefaultWatchInterval 默认轮询间隔 DefaultWatchInterval = 5 * time.Minute )
var SupportedCategory = make(map[ProviderCategory]EndpointTpl)
Functions ¶
func IsObjectCategory ¶
func IsObjectCategory(c ProviderCategory) bool
IsObjectCategory 是否为对象存储类(非本地 fs)。
Types ¶
type BaseProvider ¶
type BaseProvider struct {
// contains filtered or unexported fields
}
func (*BaseProvider) Category ¶
func (b *BaseProvider) Category() ProviderCategory
func (*BaseProvider) Region ¶
func (b *BaseProvider) Region() Region
type EndpointTpl ¶
type EndpointTpl string
EndpointTpl 模板, xxx{region}xxx格式
func (EndpointTpl) Format ¶
func (et EndpointTpl) Format(r Region) (s string)
type Gateway ¶
type Gateway struct {
// contains filtered or unexported fields
}
Gateway 按 category+region 缓存 Provider 实例。
func NewGateway ¶
func NewGateway(factory ProviderFactory) (*Gateway, error)
NewGateway 使用 factory 创建网关。
func (*Gateway) Provider ¶
func (g *Gateway) Provider(category ProviderCategory, region Region) (Provider, error)
Provider 按 category+region 获取 Provider;缓存未命中时调用 Factory 创建并缓存。
func (*Gateway) StartWatch ¶
StartWatch 后台巡检:ProviderWrapper.Expired() 为 true 的实例从缓存移除
type PresignPostResult ¶
type PresignPostResult struct {
UploadURL string `json:"uploadUrl"`
FormData map[string]string `json:"formData"`
}
PresignPostResult COS/S3 POST 表单直传签名结果(供 wx.uploadFile + formData)
type PresignProvider ¶
type PresignProvider interface {
PresignPost(ctx context.Context, key string, expiry time.Duration) (PresignPostResult, error)
PresignGet(ctx context.Context, key string, expiry time.Duration) (string, error)
}
PresignProvider 支持预签名上传/下载的对象存储后端
type Provider ¶
type Provider interface {
Save(ctx context.Context, key string, r io.Reader) (written int64, err error)
Open(ctx context.Context, key string, fn ReadFn) error
Remove(ctx context.Context, key string) error
Category() ProviderCategory
Region() Region
}
Provider 文件网关存储后端(本地 fs 或 S3 兼容对象存储)
type ProviderCategory ¶
type ProviderCategory string
ProviderCategory 供应商类型
const ( Fs ProviderCategory = "fs" // 本地文件系统 S3 ProviderCategory = "s3" // AWS S3 OSS ProviderCategory = "oss" // 阿里云 OSS(S3 兼容) COS ProviderCategory = "cos" // 腾讯云 COS OBS ProviderCategory = "obs" // 华为云 OBS TOS ProviderCategory = "tos" // 火山引擎 TOS JDOS ProviderCategory = "jdos" // 京东云 KODOS ProviderCategory = "kodos" // 七牛云 Kodo BOS ProviderCategory = "bos" // 百度云 BOS )
type ProviderConfig ¶
type ProviderConfig struct {
Auth
// contains filtered or unexported fields
}
ProviderConfig 供应器配置(字段包内可见,通过构造函数创建)
func NewFsProviderConfig ¶
func NewFsProviderConfig(root Region) ProviderConfig
NewFsProviderConfig region 为本地根目录(相对或绝对路径)
func NewObjectProviderConfig ¶
func NewObjectProviderConfig(category ProviderCategory, region Region, bucket string, auth Auth) (ProviderConfig, error)
NewObjectProviderConfig 创建 S3 兼容对象存储配置
func (ProviderConfig) Bucket ¶
func (c ProviderConfig) Bucket() string
func (ProviderConfig) Category ¶
func (c ProviderConfig) Category() ProviderCategory
func (ProviderConfig) Endpoint ¶
func (c ProviderConfig) Endpoint() Endpoint
func (ProviderConfig) Region ¶
func (c ProviderConfig) Region() Region
type ProviderFactory ¶
type ProviderFactory func(category ProviderCategory, region Region) (Provider, error)
ProviderFactory 按 category+region 构建 Provider 实例。
type ProviderFs ¶
type ProviderFs struct {
BaseProvider
}
ProviderFs 文件系统, region就是根目录, 每一个fs类型的实例必须显式配置region(不共享)
func NewProviderFs ¶
func NewProviderFs(root Region) (*ProviderFs, error)
NewProviderFs 创建本地文件系统 provider;region 为根目录,每个实例独立配置不共享
type ProviderS3 ¶
type ProviderS3 struct {
BaseProvider
// contains filtered or unexported fields
}
func NewProviderS3 ¶
func NewProviderS3(cfg ProviderConfig) (*ProviderS3, error)
NewProviderS3 创建 S3 兼容对象存储 provider
func (*ProviderS3) PresignGet ¶
func (*ProviderS3) PresignPost ¶
func (p *ProviderS3) PresignPost(ctx context.Context, key string, expiry time.Duration) (PresignPostResult, error)
type ProviderWrapper ¶
type ProviderWrapper struct {
// contains filtered or unexported fields
}
func (*ProviderWrapper) Expired ¶
func (pw *ProviderWrapper) Expired() bool
func (*ProviderWrapper) RefreshExpired ¶
func (pw *ProviderWrapper) RefreshExpired()
type Registry ¶
type Registry struct {
// contains filtered or unexported fields
}
Registry 供应器配置注册表,供内置 ProviderFactory 使用
func NewRegistry ¶
func NewRegistry() *Registry
func (*Registry) Factory ¶
func (reg *Registry) Factory() ProviderFactory
func (*Registry) RegisterFsRoot ¶
RegisterFsRoot 注册本地 fs 根目录(region);每个根目录须显式注册
func (*Registry) RegisterObject ¶
func (reg *Registry) RegisterObject(cfg ProviderConfig) error
RegisterObject 注册 S3 兼容对象存储配置