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

准备环境

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

posted @ 2020-07-23 18:57  常见-youmen  阅读(2821)  评论(0编辑  收藏  举报