ausheng

Go操作腾讯云COS对象存储的简单使用案例

Golang:用于下载和安装 Go 编译运行环境,请前往 Golang 官网进行下载

安装SDK
go get -u github.com/tencentyun/cos-go-sdk-v5
go get -u github.com/tencentyun/cos-go-sdk-v5
1|3准备腾讯云COS对象存储服务

2|0请求示例

使用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}

3|0创建存储桶

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)
	}
}

4|0查询存储桶列表

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}

5|0上传对象

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)
	}
}

6|0查询对象列表

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

7|0下载对象

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,纵有疾风起,人生不言弃!%    

8|0删除对象

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)
	}
}

ausheng

爱生活,爱编程

AUSHENG – BLOG

不要害羞,保持联系。我们喜欢结识有趣的人,结交新朋友。