更新时间:2025-08-18 15:54:22
此 Go SDK 基于CDNetworks对象存储(OS)官方API构建。
当前版本:v1.0.1
需满足Go 1.7 或更高版本
执行以下命令安装SDK
go get -u github.com/CDNetworks-Object-Storage/wcs-go-sdk-v2/wos
WosClient(WOS 客户端)是一个用于访问CDNetwork对象存储的Go客户端。它为调用者提供了丰富的接口来与WOS服务交互,管理和操作对象存储上的桶和对象等资源。
要使用WOS Go SDK发起请求,需要先初始化一个WosClient实例,并根据需要调整客户端配置参数。
创建 wosClient
var ak = "*** 请填写您的 Access Key ***"
var sk = "*** 请填写您的 Secret Key ***"
var endpoint = "https://your-endpoint"
var regionName = "your-regionName"
wosClient, err := wos.New(ak, sk, endpoint, wos.WithRegion("your-region"))
if err == nil {
// 使用 wosClient 访问对象存储
// ...
// 关闭 wosClient
wosClient.Close()
} else {
// 从API响应中解析错误码和消息
if wosError, ok := err.(wos.WosError); ok {
fmt.Println(wosError.StatusCode)
fmt.Println(wosError.Code)
fmt.Println(wosError.Message)
} else {
fmt.Println(err)
}
}
创建客户端时,可以通过参数指定不同的客户端配置。例如,将获取响应头的超时时间设置为30秒:
wosClient, err := wos.New(ak, sk, endpoint, wos.WithHeaderTimeout(30))
| 方法 | 说明 | 推荐值 |
|---|---|---|
| WithSignature(signature SignatureType) | 签名鉴权类型。默认采用WOS鉴权(wos.SignatureWos)。也可以配置为aws-v2(wos.SignatureV2)或aws-v4(wos.SignatureV4)。 | N/A |
| WithSslVerifyAndPemCerts(sslVerify bool, pemCerts []byte) | 配置服务器证书校验参数。默认不校验。 | N/A |
| WithHeaderTimeout(headerTimeout int) | 配置获取响应头的超时时间,默认60秒。 | 10, 60 |
| WithMaxConnections(maxIdleConns int) | 配置最大空闲HTTP连接数,默认1000。 | N/A |
| WithConnectTimeout(connectTimeout int) | 配置建立HTTP/HTTPS连接的超时时间(单位:秒),默认60秒。 | 10, 60 |
| WithSocketTimeout(socketTimeout int) | 配置读写数据的超时时间(单位:秒),默认60秒。 | 10, 60 |
| WithIdleConnTimeout(idleConnTimeout int) | 配置连接池中空闲HTTP连接的超时时间(单位:秒),默认30秒。 | 默认 |
| WithMaxRetryCount(maxRetryCount int) | 配置接口请求最大重试次数,默认3次。 | 1, 5 |
| WithProxyUrl(proxyUrl string) | 配置HTTP代理。 | N/A |
| WithHttpTransport(transport *http.Transport) | 配置自定义Transport。 | 默认 |
| WithRequestContext(ctx context.Context) | 配置每个HTTP请求的context。 | N/A |
| WithMaxRedirectCount(maxRedirectCount int) | 配置HTTP/HTTPS请求最大重定向次数,默认3次。 | 1, 5 |
| WithRegion(region string) | 设置S3的区域。 | 请配置控制台空间概览展示的regionName,具体获取方式请参考查询域名 |
| WithPathStyle(pathStyle boolean) | 是否使用path-style还是virtual-hosted-style URL访问服务。关闭时采用bucketName.endpoint访问,开启时采用endpoint/bucketName方式访问。默认关闭。 | N/A |
var ak = "*** 请填写您的 Access Key ***"
var sk = "*** 请填写您的 Secret Key ***"
var endpoint = "https://your-endpoint"
wosClient, _ := wos.New(ak, sk, endpoint, wos.WithRegion("your-region"))
input := &wos.ListBucketsInput{}
input.QueryLocation = true
output, err := wosClient.ListBuckets(input)
if err == nil {
fmt.Printf("StatusCode:%d, RequestId:%s\n", output.StatusCode, output.RequestId)
fmt.Printf("Owner.DisplayName:%s, Owner.ID:%s\n", output.Owner.DisplayName, output.Owner.ID)
for index, val := range output.Buckets {
fmt.Printf("Bucket[%d]-Name:%s,CreationDate:%s,EndPoint:%s,Region:%s\n", index, val.Name, val.CreationDate, val.Endpoint, val.Region)
}
} else {
// 从API响应中解析错误码和消息
if wosError, ok := err.(wos.WosError); ok {
fmt.Println(wosError.StatusCode)
fmt.Println(wosError.Code)
fmt.Println(wosError.Message)
} else {
fmt.Println(err)
}
}
var ak = "*** 请填写您的 Access Key ***"
var sk = "*** 请填写您的 Secret Key ***"
var endpoint = "https://your-endpoint"
wosClient, _ := wos.New(ak, sk, endpoint, wos.WithRegion("your-region"))
input := &wos.ListObjectsInput{}
input.Bucket = bucketName
// input.Prefix = "src/"
output, err := wosClient.ListObjects(input)
if err == nil {
fmt.Printf("StatusCode:%d, RequestId:%s,OwnerId:%s,OwnerName:%s,\n", output.StatusCode, output.RequestId, output.Owner.ID, output.Owner.DisplayName)
for index, val := range output.Contents {
fmt.Printf("Content[%d]-ETag:%s, Key:%s, LastModified:%s, Size:%d, StorageClass:%s\n",
index, val.ETag, val.Key, val.LastModified, val.Size, val.StorageClass)
}
} else {
// 从API响应中解析错误码和消息
if wosError, ok := err.(wos.WosError); ok {
fmt.Println(wosError.StatusCode)
fmt.Println(wosError.Code)
fmt.Println(wosError.Message)
} else {
fmt.Println(err)
}
}
var ak = "*** 请填写您的 Access Key ***"
var sk = "*** 请填写您的 Secret Key ***"
var endpoint = "https://your-endpoint"
wosClient, _ := wos.New(ak, sk, endpoint, wos.WithRegion("your-region"))
input := &wos.PutObjectInput{}
input.Bucket = bucketName
input.Key = objectKey
input.Metadata = map[string]string{"meta": "value"}
input.Body = strings.NewReader("Hello WOS")
output, err := wosClient.PutObject(input)
if err == nil {
fmt.Printf("StatusCode:%d, RequestId:%s\n", output.StatusCode, output.RequestId)
fmt.Printf("ETag:%s", output.ETag)
} else {
// 从API响应中解析错误码和消息
if wosError, ok := err.(wos.WosError); ok {
fmt.Println(wosError.StatusCode)
fmt.Println(wosError.Code)
fmt.Println(wosError.Message)
} else {
fmt.Println(err)
}
}
var ak = "*** 请填写您的 Access Key ***"
var sk = "*** 请填写您的 Secret Key ***"
var endpoint = "https://your-endpoint"
wosClient, _ := wos.New(ak, sk, endpoint, wos.WithRegion("your-region"))
input := &wos.GetObjectInput{}
input.Bucket = bucketName
input.Key = objectKey
output, err := wosClient.GetObject(input)
if err == nil {
defer output.Body.Close()
fmt.Printf("StatusCode:%d, RequestId:%s\n", output.StatusCode, output.RequestId)
fmt.Printf("StorageClass:%s, ETag:%s, ContentType:%s, ContentLength:%d, LastModified:%s\n",
output.StorageClass, output.ETag, output.ContentType, output.ContentLength, output.LastModified)
p := make([]byte, 1024)
var readErr error
var readCount int
for {
readCount, readErr = output.Body.Read(p)
if readCount > 0 {
fmt.Printf("%s", p[:readCount])
}
if readErr != nil {
break
}
}
} else {
// 从API响应中解析错误码和消息
if wosError, ok := err.(wos.WosError); ok {
fmt.Println(wosError.StatusCode)
fmt.Println(wosError.Code)
fmt.Println(wosError.Message)
} else {
fmt.Println(err)
}
}
var ak = "*** 请填写您的 Access Key ***"
var sk = "*** 请填写您的 Secret Key ***"
var endpoint = "https://your-endpoint"
wosClient, _ := wos.New(ak, sk, endpoint, wos.WithRegion("your-region"))
input := &wos.DeleteObjectInput{}
input.Bucket = bucketName
input.Key = objectKey
output, err := wosClient.DeleteObject(input)
if err == nil {
fmt.Printf("StatusCode:%d, RequestId:%s\n", output.StatusCode, output.RequestId)
} else {
// 从API响应中解析错误码和消息
if wosError, ok := err.(wos.WosError); ok {
fmt.Println(wosError.StatusCode)
fmt.Println(wosError.Code)
fmt.Println(wosError.Message)
} else {
fmt.Println(err)
}
}
参见 example/simple_multipart_upload_sample.go
参见 example/multipart_upload_sample.go
详见 wos_go_sample.go 示例
| 功能 | 名称 |
|---|---|
| 列举空间 | ListBuckets |
| 判断空间是否存在 | HeadBucket |
| 设置空间的生命周期 | SetBucketLifecycleConfiguration |
| 获取空间的生命周期 | GetBucketLifecycleConfiguration |
| 删除空间的生命周期 | DeleteBucketLifecycleConfiguration |
| 列举文件 | ListObjects |
| 列举文件v2 | ListObjectV2 |
| 列举分片文件 | ListMultipartUploads |
| 删除对象 | DeleteObject |
| 批量删除对象 | DeleteObjects |
| 取回归档存储对象 | RestoreObject |
| 初始化分段上传任务 | InitiateMultipartUpload |
| 上传分段 | UploadPart |
| 复制分段 | CopyPart |
| 列举已上传分段 | ListParts |
| 取消分段上传任务 | AbortMultipartUpload |
| 上传对象 | PutObject |
| 上传文件 | PutFile |
| 判断对象是否存在 | HeadObject |
| 获取对象元数据 | GetObjectMetadata |
| 下载对象 | GetObject |
| 获取对象avinfo | GetAvinfo |
| 示例内容 | 示例文件 |
|---|---|
| 分段并发复制大对象 | concurrent_copy_part_sample.go |
| 分段并发下载大对象 | concurrent_download_object_sample.go |
| 分段并发上传大对象 | multipart_upload_sample.go |
| 创建文件夹 | create_folder_sample.go |
| 批量删除对象 | delete_objects_sample.go |
| 下载文件 | download_sample.go |
| 列举空间中对象 | list_objects_sample.go |
| 列举文件夹中对象 | list_objects_in_folder_sample.go |
| 对象元数据操作 | object_meta_sample.go |
| 对象简单操作示例 | object_operations_sample.go |
| 简易分片上传 | simple_multipart_upload_sample.go |
| 临时鉴权操作示例 | temporary_signature_sample.go |