Documentation
¶
Overview ¶
Package huawei 提供华为云 OBS 的 Storage + MultipartStorage 实现。
本包位于独立子 module,用户按需引入:
go get github.com/gtkit/gouploader/huawei
使用示例:
import (
"github.com/gtkit/gouploader"
"github.com/gtkit/gouploader/huawei"
)
storage, err := huawei.NewStorage(huawei.Config{
Endpoint: "obs.cn-north-4.myhuaweicloud.com",
AccessKey: "...",
SecretKey: "...",
Bucket: "my-bucket",
})
uploader, _ := gouploader.New(
gouploader.WithStorage(storage),
gouploader.WithMultipartThreshold(100 << 20),
)
实现接口:
- gouploader.Storage —— Save / Get / Delete / Exists
- gouploader.MultipartStorage —— InitMultipart / PresignPart / ListParts / CompleteMultipart / AbortMultipart / ListOrphans
华为 OBS 签名说明: OBS SDK 默认使用 OBS 自家签名算法(与 AWS S3 V2/V4 不同)。 本包使用 SDK 默认签名,与 minio-go 强制 V4 签名相比兼容性更好,尤其是某些 region 只接受 OBS 签名。
Index ¶
- type Config
- type Storage
- func (s *Storage) AbortMultipart(_ context.Context, key, cloudUploadID string) error
- func (s *Storage) Close()
- func (s *Storage) CompleteMultipart(_ context.Context, key, cloudUploadID string, parts []gouploader.PartETag) (*gouploader.CompleteResult, error)
- func (s *Storage) CopyObject(_ context.Context, req gouploader.CopyObjectRequest) (*gouploader.ObjectStat, error)
- func (s *Storage) Delete(_ context.Context, key string) error
- func (s *Storage) DeleteObjects(_ context.Context, keys []string) error
- func (s *Storage) Exists(_ context.Context, key string) (bool, error)
- func (s *Storage) Get(_ context.Context, key string) (io.ReadCloser, error)
- func (s *Storage) GetSignedGetURL(_ context.Context, key string, ttl time.Duration) (string, error)
- func (s *Storage) InitMultipart(_ context.Context, key string, meta gouploader.MultipartMeta) (string, error)
- func (s *Storage) ListOrphans(_ context.Context, olderThan time.Duration) ([]gouploader.OrphanMultipart, error)
- func (s *Storage) ListParts(_ context.Context, key, cloudUploadID string) ([]gouploader.PartInfo, error)
- func (s *Storage) PresignPart(_ context.Context, req gouploader.PresignPartRequest) (string, error)
- func (s *Storage) RawClient() *obs.ObsClient
- func (s *Storage) Save(_ context.Context, key string, reader io.Reader, size int64) error
- func (s *Storage) SaveObject(_ context.Context, req gouploader.SaveObjectRequest) error
- func (s *Storage) SaveObjectWithResult(_ context.Context, req gouploader.SaveObjectRequest) (*gouploader.SaveObjectResult, error)
- func (s *Storage) StatObject(_ context.Context, key string) (*gouploader.ObjectStat, error)
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Config ¶
type Config struct {
// Endpoint 访问端点(如 "obs.cn-north-4.myhuaweicloud.com")。
// 可带 scheme;不带时按 UseSSL 决定。
Endpoint string
// AccessKey / SecretKey 华为云 AK/SK。
AccessKey string
SecretKey string
// SecurityToken 临时凭证时使用(可选)。
SecurityToken string
// Bucket 对象桶名。
Bucket string
// Region 区域(如 "cn-north-4")。部分高级特性签名时需要。
Region string
// UseSSL 是否使用 HTTPS。默认 true(未设置 Endpoint scheme 时生效)。
UseSSL bool
// ConnectTimeout / SocketTimeout 单位秒。<=0 使用 SDK 默认值。
ConnectTimeout int
SocketTimeout int
// CDNHost CDN 加速域名,预签名 URL 使用此域名替换 Endpoint。不影响服务端到 OBS 的通信。
//
// 接受以下三种输入格式(自动 normalize):
// - 纯 host: "static.example.com"
// - 带 scheme: "https://static.example.com"
// - 带 scheme + 多余 path: "https://static.example.com/ignored-path"
CDNHost string
}
Config 华为云 OBS 连接参数。
type Storage ¶
type Storage struct {
// contains filtered or unexported fields
}
Storage 实现 gouploader.Storage + gouploader.MultipartStorage。
func (*Storage) AbortMultipart ¶
AbortMultipart 中止云端 Multipart。幂等:NoSuchUpload 视为成功。
func (*Storage) Close ¶
func (s *Storage) Close()
Close 释放 OBS Client 内部 HTTP 连接池。 可选调用;不调用也会随进程退出释放。
func (*Storage) CompleteMultipart ¶
func (s *Storage) CompleteMultipart(_ context.Context, key, cloudUploadID string, parts []gouploader.PartETag) (*gouploader.CompleteResult, error)
CompleteMultipart 合并分片为最终对象。
func (*Storage) CopyObject ¶ added in v1.5.0
func (s *Storage) CopyObject(_ context.Context, req gouploader.CopyObjectRequest) (*gouploader.ObjectStat, error)
CopyObject 在 OBS 内复制对象。
func (*Storage) DeleteObjects ¶ added in v1.5.0
DeleteObjects 批量删除对象。幂等:不存在的 key 由 OBS 按成功处理。
func (*Storage) GetSignedGetURL ¶ added in v1.4.0
GetSignedGetURL 生成对象的预签名 GET URL。
实现:obs-sdk-go 的 CreateSignedUrl 是本地 HMAC 计算无网络调用, 性能适合在 PublicURLBuilder 中按请求生成。
配置了 CDNHost 时,返回的 URL 会被替换为 CDN 域名(保留 scheme/path/query)。
func (*Storage) InitMultipart ¶
func (s *Storage) InitMultipart(_ context.Context, key string, meta gouploader.MultipartMeta) (string, error)
InitMultipart 创建 Multipart 上传会话,返回 OBS 的 UploadId。
func (*Storage) ListOrphans ¶
func (s *Storage) ListOrphans(_ context.Context, olderThan time.Duration) ([]gouploader.OrphanMultipart, error)
ListOrphans 列出超过 olderThan 时长未完成的悬挂 Multipart。
单次最多返回 1000,通过 (KeyMarker, UploadIdMarker) 翻页。 客户端测时过滤:Initiated < (now - olderThan)。
func (*Storage) ListParts ¶
func (s *Storage) ListParts(_ context.Context, key, cloudUploadID string) ([]gouploader.PartInfo, error)
ListParts 列出云端已上传的 parts。处理分页,返回完整列表。
OBS 单次最多返回 1000 parts,通过 PartNumberMarker 翻页直到 IsTruncated=false。
func (*Storage) PresignPart ¶
func (s *Storage) PresignPart(_ context.Context, req gouploader.PresignPartRequest) (string, error)
PresignPart 为单个分片生成预签名 PUT URL。
锁死参数:
- partNumber / uploadId:签入 URL query
- Content-Length:签入 header 摘要(req.ContentLength>0 时生效)
重要:使用 OBS 自家签名(非 AWS V4)。某些 region 只接受 OBS 签名, 这是 minio-go 通用兼容层失败的根本原因之一。
func (*Storage) RawClient ¶ added in v1.5.0
RawClient returns the underlying Huawei OBS SDK client used by this storage.
func (*Storage) SaveObject ¶ added in v1.5.0
func (s *Storage) SaveObject(_ context.Context, req gouploader.SaveObjectRequest) error
SaveObject 将流式数据和对象元数据一并持久化到 OBS。
func (*Storage) SaveObjectWithResult ¶ added in v1.7.0
func (s *Storage) SaveObjectWithResult(_ context.Context, req gouploader.SaveObjectRequest) (*gouploader.SaveObjectResult, error)
SaveObjectWithResult 将流式数据和对象元数据一并持久化到 OBS,并返回保存结果。
func (*Storage) StatObject ¶ added in v1.5.0
func (s *Storage) StatObject(_ context.Context, key string) (*gouploader.ObjectStat, error)
StatObject 查询对象的通用元数据。