Compare commits
14 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
c88b73871d | ||
|
|
b0a399e92d | ||
|
|
5804e86747 | ||
|
|
cb662cdad5 | ||
|
|
0e9536d989 | ||
|
|
0206a7d026 | ||
|
|
d5130075f0 | ||
|
|
eb4e1ac4c9 | ||
|
|
649bd027d2 | ||
|
|
14683910e1 | ||
|
|
31af2decf4 | ||
|
|
17c5ed144f | ||
|
|
e870e71637 | ||
|
|
f9f617878d |
@@ -1,11 +1,5 @@
|
||||
language: go
|
||||
go:
|
||||
- '1.7'
|
||||
- '1.8'
|
||||
- '1.9'
|
||||
- 1.10.x
|
||||
- 1.11.x
|
||||
- 1.12.x
|
||||
- master
|
||||
sudo: false
|
||||
before_install:
|
||||
|
||||
1
auth.go
1
auth.go
@@ -125,6 +125,7 @@ func newAuthorization(secretID, secretKey string, req *http.Request, authTime *A
|
||||
keyTime := authTime.keyString()
|
||||
signKey := calSignKey(secretKey, keyTime)
|
||||
|
||||
req.Header.Set("Host", req.Host)
|
||||
formatHeaders := *new(string)
|
||||
signedHeaderList := *new([]string)
|
||||
formatHeaders, signedHeaderList = genFormatHeaders(req.Header)
|
||||
|
||||
@@ -6,7 +6,7 @@ import (
|
||||
)
|
||||
|
||||
// BucketGetACLResult is same to the ACLXml
|
||||
type BucketGetACLResult ACLXml
|
||||
type BucketGetACLResult = ACLXml
|
||||
|
||||
// GetACL 使用API读取Bucket的ACL表,只有所有者有权操作。
|
||||
//
|
||||
@@ -20,6 +20,9 @@ func (s *BucketService) GetACL(ctx context.Context) (*BucketGetACLResult, *Respo
|
||||
result: &res,
|
||||
}
|
||||
resp, err := s.client.send(ctx, &sendOpt)
|
||||
if err == nil {
|
||||
decodeACL(resp, &res)
|
||||
}
|
||||
return &res, resp, err
|
||||
}
|
||||
|
||||
|
||||
@@ -12,7 +12,7 @@ type BucketEncryptionConfiguration struct {
|
||||
|
||||
type BucketPutEncryptionOptions struct {
|
||||
XMLName xml.Name `xml:"ServerSideEncryptionConfiguration"`
|
||||
Rule *BucketEncryptionConfiguration `xml:"Rule>ApplySideEncryptionConfiguration"`
|
||||
Rule *BucketEncryptionConfiguration `xml:"Rule>ApplyServerSideEncryptionByDefault"`
|
||||
}
|
||||
|
||||
type BucketGetEncryptionResult BucketPutEncryptionOptions
|
||||
|
||||
@@ -21,9 +21,9 @@ func TestBucketService_GetEncryption(t *testing.T) {
|
||||
testFormValues(t, r, vs)
|
||||
fmt.Fprint(w, `<ServerSideEncryptionConfiguration>
|
||||
<Rule>
|
||||
<ApplySideEncryptionConfiguration>
|
||||
<ApplyServerSideEncryptionByDefault>
|
||||
<SSEAlgorithm>AES256</SSEAlgorithm>
|
||||
</ApplySideEncryptionConfiguration>
|
||||
</ApplyServerSideEncryptionByDefault>
|
||||
</Rule>
|
||||
</ServerSideEncryptionConfiguration>`)
|
||||
|
||||
|
||||
47
bucket_intelligenttiering.go
Normal file
47
bucket_intelligenttiering.go
Normal file
@@ -0,0 +1,47 @@
|
||||
package cos
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/xml"
|
||||
"net/http"
|
||||
)
|
||||
|
||||
type BucketIntelligentTieringTransition struct {
|
||||
Days int `xml:"Days,omitempty"`
|
||||
RequestFrequent int `xml:"RequestFrequent,omitempty"`
|
||||
}
|
||||
|
||||
type BucketPutIntelligentTieringOptions struct {
|
||||
XMLName xml.Name `xml:"IntelligentTieringConfiguration"`
|
||||
Status string `xml:"Status,omitempty"`
|
||||
Transition *BucketIntelligentTieringTransition `xml:"Transition,omitempty"`
|
||||
}
|
||||
|
||||
type BucketGetIntelligentTieringResult BucketPutIntelligentTieringOptions
|
||||
|
||||
func (s *BucketService) PutIntelligentTiering(ctx context.Context, opt *BucketPutIntelligentTieringOptions) (*Response, error) {
|
||||
if opt != nil && opt.Transition != nil {
|
||||
opt.Transition.RequestFrequent = 1
|
||||
}
|
||||
sendOpt := sendOptions{
|
||||
baseURL: s.client.BaseURL.BucketURL,
|
||||
uri: "/?intelligenttiering",
|
||||
method: http.MethodPut,
|
||||
body: opt,
|
||||
}
|
||||
resp, err := s.client.send(ctx, &sendOpt)
|
||||
return resp, err
|
||||
}
|
||||
|
||||
func (s *BucketService) GetIntelligentTiering(ctx context.Context) (*BucketGetIntelligentTieringResult, *Response, error) {
|
||||
var res BucketGetIntelligentTieringResult
|
||||
sendOpt := sendOptions{
|
||||
baseURL: s.client.BaseURL.BucketURL,
|
||||
uri: "/?intelligenttiering",
|
||||
method: http.MethodGet,
|
||||
result: &res,
|
||||
}
|
||||
resp, err := s.client.send(ctx, &sendOpt)
|
||||
return &res, resp, err
|
||||
|
||||
}
|
||||
76
bucket_intelligenttiering_test.go
Normal file
76
bucket_intelligenttiering_test.go
Normal file
@@ -0,0 +1,76 @@
|
||||
package cos
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/xml"
|
||||
"fmt"
|
||||
"net/http"
|
||||
"reflect"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestBucketService_PutIntelligentTiering(t *testing.T) {
|
||||
setup()
|
||||
defer teardown()
|
||||
opt := &BucketPutIntelligentTieringOptions{
|
||||
Status: "Enabled",
|
||||
Transition: &BucketIntelligentTieringTransition{
|
||||
Days: 30,
|
||||
},
|
||||
}
|
||||
mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
|
||||
testMethod(t, r, http.MethodPut)
|
||||
vs := values{
|
||||
"intelligenttiering": "",
|
||||
}
|
||||
testFormValues(t, r, vs)
|
||||
|
||||
body := &BucketPutIntelligentTieringOptions{}
|
||||
xml.NewDecoder(r.Body).Decode(body)
|
||||
want := opt
|
||||
want.XMLName = xml.Name{Local: "IntelligentTieringConfiguration"}
|
||||
if !reflect.DeepEqual(want, body) {
|
||||
t.Fatalf("Bucket.PutIntelligentTiering request\n body: %+v\n, want %+v\n", body, want)
|
||||
}
|
||||
})
|
||||
|
||||
_, err := client.Bucket.PutIntelligentTiering(context.Background(), opt)
|
||||
if err != nil {
|
||||
t.Fatalf("Bucket.PutIntelligentTiering failed, error: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestBucketService_GetIntelligentTiering(t *testing.T) {
|
||||
setup()
|
||||
defer teardown()
|
||||
|
||||
mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
|
||||
testMethod(t, r, http.MethodGet)
|
||||
vs := values{
|
||||
"intelligenttiering": "",
|
||||
}
|
||||
testFormValues(t, r, vs)
|
||||
|
||||
fmt.Fprint(w, `<IntelligentTieringConfiguration>
|
||||
<Status>Enabled</Status>
|
||||
<Transition>
|
||||
<Days>30</Days>
|
||||
</Transition>
|
||||
</IntelligentTieringConfiguration>`)
|
||||
})
|
||||
res, _, err := client.Bucket.GetIntelligentTiering(context.Background())
|
||||
if err != nil {
|
||||
t.Fatalf("Bucket.GetIntelligentTiering failed, error: %v", err)
|
||||
}
|
||||
want := &BucketGetIntelligentTieringResult{
|
||||
XMLName: xml.Name{Local: "IntelligentTieringConfiguration"},
|
||||
Status: "Enabled",
|
||||
Transition: &BucketIntelligentTieringTransition{
|
||||
Days: 30,
|
||||
},
|
||||
}
|
||||
|
||||
if !reflect.DeepEqual(res, want) {
|
||||
t.Errorf("Bucket.GetIntelligentTiering returned\n%+v, want\n%+v", res, want)
|
||||
}
|
||||
}
|
||||
53
cos.go
53
cos.go
@@ -11,6 +11,7 @@ import (
|
||||
"net/http"
|
||||
"net/url"
|
||||
"reflect"
|
||||
"strings"
|
||||
"text/template"
|
||||
|
||||
"strconv"
|
||||
@@ -21,7 +22,7 @@ import (
|
||||
|
||||
const (
|
||||
// Version current go sdk version
|
||||
Version = "0.7.7"
|
||||
Version = "0.7.10"
|
||||
userAgent = "cos-go-sdk-v5/" + Version
|
||||
contentTypeXML = "application/xml"
|
||||
defaultServiceBaseURL = "http://service.cos.myqcloud.com"
|
||||
@@ -355,3 +356,53 @@ type ACLXml struct {
|
||||
Owner *Owner
|
||||
AccessControlList []ACLGrant `xml:"AccessControlList>Grant,omitempty"`
|
||||
}
|
||||
|
||||
func decodeACL(resp *Response, res *ACLXml) {
|
||||
ItemMap := map[string]string{
|
||||
"ACL": "x-cos-acl",
|
||||
"READ": "x-cos-grant-read",
|
||||
"WRITE": "x-cos-grant-write",
|
||||
"READ_ACP": "x-cos-grant-read-acp",
|
||||
"WRITE_ACP": "x-cos-grant-write-acp",
|
||||
"FULL_CONTROL": "x-cos-grant-full-control",
|
||||
}
|
||||
publicACL := make(map[string]int)
|
||||
resACL := make(map[string][]string)
|
||||
for _, item := range res.AccessControlList {
|
||||
if item.Grantee == nil {
|
||||
continue
|
||||
}
|
||||
if item.Grantee.ID == "qcs::cam::anyone:anyone" || item.Grantee.URI == "http://cam.qcloud.com/groups/global/AllUsers" {
|
||||
publicACL[item.Permission] = 1
|
||||
} else if item.Grantee.ID != res.Owner.ID {
|
||||
resACL[item.Permission] = append(resACL[item.Permission], "id=\""+item.Grantee.ID+"\"")
|
||||
}
|
||||
}
|
||||
if publicACL["FULL_CONTROL"] == 1 || (publicACL["READ"] == 1 && publicACL["WRITE"] == 1) {
|
||||
resACL["ACL"] = []string{"public-read-write"}
|
||||
} else if publicACL["READ"] == 1 {
|
||||
resACL["ACL"] = []string{"public-read"}
|
||||
} else {
|
||||
resACL["ACL"] = []string{"private"}
|
||||
}
|
||||
|
||||
for item, header := range ItemMap {
|
||||
if len(resp.Header.Get(header)) > 0 || len(resACL[item]) == 0 {
|
||||
continue
|
||||
}
|
||||
resp.Header.Set(header, uniqueGrantID(resACL[item]))
|
||||
}
|
||||
}
|
||||
|
||||
func uniqueGrantID(grantIDs []string) string {
|
||||
res := []string{}
|
||||
filter := make(map[string]int)
|
||||
for _, id := range grantIDs {
|
||||
if filter[id] != 0 {
|
||||
continue
|
||||
}
|
||||
filter[id] = 1
|
||||
res = append(res, id)
|
||||
}
|
||||
return strings.Join(res, ",")
|
||||
}
|
||||
|
||||
@@ -61,9 +61,9 @@ const (
|
||||
kRepRegion = "ap-chengdu"
|
||||
|
||||
// Batch测试需要的源存储桶和目标存储桶,目前只在成都、重庆地域公测
|
||||
kBatchBucket = "testcd-1259654469"
|
||||
kTargetBatchBucket = "cosgosdkreptest-1259654469" //复用了存储桶
|
||||
kBatchRegion = "ap-chengdu"
|
||||
kBatchBucket = "cosgosdktest-1259654469"
|
||||
kTargetBatchBucket = "cosgosdktest-1259654469" //复用了存储桶
|
||||
kBatchRegion = "ap-guangzhou"
|
||||
)
|
||||
|
||||
func (s *CosTestSuite) SetupSuite() {
|
||||
@@ -812,7 +812,7 @@ func (s *CosTestSuite) TestBatch() {
|
||||
assert.Equal(s.T(), res3.Priority, 3, "priority not right")
|
||||
|
||||
// 等待状态变成Suspended
|
||||
for i := 0; i < 10; i = i + 1 {
|
||||
for i := 0; i < 50; i = i + 1 {
|
||||
res, _, err := client.Batch.DescribeJob(context.Background(), jobid, headers)
|
||||
assert.Nil(s.T(), err, "describe job Failed")
|
||||
assert.Equal(s.T(), res2.Job.ConfirmationRequired, "true", "ConfirmationRequired not right")
|
||||
|
||||
64
example/bucket/intelligenttiering.go
Normal file
64
example/bucket/intelligenttiering.go
Normal file
@@ -0,0 +1,64 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"net/http"
|
||||
"net/url"
|
||||
"os"
|
||||
|
||||
"github.com/tencentyun/cos-go-sdk-v5"
|
||||
"github.com/tencentyun/cos-go-sdk-v5/debug"
|
||||
)
|
||||
|
||||
func log_status(err error) {
|
||||
if err == nil {
|
||||
return
|
||||
}
|
||||
if cos.IsNotFoundError(err) {
|
||||
// WARN
|
||||
fmt.Println("Resource is not existed")
|
||||
} else if e, ok := cos.IsCOSError(err); ok {
|
||||
fmt.Printf("Code: %v\n", e.Code)
|
||||
fmt.Printf("Message: %v\n", e.Message)
|
||||
fmt.Printf("Resource: %v\n", e.Resource)
|
||||
fmt.Printf("RequestId: %v\n", e.RequestID)
|
||||
// ERROR
|
||||
} else {
|
||||
fmt.Println(err)
|
||||
// ERROR
|
||||
}
|
||||
}
|
||||
|
||||
func main() {
|
||||
u, _ := url.Parse("https://test-1259654469.cos.ap-guangzhou.myqcloud.com")
|
||||
b := &cos.BaseURL{
|
||||
BucketURL: u,
|
||||
}
|
||||
c := cos.NewClient(b, &http.Client{
|
||||
Transport: &cos.AuthorizationTransport{
|
||||
SecretID: os.Getenv("COS_SECRETID"),
|
||||
SecretKey: os.Getenv("COS_SECRETKEY"),
|
||||
Transport: &debug.DebugRequestTransport{
|
||||
RequestHeader: true,
|
||||
RequestBody: false,
|
||||
ResponseHeader: true,
|
||||
ResponseBody: false,
|
||||
},
|
||||
},
|
||||
})
|
||||
|
||||
opt := &cos.BucketPutIntelligentTieringOptions {
|
||||
Status: "Enabled",
|
||||
Transition: &cos.BucketIntelligentTieringTransition {
|
||||
Days: 30,
|
||||
},
|
||||
}
|
||||
_, err := c.Bucket.PutIntelligentTiering(context.Background(), opt)
|
||||
log_status(err)
|
||||
res, _, err := c.Bucket.GetIntelligentTiering(context.Background())
|
||||
log_status(err)
|
||||
fmt.Printf("%+v\n", res)
|
||||
fmt.Printf("%+v\n", res.Status)
|
||||
fmt.Printf("%+v\n", res.Transition.Days)
|
||||
}
|
||||
37
object.go
37
object.go
@@ -10,6 +10,7 @@ import (
|
||||
"net/url"
|
||||
"os"
|
||||
"sort"
|
||||
"strings"
|
||||
"time"
|
||||
)
|
||||
|
||||
@@ -236,11 +237,15 @@ type ObjectCopyResult struct {
|
||||
//
|
||||
// https://cloud.tencent.com/document/product/436/10881
|
||||
func (s *ObjectService) Copy(ctx context.Context, name, sourceURL string, opt *ObjectCopyOptions, id ...string) (*ObjectCopyResult, *Response, error) {
|
||||
surl := strings.SplitN(sourceURL, "/", 2)
|
||||
if len(surl) < 2 {
|
||||
return nil, nil, errors.New(fmt.Sprintf("x-cos-copy-source format error: %s", sourceURL))
|
||||
}
|
||||
var u string
|
||||
if len(id) == 1 {
|
||||
u = fmt.Sprintf("%s?versionId=%s", encodeURIComponent(sourceURL), id[0])
|
||||
u = fmt.Sprintf("%s/%s?versionId=%s", surl[0], encodeURIComponent(surl[1]), id[0])
|
||||
} else if len(id) == 0 {
|
||||
u = encodeURIComponent(sourceURL)
|
||||
u = fmt.Sprintf("%s/%s", surl[0], encodeURIComponent(surl[1]))
|
||||
} else {
|
||||
return nil, nil, errors.New("wrong params")
|
||||
}
|
||||
@@ -279,6 +284,7 @@ type ObjectDeleteOptions struct {
|
||||
XCosSSECustomerKeyMD5 string `header:"x-cos-server-side-encryption-customer-key-MD5,omitempty" url:"-" xml:"-"`
|
||||
//兼容其他自定义头部
|
||||
XOptionHeader *http.Header `header:"-,omitempty" url:"-" xml:"-"`
|
||||
VersionId string `header:"-" url:"VersionId,omitempty" xml:"-"`
|
||||
}
|
||||
|
||||
// Delete Object请求可以将一个文件(Object)删除。
|
||||
@@ -299,6 +305,7 @@ func (s *ObjectService) Delete(ctx context.Context, name string, opt ...*ObjectD
|
||||
uri: "/" + encodeURIComponent(name),
|
||||
method: http.MethodDelete,
|
||||
optHeader: optHeader,
|
||||
optQuery: optHeader,
|
||||
}
|
||||
resp, err := s.client.send(ctx, &sendOpt)
|
||||
return resp, err
|
||||
@@ -435,9 +442,10 @@ type ObjectDeleteMultiResult struct {
|
||||
XMLName xml.Name `xml:"DeleteResult"`
|
||||
DeletedObjects []Object `xml:"Deleted,omitempty"`
|
||||
Errors []struct {
|
||||
Key string
|
||||
Code string
|
||||
Message string
|
||||
Key string `xml:",omitempty"`
|
||||
Code string `xml:",omitempty"`
|
||||
Message string `xml:",omitempty"`
|
||||
VersionId string `xml:",omitempty"`
|
||||
} `xml:"Error,omitempty"`
|
||||
}
|
||||
|
||||
@@ -467,6 +475,7 @@ type Object struct {
|
||||
LastModified string `xml:",omitempty"`
|
||||
StorageClass string `xml:",omitempty"`
|
||||
Owner *Owner `xml:",omitempty"`
|
||||
VersionId string `xml:",omitempty"`
|
||||
}
|
||||
|
||||
// MultiUploadOptions is the option of the multiupload,
|
||||
@@ -611,6 +620,24 @@ func (s *ObjectService) Upload(ctx context.Context, name string, filepath string
|
||||
if err != nil {
|
||||
return nil, nil, err
|
||||
}
|
||||
if partNum == 0 {
|
||||
var opt0 *ObjectPutOptions
|
||||
if opt.OptIni != nil {
|
||||
opt0 = &ObjectPutOptions{
|
||||
opt.OptIni.ACLHeaderOptions,
|
||||
opt.OptIni.ObjectPutHeaderOptions,
|
||||
}
|
||||
}
|
||||
rsp, err := s.PutFromFile(ctx, name, filepath, opt0)
|
||||
if err != nil {
|
||||
return nil, rsp, err
|
||||
}
|
||||
result := &CompleteMultipartUploadResult{
|
||||
Key: name,
|
||||
ETag: rsp.Header.Get("ETag"),
|
||||
}
|
||||
return result, rsp, nil
|
||||
}
|
||||
|
||||
// 2.Init
|
||||
optini := opt.OptIni
|
||||
|
||||
@@ -6,7 +6,7 @@ import (
|
||||
)
|
||||
|
||||
// ObjectGetACLResult is the result of GetObjectACL
|
||||
type ObjectGetACLResult ACLXml
|
||||
type ObjectGetACLResult = ACLXml
|
||||
|
||||
// GetACL Get Object ACL接口实现使用API读取Object的ACL表,只有所有者有权操作。
|
||||
//
|
||||
@@ -20,6 +20,9 @@ func (s *ObjectService) GetACL(ctx context.Context, name string) (*ObjectGetACLR
|
||||
result: &res,
|
||||
}
|
||||
resp, err := s.client.send(ctx, &sendOpt)
|
||||
if err == nil {
|
||||
decodeACL(resp, &res)
|
||||
}
|
||||
return &res, resp, err
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user