Documentation
¶
Overview ¶
* @Author: kamalyes 501893067@qq.com * @Date: 2026-04-23 00:00:00 * @LastEditors: kamalyes 501893067@qq.com * @LastEditTime: 2026-04-23 00:00:00 * @FilePath: \go-toolbox\pkg\ossx\client.go * @Description: go-toolbox/pkg/ossx 对象存储易用性封装客户端 * * 纯易用性封装,不管理连接生命周期 * 连接由调用方(如 go-rpc-gateway)负责创建和关闭 * 通过 Adapter 适配器模式支持 MinIO / AWS S3 / 阿里云 OSS / 腾讯云 COS / Cloudflare R2 Worker * * Copyright (c) 2026 by kamalyes, All Rights Reserved.
* @Author: kamalyes 501893067@qq.com * @Date: 2026-04-23 00:00:00 * @LastEditors: kamalyes 501893067@qq.com * @LastEditTime: 2026-04-23 00:00:00 * @FilePath: \go-toolbox\pkg\ossx\errors.go * @Description: go-toolbox/pkg/ossx 核心错误定义 * * 定义所有适配器共用的错误变量,适配器可使用 fmt.Errorf("%w: ...") 包装 * * Copyright (c) 2026 by kamalyes, All Rights Reserved.
* @Author: kamalyes 501893067@qq.com * @Date: 2026-04-23 00:00:00 * @LastEditors: kamalyes 501893067@qq.com * @LastEditTime: 2026-04-23 00:00:00 * @FilePath: \go-toolbox\pkg\ossx\helper.go * @Description: go-toolbox/pkg/ossx 公共工具函数,供所有适配器复用 * * Copyright (c) 2026 by kamalyes, All Rights Reserved.
* @Author: kamalyes 501893067@qq.com * @Date: 2026-04-23 00:00:00 * @LastEditors: kamalyes 501893067@qq.com * @LastEditTime: 2026-04-23 00:00:00 * @FilePath: \go-toolbox\pkg\ossx\logger.go * @Description: go-toolbox/pkg/ossx 默认日志器,直接复用 go-toolbox/pkg/logger * * 当调用方未传入日志器时,使用此默认日志器 * * Copyright (c) 2026 by kamalyes, All Rights Reserved.
Index ¶
- Variables
- func DefaultContentType() string
- func HeaderToMap(headers map[string][]string) map[string]string
- func NewDefaultLogger() logger.ILogger
- func ValidatePresignParams(bucketName, objectKey string, expiry time.Duration) error
- type Adapter
- type BucketInfo
- type Client
- func (c *Client) Adapter() Adapter
- func (c *Client) BucketExists(ctx context.Context, bucketName string) (bool, error)
- func (c *Client) DeleteObject(ctx context.Context, bucketName, objectKey string) error
- func (c *Client) GetObject(ctx context.Context, bucketName, objectKey string) (*ObjectInfo, error)
- func (c *Client) GetObjectBlob(ctx context.Context, bucketName, objectKey string) ([]byte, error)
- func (c *Client) IsConnected() bool
- func (c *Client) ListObjects(ctx context.Context, bucketName, prefix string, limit int) ([]ObjectInfo, string, error)
- func (c *Client) PresignedDownloadURL(ctx context.Context, bucketName, objectKey string, expiry time.Duration) (*PresignedResult, error)
- func (c *Client) PresignedUploadURL(ctx context.Context, bucketName, objectKey string, expiry time.Duration) (*PresignedResult, error)
- func (c *Client) Provider() ProviderType
- func (c *Client) PutObject(ctx context.Context, bucketName, objectKey string, data io.Reader, size int64, ...) (*ObjectInfo, error)
- func (c *Client) Stats() map[string]interface{}
- type ObjectInfo
- type PresignedResult
- type ProviderType
Constants ¶
This section is empty.
Variables ¶
var ( ErrNilClient = errors.New("ossx: 客户端为空") // 客户端实例为空 ErrNilAdapter = errors.New("ossx: 适配器为空") // 适配器实例为空 ErrInvalidBucketName = errors.New("ossx: 无效的存储桶名称") // 存储桶名称为空 ErrInvalidObjectKey = errors.New("ossx: 无效的对象键") // 对象键为空 ErrInvalidExpiry = errors.New("ossx: 无效的过期时间") // 预签名过期时间必须大于零 ErrPresignFailed = errors.New("ossx: 预签名操作失败") // 生成预签名URL失败 ErrUploadFailed = errors.New("ossx: 上传操作失败") // 上传对象失败 ErrDownloadFailed = errors.New("ossx: 下载操作失败") // 下载对象失败 ErrDeleteFailed = errors.New("ossx: 删除操作失败") // 删除对象失败 ErrBucketNotFound = errors.New("ossx: 存储桶不存在") // 指定存储桶不存在 ErrObjectNotFound = errors.New("ossx: 对象不存在") // 指定对象不存在 ErrUnsupportedProvider = errors.New("ossx: 不支持的存储提供商") // 当前适配器不支持此操作 )
Functions ¶
func HeaderToMap ¶
HeaderToMap 将 map[string][]string 类型的 HTTP 头/查询参数 转换为 map[string]string,每个键只取第一个值
func NewDefaultLogger ¶
NewDefaultLogger 创建默认日志器 日志级别 INFO,前缀 [OSSX],不显示调用者,启用彩色输出
Types ¶
type Adapter ¶
type Adapter interface {
// Provider 返回存储提供商类型
Provider() ProviderType
// PresignedDownloadURL 生成预签名下载地址
PresignedDownloadURL(ctx context.Context, bucketName, objectKey string, expiry time.Duration) (*PresignedResult, error)
// PresignedUploadURL 生成预签名上传地址
PresignedUploadURL(ctx context.Context, bucketName, objectKey string, expiry time.Duration) (*PresignedResult, error)
// PutObject 上传对象
PutObject(ctx context.Context, bucketName, objectKey string, data io.Reader, size int64, contentType string) (*ObjectInfo, error)
// GetObject 获取对象元信息
GetObject(ctx context.Context, bucketName, objectKey string) (*ObjectInfo, error)
// GetObjectBlob 获取对象二进制内容
GetObjectBlob(ctx context.Context, bucketName, objectKey string) ([]byte, error)
// DeleteObject 删除对象
DeleteObject(ctx context.Context, bucketName, objectKey string) error
// ListObjects 列举对象,返回对象列表和下一页标记
ListObjects(ctx context.Context, bucketName, prefix string, limit int) ([]ObjectInfo, string, error)
// BucketExists 判断存储桶是否存在
BucketExists(ctx context.Context, bucketName string) (bool, error)
// IsConnected 检查底层客户端是否可用
IsConnected() bool
}
Adapter 对象存储适配器接口 各存储提供商实现此接口,通过适配器模式统一操作
type BucketInfo ¶
type BucketInfo struct {
Name string `json:"name"` // 存储桶名称
CreationDate time.Time `json:"creation_date"` // 创建时间
}
BucketInfo 存储桶信息
type Client ¶
type Client struct {
// contains filtered or unexported fields
}
Client go-toolbox/pkg/ossx 客户端,持有 Adapter 实例并委托调用
func New ¶
New 创建 go-toolbox/pkg/ossx 客户端 adapter: 存储适配器实例(如 minio.Adapter、s3adapter.Adapter、r2.Adapter) log: 可选日志记录器,不传则使用默认日志器
func (*Client) BucketExists ¶
BucketExists 判断存储桶是否存在
func (*Client) DeleteObject ¶
DeleteObject 删除指定对象
func (*Client) GetObjectBlob ¶
GetObjectBlob 获取对象二进制内容
func (*Client) ListObjects ¶
func (c *Client) ListObjects(ctx context.Context, bucketName, prefix string, limit int) ([]ObjectInfo, string, error)
ListObjects 列举存储桶中的对象
func (*Client) PresignedDownloadURL ¶
func (c *Client) PresignedDownloadURL(ctx context.Context, bucketName, objectKey string, expiry time.Duration) (*PresignedResult, error)
PresignedDownloadURL 生成预签名下载地址
func (*Client) PresignedUploadURL ¶
func (c *Client) PresignedUploadURL(ctx context.Context, bucketName, objectKey string, expiry time.Duration) (*PresignedResult, error)
PresignedUploadURL 生成预签名上传地址
type ObjectInfo ¶
type ObjectInfo struct {
Key string `json:"key"` // 对象键
Size int64 `json:"size"` // 对象大小(字节)
ETag string `json:"etag"` // 对象 ETag
ContentType string `json:"content_type"` // 内容类型
LastModified time.Time `json:"last_modified"` // 最后修改时间
Metadata map[string]string `json:"metadata"` // 自定义元数据
}
ObjectInfo 对象元信息
type PresignedResult ¶
type PresignedResult struct {
URL string `json:"url"` // 预签名访问地址
Method string `json:"method"` // HTTP 请求方法(GET/PUT)
SignedHeader map[string]string `json:"signed_header"` // 需携带的签名头
}
PresignedResult 预签名URL结果
type ProviderType ¶
type ProviderType string
ProviderType 存储提供商标识
const ( ProviderMinIO ProviderType = "minio" // MinIO 对象存储 ProviderS3 ProviderType = "s3" // AWS S3 及兼容协议存储 ProviderAliyunOSS ProviderType = "aliyun_oss" // 阿里云 OSS ProviderTencentCOS ProviderType = "tencent_cos" // 腾讯云 COS ProviderR2 ProviderType = "r2" // Cloudflare R2 Worker )
Directories
¶
| Path | Synopsis |
|---|---|
|
adapter
|
|
|
minio
* @Author: kamalyes 501893067@qq.com * @Date: 2026-04-23 00:00:00 * @LastEditors: kamalyes 501893067@qq.com * @LastEditTime: 2026-04-23 00:00:00 * @FilePath: \go-toolbox\pkg\ossx\adapter\minio\minio.go * @Description: MinIO 对象存储适配器 * * 基于 minio-go/v7 实现 ossx.Adapter 接口 * 支持:预签名上传/下载、对象 CRUD、桶存在判断 * * Copyright (c) 2026 by kamalyes, All Rights Reserved.
|
* @Author: kamalyes 501893067@qq.com * @Date: 2026-04-23 00:00:00 * @LastEditors: kamalyes 501893067@qq.com * @LastEditTime: 2026-04-23 00:00:00 * @FilePath: \go-toolbox\pkg\ossx\adapter\minio\minio.go * @Description: MinIO 对象存储适配器 * * 基于 minio-go/v7 实现 ossx.Adapter 接口 * 支持:预签名上传/下载、对象 CRUD、桶存在判断 * * Copyright (c) 2026 by kamalyes, All Rights Reserved. |
|
r2
* @Author: kamalyes 501893067@qq.com * @Date: 2026-04-23 00:00:00 * @LastEditors: kamalyes 501893067@qq.com * @LastEditTime: 2026-04-23 00:00:00 * @FilePath: \go-toolbox\pkg\ossx\adapter\r2\r2.go * @Description: Cloudflare R2 Worker 对象存储适配器 * * 通过 Cloudflare Worker 代理访问 R2 存储 * 使用 AES-CBC-PKCS7 加密签名保证安全性 * 仅支持:预签名上传地址、上传对象(其他操作通过 Worker API 扩展) * * Copyright (c) 2026 by kamalyes, All Rights Reserved.
|
* @Author: kamalyes 501893067@qq.com * @Date: 2026-04-23 00:00:00 * @LastEditors: kamalyes 501893067@qq.com * @LastEditTime: 2026-04-23 00:00:00 * @FilePath: \go-toolbox\pkg\ossx\adapter\r2\r2.go * @Description: Cloudflare R2 Worker 对象存储适配器 * * 通过 Cloudflare Worker 代理访问 R2 存储 * 使用 AES-CBC-PKCS7 加密签名保证安全性 * 仅支持:预签名上传地址、上传对象(其他操作通过 Worker API 扩展) * * Copyright (c) 2026 by kamalyes, All Rights Reserved. |