目前cos现网支持V4签名,所以之前由于V4签名被卡掉的awssdk现在都可以兼容cos了。
在这里提供一份aws-go-sdk兼容cos的使用指南
如何通过aws的 gosdk调用cos的服务
1、安装go并配置环境
2、下载并安装aws-go-sdk
go get -u github.com/aws/aws-sdk-go
3、通过sdk使用cos具体示例
- 注意!!!:请将
、 、 和 、 分别替换为真实的信息
可能涉及到的库
package main
import (
"bufio"
"fmt"
"os"
"strings"
"bytes"
"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"
)
创建client
myCustomResolver := func(service, region string, optFns ...func(*endpoints.Options)) (endpoints.ResolvedEndpoint, error) {
if service == endpoints.S3ServiceID {
return endpoints.ResolvedEndpoint{
URL: "http://cos..myqcloud.com",
SigningRegion: "",
}, nil
}
return endpoints.DefaultResolver().EndpointFor(service, region, optFns...)
}
creds := credentials.NewStaticCredentials("", "", "")
sess := session.Must(session.NewSession(&aws.Config{
Credentials: creds,
Region: aws.String(""),
EndpointResolver: endpoints.ResolverFunc(myCustomResolver),
}))
bucket := aws.String("-”)
svc := s3.New(sess)
创建bucket
//create bucket
input0 := &s3.CreateBucketInput{
Bucket: bucket,
}
result0, err0 := svc.CreateBucket(input0)
fmt.Println(result0)
fmt.Println(err0)
上传文件(文件大于分块时,使用分块上传)
f, err := os.Open(local_path)
if err != nil {
fmt.Println("failed to open file")os
return
}
upParams := &s3manager.UploadInput{
Bucket: bucket,
Key: aws.String("exampleobject"),
Body: f,
}
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)
上传文件
//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)
下载文件
//get object
input3 := &s3.GetObjectInput{
Bucket: bucket,
Key: aws.String("exampleobject"),
}
result3, err3 := svc.GetObject(input3)
fmt.Println(result3)
fmt.Println(err3)
删除文件
//delete object
input4 := &s3.DeleteObjectInput{
Bucket: bucket,
Key: aws.String("exampleobject"),
}
result4, err4 := svc.DeleteObject(input4)
fmt.Println(result4)
fmt.Println(err4)
删除bucket
//delete bucket
input5 := &s3.DeleteBucketInput{
Bucket: bucket,
}
result5, err5 := svc.DeleteBucket(input5)
fmt.Println(result5)
fmt.Println(err5)
查看bucket
//head bucket
input6 := &s3.HeadBucketInput{
Bucket: bucket,
}
result6, err6 := svc.HeadBucket(input6)
fmt.Println(result6)
fmt.Println(err6)
查看文件
//head object
input7 := &s3.HeadObjectInput{
Bucket: bucket,
Key: aws.String("exampleobject"),
}
result7, err7 := svc.HeadObject(input7)
fmt.Println(result7)
fmt.Println(err7)
查看文件列表
//list objects
input8 := &s3.ListObjectsInput{
Bucket: bucket,
}
result8, err8 := svc.ListObjects(input8)
fmt.Println(result8)
fmt.Println(err8)
更多详细使用请参照awsgosdk s3文档
- https://docs.aws.amazon.com/sdk-for-go/api/service/s3/