go get -u github.com/tencentyun/cos-go-sdk-v5
func NewClient(uri *BaseURL, httpClient *http.Client) *Client
// BaseURL The base URL required to access each APItype BaseURL struct {// Base URL for accessing APIs related to bucket and object (excluding the path part): https://examplebucket-1250000000.cos.<Region>.myqcloud.comBucketURL *url.URL// Base URL for accessing service APIs (excluding the path part): https://cos.<Region>.myqcloud.comServiceURL *url.URL// Base URL for accessing Batch APIs (excluding the path part): https://<UIN>.cos-control.<Region>.myqcloud.comBatchURL *url.URL// Base URL for accessing CI (excluding the path part): https://examplebucket-1250000000.ci.<Region>.myqcloud.com.CIURL *url.URL}
Parameter Name | Parameter Description | Type | Required |
BucketURL | Base URL for accessing APIs related to bucket and object (excluding the path part) | string | Yes |
ServiceURL | Base URL for accessing service APIs (excluding the path part) | string | No |
BatchURL | Base URL for accessing Batch APIs (excluding the path part). | string | No |
CIURL | Base URL for accessing CI (excluding the path part). | string | No |
// Replace examplebucket-1250000000 and COS_REGION with your actual information.// Bucket name, composed of bucketname-appid. The appid must be included. You can view the bucket name in the COS console. https://console.tencentcloud.com/cos5/bucket// COS_REGION can be viewed in the console at https://console.tencentcloud.com/cos5/bucket. For details about regions, see https://www.tencentcloud.com/document/product/436/6224?from_cn_redirect=1u, _ := url.Parse("https://examplebucket-1250000000.cos.COS_REGION.myqcloud.com")b := &cos.BaseURL{BucketURL: u}// 2. Temporary Secret Keysclient := cos.NewClient(b, &http.Client{Transport: &cos.AuthorizationTransport{// If you are using temporary secret keys, you need to enter them. For guidelines on generating and using temporary secret keys, see https://www.tencentcloud.com/document/product/436/14048SecretID: "SECRETID",SecretKey: "SECRETKEY",SessionToken: "SECRETTOKEN",},})if client != nil {// Call COS requests.}
// Replace examplebucket-1250000000 and COS_REGION with your actual information.// Bucket name, composed of bucketname-appid. The appid must be included. You can view the bucket name in the COS console. https://console.tencentcloud.com/cos5/bucket// COS_REGION can be viewed in the console at https://console.tencentcloud.com/cos5/bucket. For details about regions, see https://www.tencentcloud.com/document/product/436/6224?from_cn_redirect=1.u, _ := url.Parse("https://examplebucket-1250000000.cos.COS_REGION.myqcloud.com")// Used for Get Service queries, defaults to all regions: service.cos.myqcloud.com.su, _ := url.Parse("https://cos.COS_REGION.myqcloud.com")b := &cos.BaseURL{BucketURL: u, ServiceURL: su}// 1. Permanent Secret Keysclient := cos.NewClient(b, &http.Client{Transport: &cos.AuthorizationTransport{SecretID: os.Getenv("SECRETID"), // User's SecretId. It is recommended to use sub-account keys, with authorization following the principle of least privilege to mitigate usage risks. For how to obtain sub-account keys, refer to https://www.tencentcloud.com/document/product/598/37140?from_cn_redirect=1SecretKey: os.Getenv("SECRETKEY"), // User's SecretKey. It is recommended to use sub-account keys, with authorization following the principle of least privilege to mitigate usage risks. For how to obtain sub-account keys, refer to https://www.tencentcloud.com/document/product/598/37140?from_cn_redirect=1},})
// Use a domain name for global acceleration to access COS.u, _ := url.Parse("http://<BucketName-APPID>.cos.accelerate.myqcloud.com")b := &cos.BaseURL{BucketURL: u}// 2. Temporary Secret Keysclient := cos.NewClient(b, &http.Client{Transport: &cos.AuthorizationTransport{// If you are using temporary secret keys, you need to enter them. For guidelines on generating and using temporary secret keys, see https://www.tencentcloud.com/document/product/436/14048.SecretID: "SECRETID",SecretKey: "SECRETKEY",SessionToken: "SECRETTOKEN",},})
// Replace examplebucket-1250000000 and COS_REGION with your actual information.// Bucket name, composed of bucketname-appid. The appid must be included. You can view the bucket name in the COS console. https://console.tencentcloud.com/cos5/bucket// COS_REGION can be viewed in the console at https://console.tencentcloud.com/cos5/bucket. For details about regions, see https://www.tencentcloud.com/document/product/436/6224?from_cn_redirect=1.u, _ := url.Parse("https://examplebucket-1250000000.cos.COS_REGION.myqcloud.com")b := &cos.BaseURL{BucketURL: u}// 2. Temporary Secret Keysclient := cos.NewClient(b, &http.Client{Transport: &cos.AuthorizationTransport{// If you are using temporary secret keys, you need to enter them. For guidelines on generating and using temporary secret keys, see https://www.tencentcloud.com/document/product/436/14048.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 your actual information.// Bucket name, composed of bucketname-appid. The appid must be included. You can view the bucket name in the COS console. https://console.tencentcloud.com/cos5/bucket// COS_REGION can be viewed in the console at https://console.tencentcloud.com/cos5/bucket. For details about regions, see https://www.tencentcloud.com/document/product/436/6224?from_cn_redirect=1.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's SecretId. It is recommended to use sub-account keys, with authorization following the principle of least privilege to mitigate usage risks. For how to obtain sub-account keys, refer to https://www.tencentcloud.com/document/product/598/37140?from_cn_redirect=1SecretKey: os.Getenv("SECRETKEY"), // User's SecretKey. It is recommended to use sub-account keys, with authorization following the principle of least privilege to mitigate usage risks. For how to obtain sub-account keys, refer to https://www.tencentcloud.com/document/product/598/37140?from_cn_redirect=1},})_, 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's SecretId. It is recommended to use sub-account keys, with authorization following the principle of least privilege to mitigate usage risks. For how to obtain sub-account keys, refer to https://www.tencentcloud.com/document/product/598/37140?from_cn_redirect=1SecretKey: os.Getenv("SECRETKEY"), // User's SecretKey. It is recommended to use sub-account keys, with authorization following the principle of least privilege to mitigate usage risks. For how to obtain sub-account keys, refer to https://www.tencentcloud.com/document/product/598/37140?from_cn_redirect=1},})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 your actual information.// Bucket name, composed of bucketname-appid. The appid must be included. You can view the bucket name in the COS console. https://console.tencentcloud.com/cos5/bucket// COS_REGION can be viewed in the console at https://console.tencentcloud.com/cos5/bucket. For details about regions, see https://www.tencentcloud.com/document/product/436/6224?from_cn_redirect=1.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's SecretId. It is recommended to use sub-account keys, with authorization following the principle of least privilege to mitigate usage risks. For how to obtain sub-account keys, refer to https://www.tencentcloud.com/document/product/598/37140?from_cn_redirect=1SecretKey: os.Getenv("SECRETKEY"), // User's SecretKey. It is recommended to use sub-account keys, with authorization following the principle of least privilege to mitigate usage risks. For how to obtain sub-account keys, refer to https://www.tencentcloud.com/document/product/598/37140?from_cn_redirect=1},})// The object key is the unique identifier of the object in the bucket.// For example, in the object's 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 an object via a string.f := strings.NewReader("test")_, err := c.Object.Put(context.Background(), name, f, nil)if err != nil {panic(err)}// 2. Upload an object via a local file._, err = c.Object.PutFromFile(context.Background(), name, "../test", nil)if err != nil {panic(err)}// 3. Upload an object via 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 your actual information.// Bucket name, composed of bucketname-appid. The appid must be included. You can view the bucket name in the COS console. https://console.tencentcloud.com/cos5/bucket// COS_REGION can be viewed in the console at https://console.tencentcloud.com/cos5/bucket. For details about regions, see https://www.tencentcloud.com/document/product/436/6224?from_cn_redirect=1.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's SecretId. It is recommended to use sub-account keys, with authorization following the principle of least privilege to mitigate usage risks. For how to obtain sub-account keys, refer to https://www.tencentcloud.com/document/product/598/37140?from_cn_redirect=1.SecretKey: os.Getenv("SECRETKEY"), // User's SecretKey. It is recommended to use sub-account keys, with authorization following the principle of least privilege to mitigate usage risks. For how to obtain sub-account keys, refer to https://www.tencentcloud.com/document/product/598/37140?from_cn_redirect=1.},})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""net/http""net/url""os""github.com/tencentyun/cos-go-sdk-v5")func main() {// Replace examplebucket-1250000000 and COS_REGION with your actual information.// Bucket name, composed of bucketname-appid. The appid must be included. You can view the bucket name in the COS console. https://console.tencentcloud.com/cos5/bucket// COS_REGION can be viewed in the console at https://console.tencentcloud.com/cos5/bucket. For details about regions, see https://www.tencentcloud.com/document/product/436/6224?from_cn_redirect=1.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's SecretId. It is recommended to use sub-account keys, with authorization following the principle of least privilege to mitigate usage risks. For how to obtain sub-account keys, refer to https://www.tencentcloud.com/document/product/598/37140?from_cn_redirect=1.SecretKey: os.Getenv("SECRETKEY"), // User's SecretKey. It is recommended to use sub-account keys, with authorization following the principle of least privilege to mitigate usage risks. For how to obtain sub-account keys, refer to https://www.tencentcloud.com/document/product/598/37140?from_cn_redirect=1.},})// 1. Obtain the object via the response body.name := "test/objectPut.go"resp, err := c.Object.Get(context.Background(), name, nil)if err != nil {panic(err)}bs, _ := io.ReadAll(resp.Body)resp.Body.Close()fmt.Printf("%s\\n", string(bs))// 2. Obtain the object to a local file._, 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 your actual information.// Bucket name, composed of bucketname-appid. The appid must be included. You can view the bucket name in the COS console. https://console.tencentcloud.com/cos5/bucket// COS_REGION can be viewed in the console at https://console.tencentcloud.com/cos5/bucket. For details about regions, see https://www.tencentcloud.com/document/product/436/6224?from_cn_redirect=1.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's SecretId. It is recommended to use sub-account keys, with authorization following the principle of least privilege to mitigate usage risks. For how to obtain sub-account keys, refer to https://www.tencentcloud.com/document/product/598/37140?from_cn_redirect=1.SecretKey: os.Getenv("SECRETKEY"), // User's SecretKey. It is recommended to use sub-account keys, with authorization following the principle of least privilege to mitigate usage risks. For how to obtain sub-account keys, refer to https://www.tencentcloud.com/document/product/598/37140?from_cn_redirect=1.},})name := "test/objectPut.go"_, err := c.Object.Delete(context.Background(), name)if err != nil {panic(err)}}
Feedback