Go操作騰訊雲COS物件儲存的簡單使用案例

men發表於2020-07-23

準備環境

安裝Go環境

Golang:用於下載和安裝 Go 編譯執行環境,請前往 Golang 官網進行下載

安裝SDK
go get -u github.com/tencentyun/cos-go-sdk-v5
準備騰訊雲COS物件儲存服務

新使用者一元錢即可購買騰訊雲六個月50G容量的物件儲存,別說一元錢都沒有?

請求示例

使用COS域名生成 COS GO 客戶端 Client 結構

使用永久金鑰

package main

import (
	"fmt"
	"github.com/tencentyun/cos-go-sdk-v5"
	"net/http"
	"net/url"
)

// 請求示例,使用永久金鑰
func main()  {
	u, _ := url.Parse("cos_url")
	b := &cos.BaseURL{BucketURL: u}
	client := cos.NewClient(b, &http.Client{
		Transport: &cos.AuthorizationTransport{
			SecretID:  secretid,
			SecretKey: secretkey,
		},
	})
	fmt.Println(client)
}

// 輸出
&{0xc00007eea0  cos-go-sdk-v5/0.7.7 0xc00000e380 {0xc00010e2a0} 0xc00010e2d0 0xc00010e2d0 0xc00010e2d0 0xc00010e2d0}

建立儲存桶

package main

import (
	"context"
	"github.com/tencentyun/cos-go-sdk-v5"
	"net/http"
	"net/url"
)

var secretid string = "AKIDe75eCTxIpqhjT9Iyyi5CPMIyNYR0E8gC"
var secretkey string = "mIYYHkJbomVyCB0sSmqeCKHyjW6HCrG2"
var cos_url string ="https://soft-1259174039.cos.ap-beijing.myqcloud.com"

func main()  {
	u, _ := url.Parse("cos_url")
	b := &cos.BaseURL{BucketURL: u}
	c := cos.NewClient(b, &http.Client{
		Transport: &cos.AuthorizationTransport{
			SecretID:  secretid,
			SecretKey: secretkey,
		},
	})
	_,err := c.Bucket.Put(context.Background(),nil)
	if err != nil{
		panic(err)
	}
}

查詢儲存桶列表

package main

import (
	"context"
	"fmt"
	"github.com/tencentyun/cos-go-sdk-v5"
	"net/http"
	"net/url"
)

var secretid string = "AKIDe75eCTxIpqhjT9Iyyi5CPMIyNYR0E8gC"
var secretkey string = "mIYYHkJbomVyCB0sSmqeCKHyjW6HCrG2"
var cos_url string ="https://soft-1259174039.cos.ap-beijing.myqcloud.com"

func main()  {
	u, _ := url.Parse("cos_url")
	b := &cos.BaseURL{BucketURL: u}
	c := cos.NewClient(b, &http.Client{
		Transport: &cos.AuthorizationTransport{
			SecretID:  secretid,
			SecretKey: secretkey,
		},
	})
	s,_,err := c.Service.Get(context.Background())
	if err != nil{
		panic(err)
	}

	for _,b := range s.Buckets {
		fmt.Println("%#v\n",b)
	}
}

// %#v
// {soft-1259174039 ap-beijing 2020-07-23T09:25:40Z}

上傳物件

package main

import (
	"context"
	"github.com/tencentyun/cos-go-sdk-v5"
	"net/http"
	"net/url"
	"strings"
)

func main()  {
	u, _ := url.Parse("cos_url")
	b := &cos.BaseURL{BucketURL: u}
	c := cos.NewClient(b, &http.Client{
		Transport: &cos.AuthorizationTransport{
			SecretID:  "secretid",
			SecretKey: "secretkey",
		},
	})

	name := "test/objectPut.go"
	// 1. 通過字串上傳物件
	f := strings.NewReader("test")
	//
	_,err := c.Object.Put(context.Background(),name,f,nil)
	if err != nil{
		panic(err)
	}

	// 上傳本地檔案
	_, err = c.Object.PutFromFile(context.Background(),name,"/Users/youmen/a/test.txt",nil)
	if err != nil {
		panic(err)
	}
}

查詢物件列表

package main

import (
	"context"
	"fmt"
	"github.com/tencentyun/cos-go-sdk-v5"
	"net/http"
	"net/url"
)

func main()  {
	u, _ := url.Parse("cos_url")
	b := &cos.BaseURL{BucketURL: u}
	c := cos.NewClient(b, &http.Client{
		Transport: &cos.AuthorizationTransport{
			SecretID:  "secretid",
			SecretKey: "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.Println("%s,%d\n",c.Key,c.Size)
	}
}

//%s,%d
// test/objectPut.go 43

下載物件

package main

import (
	"context"
	"fmt"
	"github.com/tencentyun/cos-go-sdk-v5"
	"io/ioutil"
	"net/http"
	"net/url"
)

func main()  {
	u, _ := url.Parse("cos_url")
	b := &cos.BaseURL{BucketURL: u}
	c := cos.NewClient(b, &http.Client{
		Transport: &cos.AuthorizationTransport{
			SecretID:  "secretid",
			SecretKey: "secretkey",
		},
	})

	// 1.通過響應體獲取物件
	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.Println("%s\n",string(bs))

	// 2.獲取物件到本地檔案
	_,err = c.Object.GetToFile(context.Background(),name,"exampleobject",nil)
	if err != nil{
		panic(err)
	}
}



//%s
// HelloWorld,縱有疾風起,人生不言棄!

// 我們去檢視之前上傳的檔案內容
// cat /Users/youmen/a/test.txt
// HelloWorld,縱有疾風起,人生不言棄!%    

刪除物件

package main

import (
	"context"
	"github.com/tencentyun/cos-go-sdk-v5"
	"net/http"
	"net/url"
)

func main()  {
	u, _ := url.Parse("cos_url")
	b := &cos.BaseURL{BucketURL: u}
	c := cos.NewClient(b, &http.Client{
		Transport: &cos.AuthorizationTransport{
			SecretID:  "secretid",
			SecretKey: "secretkey",
		},
	})

	// 刪除物件
	name := "test/objectPut.go"
	_,err := c.Object.Delete(context.Background(),name)
	if err != nil{
		panic(err)
	}
}

相關文章