go get -u github.com/tencentyun/cos-go-sdk-v5
func NewClient(uri *BaseURL, httpClient *http.Client) *Client
// `BaseURL` indicates the base URL required for accessing APIstype BaseURL struct {// Base URL (excluding `path`) for accessing bucket and object related APIs: https://examplebucket-1250000000.cos.<Region>.myqcloud.comBucketURL *url.URL// Base URL (excluding `path`) for accessing service APIs: https://cos.<Region>.myqcloud.comServiceURL *url.URL// Base URL (excluding `path`) for accessing batch APIs: https://<UIN>.cos-control.<Region>.myqcloud.comBatchURL *url.URL// Base URL (excluding `path`) for accessing CI: https://examplebucket-1250000000.ci.<Region>.myqcloud.comCIURL *url.URL}
Parameter | Description | Type | Required |
BucketURL | Base URL (excluding path ) for accessing bucket and object related APIs | String | Yes |
ServiceURL | Base URL (excluding path ) for accessing service APIs | String | No |
BatchURL | Base URL (excluding path ) for accessing batch APIs | String | No |
CIURL | Base URL (excluding path ) for accessing CI | String | No |
// Replace `examplebucket-1250000000` and `COS_REGION` with the actual information of users// Bucket name in the format of `BucketName-APPID` (`APPID` is required), which can be viewed in the COS console at https://console.tencentcloud.com/cos5/bucket.// `COS_REGION` can be viewed in the COS console at https://console.tencentcloud.com/cos5/bucket/. For more information about regions, visit https://www.tencentcloud.com/document/product/436/6224.u, _ := url.Parse("https://examplebucket-1250000000.cos.COS_REGION.myqcloud.com")// The following calls the `Get Service` API. By default, all regions (service.cos.myqcloud.com) will be queried.su, _ := url.Parse("https://cos.COS_REGION.myqcloud.com")b := &cos.BaseURL{BucketURL: u, ServiceURL: su}// 1. Permanent keyclient := cos.NewClient(b, &http.Client{Transport: &cos.AuthorizationTransport{SecretID: os.Getenv("SECRETID"), // User `SecretId`. We recommend you use a sub-account key and follow the principle of least privilege to reduce risks. For information about how to obtain a sub-account key, visit https://www.tencentcloud.com/document/product/598/32675.SecretKey: os.Getenv("SECRETKEY"), // User `SecretKey`. We recommend you use a sub-account key and follow the principle of least privilege to reduce risks. For information about how to obtain a sub-account key, visit https://www.tencentcloud.com/document/product/598/32675.},})
// Replace `examplebucket-1250000000` and `COS_REGION` with the actual information// Bucket name in the format of `BucketName-APPID` (`APPID` is required), which can be viewed in the COS console at https://console.tencentcloud.com/cos5/bucket.// `COS_REGION` can be viewed in the COS console at https://console.tencentcloud.com/cos5/bucket/. For more information about regions, visit https://www.tencentcloud.com/document/product/436/6224.u, _ := url.Parse("https://examplebucket-1250000000.cos.COS_REGION.myqcloud.com")b := &cos.BaseURL{BucketURL: u}// 2. Temporary keyclient := cos.NewClient(b, &http.Client{Transport: &cos.AuthorizationTransport{// If a temporary key is required, see the instructions at https://www.tencentcloud.com/document/product/436/14048 for generating and using a temporary key.SecretID: "SECRETID",SecretKey: "SECRETKEY",SessionToken: "SECRETTOKEN",},})if client != nil {// Call the COS request}
BaseURL
, you can directly use a custom domain name or global acceleration domain name to access COS.// Use a global acceleration domain name to access COSu, _ := url.Parse("http://<BucketName-APPID>.cos.accelerate.myqcloud.com")b := &cos.BaseURL{BucketURL: u}// 2. Temporary keyclient := cos.NewClient(b, &http.Client{Transport: &cos.AuthorizationTransport{// If a temporary key is required, see the instructions at https://www.tencentcloud.com/document/product/436/14048 for generating and using a temporary key.SecretID: "SECRETID",SecretKey: "SECRETKEY",SessionToken: "SECRETTOKEN",},})
// Replace `examplebucket-1250000000` and `COS_REGION` with the actual information// Bucket name in the format of `BucketName-APPID` (`APPID` is required), which can be viewed in the COS console at https://console.tencentcloud.com/cos5/bucket.// `COS_REGION` can be viewed in the COS console at https://console.tencentcloud.com/cos5/bucket/. For more information about regions, visit https://www.tencentcloud.com/document/product/436/6224.u, _ := url.Parse("https://examplebucket-1250000000.cos.COS_REGION.myqcloud.com")b := &cos.BaseURL{BucketURL: u}// 2. Temporary keyclient := cos.NewClient(b, &http.Client{Transport: &cos.AuthorizationTransport{// If a temporary key is required, see the instructions at https://www.tencentcloud.com/document/product/436/14048 for generating and using a temporary key.SecretID: "SECRETID",SecretKey: "SECRETKEY",SessionToken: "SECRETTOKEN",},})// Disable CRC64 verification.client.Conf.EnableCRC = false
package mainimport ("context""net/http""net/url""os""github.com/tencentyun/cos-go-sdk-v5")func main(){// Replace `examplebucket-1250000000` and `COS_REGION` with the actual information// Bucket name in the format of `BucketName-APPID` (`APPID` is required), which can be viewed in the COS console at https://console.tencentcloud.com/cos5/bucket.// `COS_REGION` can be viewed in the COS console at https://console.tencentcloud.com/cos5/bucket/. For more information about regions, visit https://www.tencentcloud.com/document/product/436/6224.u, _ := url.Parse("https://examplebucket-1250000000.cos.COS_REGION.myqcloud.com")b := &cos.BaseURL{BucketURL: u}c := cos.NewClient(b, &http.Client{Transport: &cos.AuthorizationTransport{SecretID: os.Getenv("SECRETID"), // User `SecretId`. We recommend you use a sub-account key and follow the principle of least privilege to reduce risks. For information about how to obtain a sub-account key, visit https://www.tencentcloud.com/document/product/598/32675.SecretKey: os.Getenv("SECRETKEY"), // User `SecretKey`. We recommend you use a sub-account key and follow the principle of least privilege to reduce risks. For information about how to obtain a sub-account key, visit https://www.tencentcloud.com/document/product/598/32675.},})_, err := c.Bucket.Put(context.Background(), nil)if err != nil{panic(err)}}
package mainimport ("context""fmt""net/http""os""github.com/tencentyun/cos-go-sdk-v5")func main(){c := cos.NewClient(nil, &http.Client{Transport: &cos.AuthorizationTransport{SecretID: os.Getenv("SECRETID"), // User `SecretId`. We recommend you use a sub-account key and follow the principle of least privilege to reduce risks. For information about how to obtain a sub-account key, visit https://www.tencentcloud.com/document/product/598/32675.SecretKey: os.Getenv("SECRETKEY"), // User `SecretKey`. We recommend you use a sub-account key and follow the principle of least privilege to reduce risks. For information about how to obtain a sub-account key, visit https://www.tencentcloud.com/document/product/598/32675.},})s, _, err := c.Service.Get(context.Background())if err != nil{panic(err)}for _, b := range s.Buckets {fmt.Printf("%#v\\n", b)}}
package mainimport ("context""net/http""net/url""os""strings""github.com/tencentyun/cos-go-sdk-v5")func main(){// Replace `examplebucket-1250000000` and `COS_REGION` with the actual information// Bucket name in the format of `BucketName-APPID` (`APPID` is required), which can be viewed in the COS console at https://console.tencentcloud.com/cos5/bucket.// `COS_REGION` can be viewed in the COS console at https://console.tencentcloud.com/cos5/bucket/. For more information about regions, visit https://www.tencentcloud.com/document/product/436/6224.u, _ := url.Parse("https://examplebucket-1250000000.cos.COS_REGION.myqcloud.com")b := &cos.BaseURL{BucketURL: u}c := cos.NewClient(b, &http.Client{Transport: &cos.AuthorizationTransport{SecretID: os.Getenv("SECRETID"), // User `SecretId`. We recommend you use a sub-account key and follow the principle of least privilege to reduce risks. For information about how to obtain a sub-account key, visit https://www.tencentcloud.com/document/product/598/32675.SecretKey: os.Getenv("SECRETKEY"), // User `SecretKey`. We recommend you use a sub-account key and follow the principle of least privilege to reduce risks. For information about how to obtain a sub-account key, visit https://www.tencentcloud.com/document/product/598/32675.},})// An object key is the unique identifier of an object in a bucket// For example, in the access domain name `examplebucket-1250000000.cos.COS_REGION.myqcloud.com/test/objectPut.go`, the object key is `test/objectPut.go`name := "test/objectPut.go"// 1. Upload the object with a string.f := strings.NewReader("test")_, err := c.Object.Put(context.Background(), name, f, nil)if err != nil{panic(err)}// 2. Upload the object with a local file._, err = c.Object.PutFromFile(context.Background(), name, "../test", nil)if err != nil{panic(err)}// 3. Upload the object with a file stream.fd, err := os.Open("./test")if err != nil{panic(err)}defer fd.Close()_, err = c.Object.Put(context.Background(), name, fd, nil)if err != nil{panic(err)}}
package mainimport ("context""fmt""net/http""net/url""os""github.com/tencentyun/cos-go-sdk-v5")func main(){// Replace `examplebucket-1250000000` and `COS_REGION` with the actual information// Bucket name in the format of `BucketName-APPID` (`APPID` is required), which can be viewed in the COS console at https://console.tencentcloud.com/cos5/bucket.// `COS_REGION` can be viewed in the COS console at https://console.tencentcloud.com/cos5/bucket/. For more information about regions, visit https://www.tencentcloud.com/document/product/436/6224.u, _ := url.Parse("https://examplebucket-1250000000.cos.COS_REGION.myqcloud.com")b := &cos.BaseURL{BucketURL: u}c := cos.NewClient(b, &http.Client{Transport: &cos.AuthorizationTransport{SecretID: os.Getenv("SECRETID"), // User `SecretId`. We recommend you use a sub-account key and follow the principle of least privilege to reduce risks. For information about how to obtain a sub-account key, visit https://www.tencentcloud.com/document/product/598/32675.SecretKey: os.Getenv("SECRETKEY"), // User `SecretKey`. We recommend you use a sub-account key and follow the principle of least privilege to reduce risks. For information about how to obtain a sub-account key, visit https://www.tencentcloud.com/document/product/598/32675.},})opt := &cos.BucketGetOptions{Prefix: "test",MaxKeys: 3,}v, _, err := c.Bucket.Get(context.Background(), opt)if err != nil{panic(err)}for _, c := range v.Contents {fmt.Printf("%s, %d\\n", c.Key, c.Size)}}
package mainimport ("context""fmt""io/ioutil""net/http""net/url""os""github.com/tencentyun/cos-go-sdk-v5")func main(){// Replace `examplebucket-1250000000` and `COS_REGION` with the actual information// Bucket name in the format of `BucketName-APPID` (`APPID` is required), which can be viewed in the COS console at https://console.tencentcloud.com/cos5/bucket.// `COS_REGION` can be viewed in the COS console at https://console.tencentcloud.com/cos5/bucket/. For more information about regions, visit https://www.tencentcloud.com/document/product/436/6224.u, _ := url.Parse("https://examplebucket-1250000000.cos.COS_REGION.myqcloud.com")b := &cos.BaseURL{BucketURL: u}c := cos.NewClient(b, &http.Client{Transport: &cos.AuthorizationTransport{SecretID: os.Getenv("SECRETID"), // User `SecretId`. We recommend you use a sub-account key and follow the principle of least privilege to reduce risks. For information about how to obtain a sub-account key, visit https://www.tencentcloud.com/document/product/598/32675.SecretKey: os.Getenv("SECRETKEY"), // User `SecretKey`. We recommend you use a sub-account key and follow the principle of least privilege to reduce risks. For information about how to obtain a sub-account key, visit https://www.tencentcloud.com/document/product/598/32675.},})// 1. Obtain the object through response bodyname := "test/objectPut.go"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))// 2. Download the object to the local file system_, err = c.Object.GetToFile(context.Background(), name, "exampleobject", nil)if err != nil{panic(err)}}
package mainimport ("context""net/http""net/url""os""github.com/tencentyun/cos-go-sdk-v5")func main(){// Replace `examplebucket-1250000000` and `COS_REGION` with the actual information// Bucket name in the format of `BucketName-APPID` (`APPID` is required), which can be viewed in the COS console at https://console.tencentcloud.com/cos5/bucket.// `COS_REGION` can be viewed in the COS console at https://console.tencentcloud.com/cos5/bucket/. For more information about regions, visit https://www.tencentcloud.com/document/product/436/6224.u, _ := url.Parse("https://examplebucket-1250000000.cos.COS_REGION.myqcloud.com")b := &cos.BaseURL{BucketURL: u}c := cos.NewClient(b, &http.Client{Transport: &cos.AuthorizationTransport{SecretID: os.Getenv("SECRETID"), // User `SecretId`. We recommend you use a sub-account key and follow the principle of least privilege to reduce risks. For information about how to obtain a sub-account key, visit https://www.tencentcloud.com/document/product/598/32675.SecretKey: os.Getenv("SECRETKEY"), // User `SecretKey`. We recommend you use a sub-account key and follow the principle of least privilege to reduce risks. For information about how to obtain a sub-account key, visit https://www.tencentcloud.com/document/product/598/32675.},})name := "test/objectPut.go"_, err := c.Object.Delete(context.Background(), name)if err != nil{panic(err)}}
Was this page helpful?