Go操作腾讯云COS工具存储的简朴使用案例

admin 4周前 (09-01) 科技 38 1

准备环境

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

,

Allbet Gmaing

欢迎进入欧博Allbet官网(Allbet Game):www.aLLbetgame.us,欧博官网是欧博集团的官方网站。欧博官网开放Allbet注册、Allbe代理、Allbet电脑客户端、Allbet手机版下载等业务。

皇冠体育声明:该文看法仅代表作者自己,与本平台无关。转载请注明:Go操作腾讯云COS工具存储的简朴使用案例

文章归档

    站点信息

    • 文章总数:493
    • 页面总数:0
    • 分类总数:8
    • 标签总数:895
    • 评论总数:107
    • 浏览总数:7146