Getting Started

Last updated: 2020-12-23 11:54:42

    Download and Installation

    Relevant resources

    • Download the source code for the COS XML Go SDK here.
    • Download the demo here.
    • For more information, please see COS SDK for Go.
    • Find the complete sample code here.
    • For the SDK changelog, please see Changelog.

    Environmental dependency

    • Golang is used to download and install the Go operating environment; it can be downloaded from the Golang official website.

    Installing SDK

    Execute the following command to install the COS Go SDK:

    go get -u github.com/tencentyun/cos-go-sdk-v5

    Getting Started

    The section below describes how to use the COS Go SDK to perform basic operations, such as initializing a client, creating a bucket, querying a bucket list, uploading an object, querying an object list, downloading an object, and deleting an object..

    Initialization

    A COS GO client is generated using the COS domain name.

    Method prototype

    func NewClient(uri *BaseURL, httpClient *http.Client) *Client

    Sample request

    Using a permanent key:

    // Replace examplebucket-1250000000 and COS_REGION with your information
    u, _ := url.Parse("https://examplebucket-1250000000.cos.COS_REGION.myqcloud.com")
    b := &cos.BaseURL{BucketURL: u}
    // 1. Permanent key
    client := cos.NewClient(b, &http.Client{
        Transport: &cos.AuthorizationTransport{
            SecretID:  "COS_SECRETID",
            SecretKey: "COS_SECRETKEY",
        },
    })

    Using a temporary key:

    // Replace examplebucket-1250000000 and COS_REGION with your information
    u, _ := url.Parse("https://examplebucket-1250000000.cos.COS_REGION.myqcloud.com")
    b := &cos.BaseURL{BucketURL: u}
    // 2. Temporary key
    client := cos.NewClient(b, &http.Client{
        Transport: &cos.AuthorizationTransport{
            SecretID:     "COS_SECRETID",
            SecretKey:    "COS_SECRETKEY",
            SessionToken: "COS_SECRETTOKEN",
        },
    })
    if client != nil {
        // Call the COS request
    }

    Note:

    For more information on how to generate and use a temporary key, please see Generating and Using Temporary Keys.

    Creating bucket

    package main
    
    import(
        "context"
        "net/http"
        "net/url"
        "os"
    
        "github.com/tencentyun/cos-go-sdk-v5"
    )
    
    func main() {
        // Replace examplebucket-1250000000 and COS_REGION with your information
        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:  "COS_SECRETID",
                SecretKey: "COS_SECRETKEY",
            },
        })
    
        _, err := c.Bucket.Put(context.Background(), nil)
        if err != nil {
            panic(err)
        }
    }

    Querying bucket list

    package main
    
    import(
        "context"
        "fmt"
        "net/http"
        "os"
    
        "github.com/tencentyun/cos-go-sdk-v5"
    )
    
    func main() {
        c := cos.NewClient(nil, &http.Client{
            Transport: &cos.AuthorizationTransport{
                SecretID:  "COS_SECRETID",
                SecretKey: "COS_SECRETKEY",
            },
        })
    
        s, _, err := c.Service.Get(context.Background())
        if err != nil {
            panic(err)
        }
    
        for _, b := range s.Buckets {
            fmt.Printf("%#v\n", b)
        }
    }

    Uploading object

    package main
    
    import(
        "context"
        "net/http"
        "net/url"
        "os"
        "strings"
    
        "github.com/tencentyun/cos-go-sdk-v5"
    )
    
    func main() {
        // Replace examplebucket-1250000000 and COS_REGION with your information
        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:  "COS_SECRETID",
                SecretKey: "COS_SECRETKEY",
            },
        })
        // Object key (Key) is the unique identifier of the object in the bucket.
        // For example, in the access domain name `examplebucket-1250000000.cos.ap-guangzhou.myqcloud.com/test/objectPut.go`, the object key is `test/objectPut.go`
        name := "test/objectPut.go"
        // 1. Uploading an object using a key
        f := strings.NewReader("test")
    
        _, err := c.Object.Put(context.Background(), name, f, nil)
        if err != nil {
            panic(err)
        }
        // 2. Uploading an object through local files
        _, err = c.Object.PutFromFile(context.Background(), name, "./test", nil)
        if err != nil {
            panic(err)
        }
    }

    Querying object list

    package main
    
    import(
        "context"
        "fmt"
        "net/http"
        "net/url"
        "os"
    
        "github.com/tencentyun/cos-go-sdk-v5"
    )
    
    func main() {
        // Replace examplebucket-1250000000 and COS_REGION with your information
        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:  "COS_SECRETID",
                SecretKey: "COS_SECRETKEY",
            },
        })
    
        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)
        }
    }

    Downloading object

    package main
    
    import(
        "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 your information
        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:  "COS_SECRETID",
                SecretKey: "COS_SECRETKEY",
            },
        })
        // 1. Getting the object through the response body
        name := "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. Downloading the object to the local file system
        _, err = c.Object.GetToFile(context.Background(), name, "example", nil)
        if err != nil {
            panic(err)
        }
    }

    Deleting object

    package main
    
    import(
        "context"
        "net/http"
        "net/url"
        "os"
    
        "github.com/tencentyun/cos-go-sdk-v5"
    )
    
    func main() {
        // Replace examplebucket-1250000000 and COS_REGION with your information
        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:  "COS_SECRETID",
                SecretKey: "COS_SECRETKEY",
            },
        })
        name := "test/objectPut.go"
        _, err := c.Object.Delete(context.Background(), name)
        if err != nil {
            panic(err)
        }
    }

    Was this page helpful?

    Was this page helpful?

    • Not at all
    • Not very helpful
    • Somewhat helpful
    • Very helpful
    • Extremely helpful
    Send Feedback
    Help