Skip to content

Commit c88b738

Browse files
authored
Merge pull request #87 from agin719/common-dev
ACL转换
2 parents d513007 + b0a399e commit c88b738

File tree

6 files changed

+87
-14
lines changed

6 files changed

+87
-14
lines changed

.travis.yml

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,5 @@
11
language: go
22
go:
3-
- '1.7'
4-
- '1.8'
5-
- '1.9'
6-
- 1.10.x
7-
- 1.11.x
8-
- 1.12.x
93
- master
104
sudo: false
115
before_install:

bucket_acl.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import (
66
)
77

88
// BucketGetACLResult is same to the ACLXml
9-
type BucketGetACLResult ACLXml
9+
type BucketGetACLResult = ACLXml
1010

1111
// GetACL 使用API读取Bucket的ACL表,只有所有者有权操作。
1212
//
@@ -20,6 +20,9 @@ func (s *BucketService) GetACL(ctx context.Context) (*BucketGetACLResult, *Respo
2020
result: &res,
2121
}
2222
resp, err := s.client.send(ctx, &sendOpt)
23+
if err == nil {
24+
decodeACL(resp, &res)
25+
}
2326
return &res, resp, err
2427
}
2528

bucket_encryption_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,9 @@ func TestBucketService_GetEncryption(t *testing.T) {
2121
testFormValues(t, r, vs)
2222
fmt.Fprint(w, `<ServerSideEncryptionConfiguration>
2323
<Rule>
24-
<ApplySideEncryptionConfiguration>
24+
<ApplyServerSideEncryptionByDefault>
2525
<SSEAlgorithm>AES256</SSEAlgorithm>
26-
</ApplySideEncryptionConfiguration>
26+
</ApplyServerSideEncryptionByDefault>
2727
</Rule>
2828
</ServerSideEncryptionConfiguration>`)
2929

cos.go

Lines changed: 52 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import (
1111
"net/http"
1212
"net/url"
1313
"reflect"
14+
"strings"
1415
"text/template"
1516

1617
"strconv"
@@ -21,7 +22,7 @@ import (
2122

2223
const (
2324
// Version current go sdk version
24-
Version = "0.7.8"
25+
Version = "0.7.10"
2526
userAgent = "cos-go-sdk-v5/" + Version
2627
contentTypeXML = "application/xml"
2728
defaultServiceBaseURL = "http://service.cos.myqcloud.com"
@@ -355,3 +356,53 @@ type ACLXml struct {
355356
Owner *Owner
356357
AccessControlList []ACLGrant `xml:"AccessControlList>Grant,omitempty"`
357358
}
359+
360+
func decodeACL(resp *Response, res *ACLXml) {
361+
ItemMap := map[string]string{
362+
"ACL": "x-cos-acl",
363+
"READ": "x-cos-grant-read",
364+
"WRITE": "x-cos-grant-write",
365+
"READ_ACP": "x-cos-grant-read-acp",
366+
"WRITE_ACP": "x-cos-grant-write-acp",
367+
"FULL_CONTROL": "x-cos-grant-full-control",
368+
}
369+
publicACL := make(map[string]int)
370+
resACL := make(map[string][]string)
371+
for _, item := range res.AccessControlList {
372+
if item.Grantee == nil {
373+
continue
374+
}
375+
if item.Grantee.ID == "qcs::cam::anyone:anyone" || item.Grantee.URI == "http://cam.qcloud.com/groups/global/AllUsers" {
376+
publicACL[item.Permission] = 1
377+
} else if item.Grantee.ID != res.Owner.ID {
378+
resACL[item.Permission] = append(resACL[item.Permission], "id=\""+item.Grantee.ID+"\"")
379+
}
380+
}
381+
if publicACL["FULL_CONTROL"] == 1 || (publicACL["READ"] == 1 && publicACL["WRITE"] == 1) {
382+
resACL["ACL"] = []string{"public-read-write"}
383+
} else if publicACL["READ"] == 1 {
384+
resACL["ACL"] = []string{"public-read"}
385+
} else {
386+
resACL["ACL"] = []string{"private"}
387+
}
388+
389+
for item, header := range ItemMap {
390+
if len(resp.Header.Get(header)) > 0 || len(resACL[item]) == 0 {
391+
continue
392+
}
393+
resp.Header.Set(header, uniqueGrantID(resACL[item]))
394+
}
395+
}
396+
397+
func uniqueGrantID(grantIDs []string) string {
398+
res := []string{}
399+
filter := make(map[string]int)
400+
for _, id := range grantIDs {
401+
if filter[id] != 0 {
402+
continue
403+
}
404+
filter[id] = 1
405+
res = append(res, id)
406+
}
407+
return strings.Join(res, ",")
408+
}

object.go

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -284,6 +284,7 @@ type ObjectDeleteOptions struct {
284284
XCosSSECustomerKeyMD5 string `header:"x-cos-server-side-encryption-customer-key-MD5,omitempty" url:"-" xml:"-"`
285285
//兼容其他自定义头部
286286
XOptionHeader *http.Header `header:"-,omitempty" url:"-" xml:"-"`
287+
VersionId string `header:"-" url:"VersionId,omitempty" xml:"-"`
287288
}
288289

289290
// Delete Object请求可以将一个文件(Object)删除。
@@ -304,6 +305,7 @@ func (s *ObjectService) Delete(ctx context.Context, name string, opt ...*ObjectD
304305
uri: "/" + encodeURIComponent(name),
305306
method: http.MethodDelete,
306307
optHeader: optHeader,
308+
optQuery: optHeader,
307309
}
308310
resp, err := s.client.send(ctx, &sendOpt)
309311
return resp, err
@@ -440,9 +442,10 @@ type ObjectDeleteMultiResult struct {
440442
XMLName xml.Name `xml:"DeleteResult"`
441443
DeletedObjects []Object `xml:"Deleted,omitempty"`
442444
Errors []struct {
443-
Key string
444-
Code string
445-
Message string
445+
Key string `xml:",omitempty"`
446+
Code string `xml:",omitempty"`
447+
Message string `xml:",omitempty"`
448+
VersionId string `xml:",omitempty"`
446449
} `xml:"Error,omitempty"`
447450
}
448451

@@ -472,6 +475,7 @@ type Object struct {
472475
LastModified string `xml:",omitempty"`
473476
StorageClass string `xml:",omitempty"`
474477
Owner *Owner `xml:",omitempty"`
478+
VersionId string `xml:",omitempty"`
475479
}
476480

477481
// MultiUploadOptions is the option of the multiupload,
@@ -616,6 +620,24 @@ func (s *ObjectService) Upload(ctx context.Context, name string, filepath string
616620
if err != nil {
617621
return nil, nil, err
618622
}
623+
if partNum == 0 {
624+
var opt0 *ObjectPutOptions
625+
if opt.OptIni != nil {
626+
opt0 = &ObjectPutOptions{
627+
opt.OptIni.ACLHeaderOptions,
628+
opt.OptIni.ObjectPutHeaderOptions,
629+
}
630+
}
631+
rsp, err := s.PutFromFile(ctx, name, filepath, opt0)
632+
if err != nil {
633+
return nil, rsp, err
634+
}
635+
result := &CompleteMultipartUploadResult{
636+
Key: name,
637+
ETag: rsp.Header.Get("ETag"),
638+
}
639+
return result, rsp, nil
640+
}
619641

620642
// 2.Init
621643
optini := opt.OptIni

object_acl.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import (
66
)
77

88
// ObjectGetACLResult is the result of GetObjectACL
9-
type ObjectGetACLResult ACLXml
9+
type ObjectGetACLResult = ACLXml
1010

1111
// GetACL Get Object ACL接口实现使用API读取Object的ACL表,只有所有者有权操作。
1212
//
@@ -20,6 +20,9 @@ func (s *ObjectService) GetACL(ctx context.Context, name string) (*ObjectGetACLR
2020
result: &res,
2121
}
2222
resp, err := s.client.send(ctx, &sendOpt)
23+
if err == nil {
24+
decodeACL(resp, &res)
25+
}
2326
return &res, resp, err
2427
}
2528

0 commit comments

Comments
 (0)