4.3 KiB
4.3 KiB
cos-go-sdk-v5
腾讯云对象存储服务 COS(Cloud Object Storage) Go SDK(API 版本:V5 版本的 XML API)。
Install
go get -u github.com/tencentyun/cos-go-sdk-v5
Usage
package main
import (
"context"
"fmt"
"io/ioutil"
"net/http"
"net/url"
"os"
"time"
"github.com/tencentyun/cos-go-sdk-v5"
)
func main() {
//将<bucket>和<region>修改为真实的信息
//bucket的命名规则为{name}-{appid} ,此处填写的存储桶名称必须为此格式
u, _ := url.Parse("https://<bucket>.cos.<region>.myqcloud.com")
b := &cos.BaseURL{BucketURL: u}
c := cos.NewClient(b, &http.Client{
//设置超时时间
Timeout: 100 * time.Second,
Transport: &cos.AuthorizationTransport{
//如实填写账号和密钥,也可以设置为环境变量
SecretID: os.Getenv("COS_SECRETID"),
SecretKey: os.Getenv("COS_SECRETKEY"),
},
})
name := "test/hello.txt"
resp, err := c.Object.Get(context.Background(), name, nil)
if err != nil {
panic(err)
}
bs, _ := ioutil.ReadAll(resp.Body)
resp.Body.Close()
fmt.Printf("%s\n", string(bs))
}
所有的 API 在 example 目录下都有对应的使用示例。
Service API:
- Get Service(使用示例:service/get.go)
Bucket API:
- Get Bucket(使用示例:bucket/get.go)
- Get Bucket ACL(使用示例:bucket/getACL.go)
- Get Bucket CORS(使用示例:bucket/getCORS.go)
- Get Bucket Location(使用示例:bucket/getLocation.go)
- Get Buket Lifecycle(使用示例:bucket/getLifecycle.go)
- Get Bucket Tagging(使用示例:bucket/getTagging.go)
- Put Bucket(使用示例:bucket/put.go)
- Put Bucket ACL(使用示例:bucket/putACL.go)
- Put Bucket CORS(使用示例:bucket/putCORS.go)
- Put Bucket Lifecycle(使用示例:bucket/putLifecycle.go)
- Put Bucket Tagging(使用示例:bucket/putTagging.go)
- Delete Bucket(使用示例:bucket/delete.go)
- Delete Bucket CORS(使用示例:bucket/deleteCORS.go)
- Delete Bucket Lifecycle(使用示例:bucket/deleteLifecycle.go)
- Delete Bucket Tagging(使用示例:bucket/deleteTagging.go)
- Head Bucket(使用示例:bucket/head.go)
- List Multipart Uploads(使用示例:bucket/listMultipartUploads.go)
Object API:
- Get Object(使用示例:object/get.go)
- Get Object ACL(使用示例:object/getACL.go)
- Put Object(使用示例:object/put.go)
- Put Object ACL(使用示例:object/putACL.go)
- Put Object Copy(使用示例:object/copy.go)
- Delete Object(使用示例:object/delete.go)
- Delete Multiple Object(使用示例:object/deleteMultiple.go)
- Head Object(使用示例:object/head.go)
- Options Object(使用示例:object/options.go)
- Initiate Multipart Upload(使用示例:object/initiateMultipartUpload.go)
- Upload Part(使用示例:object/uploadPart.go)
- List Parts(使用示例:object/listParts.go)
- Complete Multipart Upload(使用示例:object/completeMultipartUpload.go)
- Abort Multipart Upload(使用示例:object/abortMultipartUpload.go)
- Mutipart Upload(使用示例:object/MutiUpload.go)