Skip to content

Commit

Permalink
Merge pull request #87 from agin719/common-dev
Browse files Browse the repository at this point in the history
ACL转换
  • Loading branch information
agin719 authored Sep 29, 2020
2 parents d513007 + b0a399e commit c88b738
Show file tree
Hide file tree
Showing 6 changed files with 87 additions and 14 deletions.
6 changes: 0 additions & 6 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -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:
Expand Down
5 changes: 4 additions & 1 deletion bucket_acl.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (
)

// BucketGetACLResult is same to the ACLXml
type BucketGetACLResult ACLXml
type BucketGetACLResult = ACLXml

// GetACL 使用API读取Bucket的ACL表,只有所有者有权操作。
//
Expand All @@ -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
}

Expand Down
4 changes: 2 additions & 2 deletions bucket_encryption_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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>`)

Expand Down
53 changes: 52 additions & 1 deletion cos.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"net/http"
"net/url"
"reflect"
"strings"
"text/template"

"strconv"
Expand All @@ -21,7 +22,7 @@ import (

const (
// Version current go sdk version
Version = "0.7.8"
Version = "0.7.10"
userAgent = "cos-go-sdk-v5/" + Version
contentTypeXML = "application/xml"
defaultServiceBaseURL = "http://service.cos.myqcloud.com"
Expand Down Expand Up @@ -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, ",")
}
28 changes: 25 additions & 3 deletions object.go
Original file line number Diff line number Diff line change
Expand Up @@ -284,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)删除。
Expand All @@ -304,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
Expand Down Expand Up @@ -440,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"`
}

Expand Down Expand Up @@ -472,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,
Expand Down Expand Up @@ -616,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
Expand Down
5 changes: 4 additions & 1 deletion object_acl.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (
)

// ObjectGetACLResult is the result of GetObjectACL
type ObjectGetACLResult ACLXml
type ObjectGetACLResult = ACLXml

// GetACL Get Object ACL接口实现使用API读取Object的ACL表,只有所有者有权操作。
//
Expand All @@ -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
}

Expand Down

0 comments on commit c88b738

Please sign in to comment.