欢迎您来到腾讯云!积分商城

腾讯云腾讯云论坛

 找回密码
 立即注册
忘了密码?

扫一扫,访问微社区

快捷导航
搜索
查看: 640|回复: 0

[经验分享] cos 兼容 aws-go-sdk 使用指南

[复制链接]

8

主题

0

好友

380

积分

程序猿[LV2]

Rank: 2Rank: 2

云币
1000
威望
380
发表于 2017-11-16 16:47:41 |显示全部楼层 |未分类
目前 cos 现网支持 V4签名,所以之前由于 V4签名被卡掉的 awssdk 现在都可以兼容 cos 了。
在这里提供一份 aws-go-sdk 兼容 cos 的使用指南

1、安装 go 并配置环境
下载并安装 aws-go-sdk
go get -u github.com/aws/aws-sdk-go

2、通过 sdk 使用 cos 具体示例
可能涉及到的库
package main
import (
    "bufio"
    "fmt"
    "os"
    "strings"
    "github.com/aws/aws-sdk-go/aws"
    "github.com/aws/aws-sdk-go/aws/credentials"
    "github.com/aws/aws-sdk-go/aws/endpoints"
    "github.com/aws/aws-sdk-go/aws/session"
    "github.com/aws/aws-sdk-go/service/s3"
    "github.com/aws/aws-sdk-go/service/s3/s3manager"
)

3、创建 client
myCustomResolver := func(service, region string, optFns ...func(*endpoints.Options)) (endpoints.ResolvedEndpoint, error) {
    if service == endpoints.S3ServiceID {
        return endpoints.ResolvedEndpoint{
            URL:           "http://cn-north.myqcloud.com",
            SigningRegion: "cn-north",
        }, nil
    }
    return endpoints.DefaultResolver().EndpointFor(service, region, optFns...)
}
creds := credentials.NewStaticCredentials("youseceretid", "yoursecretkey", "")
sess := session.Must(session.NewSession(&aws.Config{
    Credentials:      creds,
    Region:           aws.String("cn-north"),
    EndpointResolver: endpoints.ResolverFunc(myCustomResolver),
}))
bucket := aws.String("bucketname-appid”)
svc := s3.New(sess)
注意:请将 youseceretid、yoursecretkey、bucketname 和 appip 分别替换为真实的信息

4、创建 bucket
//create bucket
input0 := &s3.CreateBucketInput{
    Bucket: bucket,
}
result0, err0 := svc.CreateBucket(input0)
fmt.Println(result0)
fmt.Println(err0)

5、上传文件(文件大于分块时,使用分块上传)
//upload
file, err := os.Open("tmp.zip")if err != nil {
    fmt.Println("failed to open")
    return
}
defer file.Close()
data := ""
reader := bufio.NewReader(file)for {
    str, err := reader.ReadString('\n') //每次读取一行
    if err != nil {
        break // 读完或发生错误
    }
    data += str
}
upParams := &s3manager.UploadInput{
    Bucket: bucket,
    Key:    aws.String("exampleobject"),
    Body:   aws.ReadSeekCloser(strings.NewReader(data)),
}
uploader := s3manager.NewUploaderWithClient(svc)
result1, err1 := uploader.Upload(upParams, func(u *s3manager.Uploader) {
    u.PartSize = 1024 * 1024 * 5 // 5MB part size
    u.LeavePartsOnError = true   // Don't delete the parts if the upload fails.
})
fmt.Println(result1)
fmt.Println(err1)

6、上传文件
//put object
input2 := &s3.PutObjectInput{
    Body:   aws.ReadSeekCloser(strings.NewReader("helloworld.go.exe")),
    Bucket: bucket,
    Key:    aws.String("exampleobject"),
}
result2, err2 := svc.PutObject(input2)
fmt.Println(result2)
fmt.Println(err2)

7、下载文件
//get object
input3 := &s3.GetObjectInput{
    Bucket: bucket,
    Key:    aws.String("exampleobject"),
}
result3, err3 := svc.GetObject(input3)
fmt.Println(result3)
fmt.Println(err3)

8、删除文件
//delete object
input4 := &s3.DeleteObjectInput{
    Bucket: bucket,
    Key:    aws.String("exampleobject"),
}
result4, err4 := svc.DeleteObject(input4)
fmt.Println(result4)
fmt.Println(err4)

9、删除 bucket
//delete bucket
input5 := &s3.DeleteBucketInput{
    Bucket: bucket,
}
result5, err5 := svc.DeleteBucket(input5)
fmt.Println(result5)
fmt.Println(err5)

10、查看 bucket
//head bucket
input6 := &s3.HeadBucketInput{
    Bucket: bucket,
}
result6, err6 := svc.HeadBucket(input6)
fmt.Println(result6)
fmt.Println(err6)

11、查看文件
//head object
input7 := &s3.HeadObjectInput{
    Bucket: bucket,
    Key:    aws.String("exampleobject"),
}
result7, err7 := svc.HeadObject(input7)
fmt.Println(result7)
fmt.Println(err7)

12、查看文件列表
//list objects
input8 := &s3.ListObjectsInput{
    Bucket: bucket,
}
result8, err8 := svc.ListObjects(input8)
fmt.Println(result8)
fmt.Println(err8)


您需要登录后才可以回帖 登录 | 立即注册
您需要登录后才可以发帖 QQ登录

联系我们|腾讯云平台|积分商城|腾讯云官方论坛    

GMT+8, 2018-8-16 10:29 , Processed in 1.175362 second(s), 29 queries .

Powered by Discuz! X2.5

© 2001-2012 Comsenz Inc.

回顶部