Skip to content

Commit 8be093d

Browse files
authored
Merge pull request #124 from agin719/cos-dev-v5
add ci && download checkpoint
2 parents e8b4da1 + da7dafc commit 8be093d

23 files changed

+1809
-237
lines changed

auth.go

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,12 @@ var needSignHeaders = map[string]bool{
4848
"x-cos-object-type": true,
4949
}
5050

51+
var ciParameters = map[string]bool{
52+
"imagemogr2/": true,
53+
"watermark/": true,
54+
"imageview2/": true,
55+
}
56+
5157
func safeURLEncode(s string) string {
5258
s = encodeURIComponent(s)
5359
s = strings.Replace(s, "!", "%21", -1)
@@ -205,8 +211,10 @@ func genFormatParameters(parameters url.Values) (formatParameters string, signed
205211
for key, values := range parameters {
206212
key = strings.ToLower(key)
207213
for _, value := range values {
208-
ps.Add(key, value)
209-
signedParameterList = append(signedParameterList, key)
214+
if !isCIParameter(key) {
215+
ps.Add(key, value)
216+
signedParameterList = append(signedParameterList, key)
217+
}
210218
}
211219
}
212220
//formatParameters = strings.ToLower(ps.Encode())
@@ -246,6 +254,15 @@ func calHMACDigest(key, msg, signMethod string) []byte {
246254
return h.Sum(nil)
247255
}
248256

257+
func isCIParameter(key string) bool {
258+
for k, v := range ciParameters {
259+
if strings.HasPrefix(key, k) && v {
260+
return true
261+
}
262+
}
263+
return false
264+
}
265+
249266
func isSignHeader(key string) bool {
250267
for k, v := range needSignHeaders {
251268
if key == k && v {

batch.go

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -113,13 +113,6 @@ type BatchCreateJobResult struct {
113113
JobId string `xml:"JobId,omitempty"`
114114
}
115115

116-
func processETag(opt *BatchCreateJobOptions) *BatchCreateJobOptions {
117-
if opt != nil && opt.Manifest != nil && opt.Manifest.Location != nil {
118-
opt.Manifest.Location.ETag = "<ETag>" + opt.Manifest.Location.ETag + "</ETag>"
119-
}
120-
return opt
121-
}
122-
123116
func (s *BatchService) CreateJob(ctx context.Context, opt *BatchCreateJobOptions, headers *BatchRequestHeaders) (*BatchCreateJobResult, *Response, error) {
124117
var res BatchCreateJobResult
125118
sendOpt := sendOptions{

bucket.go

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,19 @@ func (s *BucketService) Get(ctx context.Context, opt *BucketGetOptions) (*Bucket
5050
}
5151

5252
// BucketPutOptions is same to the ACLHeaderOptions
53-
type BucketPutOptions ACLHeaderOptions
53+
type BucketPutOptions struct {
54+
XCosACL string `header:"x-cos-acl,omitempty" url:"-" xml:"-"`
55+
XCosGrantRead string `header:"x-cos-grant-read,omitempty" url:"-" xml:"-"`
56+
XCosGrantWrite string `header:"x-cos-grant-write,omitempty" url:"-" xml:"-"`
57+
XCosGrantFullControl string `header:"x-cos-grant-full-control,omitempty" url:"-" xml:"-"`
58+
XCosGrantReadACP string `header:"x-cos-grant-read-acp,omitempty" url:"-" xml:"-"`
59+
XCosGrantWriteACP string `header:"x-cos-grant-write-acp,omitempty" url:"-" xml:"-"`
60+
CreateBucketConfiguration *CreateBucketConfiguration `header:"-" url:"-" xml:"-"`
61+
}
62+
type CreateBucketConfiguration struct {
63+
XMLName xml.Name `xml:"CreateBucketConfiguration"`
64+
BucketAZConfig string `xml:"BucketAZConfig,omitempty"`
65+
}
5466

5567
// Put Bucket请求可以在指定账号下创建一个Bucket。
5668
//
@@ -62,6 +74,9 @@ func (s *BucketService) Put(ctx context.Context, opt *BucketPutOptions) (*Respon
6274
method: http.MethodPut,
6375
optHeader: opt,
6476
}
77+
if opt != nil && opt.CreateBucketConfiguration != nil {
78+
sendOpt.body = opt.CreateBucketConfiguration
79+
}
6580
resp, err := s.client.send(ctx, &sendOpt)
6681
return resp, err
6782
}

bucket_origin.go

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,11 @@ type BucketPutOriginOptions struct {
1212
}
1313

1414
type BucketOriginRule struct {
15-
OriginType string `xml:"OriginType"`
16-
OriginCondition *BucketOriginCondition `xml:"OriginCondition"`
17-
OriginParameter *BucketOriginParameter `xml:"OriginParameter"`
18-
OriginInfo *BucketOriginInfo `xml:"OriginInfo"`
15+
RulePriority int `xml:"RulePriority,omitempty"`
16+
OriginType string `xml:"OriginType,omitempty"`
17+
OriginCondition *BucketOriginCondition `xml:"OriginCondition,omitempty"`
18+
OriginParameter *BucketOriginParameter `xml:"OriginParameter,omitempty"`
19+
OriginInfo *BucketOriginInfo `xml:"OriginInfo,omitempty"`
1920
}
2021

2122
type BucketOriginCondition struct {

bucket_origin_test.go

Lines changed: 180 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,180 @@
1+
package cos
2+
3+
import (
4+
"context"
5+
"encoding/xml"
6+
"fmt"
7+
"net/http"
8+
"reflect"
9+
"testing"
10+
)
11+
12+
func TestBucketService_GetOrigin(t *testing.T) {
13+
setup()
14+
defer teardown()
15+
16+
mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
17+
testMethod(t, r, "GET")
18+
vs := values{
19+
"origin": "",
20+
}
21+
testFormValues(t, r, vs)
22+
fmt.Fprint(w, `<OriginConfiguration>
23+
<OriginRule>
24+
<RulePriority>1</RulePriority>
25+
<OriginType>Mirror</OriginType>
26+
<OriginCondition>
27+
<HTTPStatusCode>404</HTTPStatusCode>
28+
<Prefix></Prefix>
29+
</OriginCondition>
30+
<OriginParameter>
31+
<Protocol>HTTP</Protocol>
32+
<FollowQueryString>true</FollowQueryString>
33+
<HttpHeader>
34+
<NewHttpHeaders>
35+
<Header>
36+
<Key>x-cos</Key>
37+
<Value>exampleHeader</Value>
38+
</Header>
39+
</NewHttpHeaders>
40+
<FollowHttpHeaders>
41+
<Header>
42+
<Key>exampleHeaderKey</Key>
43+
</Header>
44+
</FollowHttpHeaders>
45+
</HttpHeader>
46+
<FollowRedirection>true</FollowRedirection>
47+
<HttpRedirectCode>302</HttpRedirectCode>
48+
</OriginParameter>
49+
<OriginInfo>
50+
<HostInfo>
51+
<HostName>examplebucket-1250000000.cos.ap-shanghai.myqcloud.com</HostName>
52+
</HostInfo>
53+
</OriginInfo>
54+
</OriginRule>
55+
</OriginConfiguration>
56+
`)
57+
})
58+
59+
res, _, err := client.Bucket.GetOrigin(context.Background())
60+
if err != nil {
61+
t.Fatalf("Bucket.GetOrigin returned error %v", err)
62+
}
63+
64+
want := &BucketGetOriginResult{
65+
XMLName: xml.Name{Local: "OriginConfiguration"},
66+
Rule: []BucketOriginRule{
67+
{
68+
OriginType: "Mirror",
69+
RulePriority: 1,
70+
OriginCondition: &BucketOriginCondition{
71+
HTTPStatusCode: "404",
72+
},
73+
OriginParameter: &BucketOriginParameter{
74+
Protocol: "HTTP",
75+
FollowQueryString: true,
76+
HttpHeader: &BucketOriginHttpHeader{
77+
FollowHttpHeaders: []OriginHttpHeader{
78+
{
79+
Key: "exampleHeaderKey",
80+
},
81+
},
82+
NewHttpHeaders: []OriginHttpHeader{
83+
{
84+
Key: "x-cos",
85+
Value: "exampleHeader",
86+
},
87+
},
88+
},
89+
FollowRedirection: true,
90+
HttpRedirectCode: "302",
91+
},
92+
OriginInfo: &BucketOriginInfo{
93+
HostInfo: "examplebucket-1250000000.cos.ap-shanghai.myqcloud.com",
94+
},
95+
},
96+
},
97+
}
98+
99+
if !reflect.DeepEqual(res, want) {
100+
t.Errorf("Bucket.GetOrigin returned %+v, want %+v", res, want)
101+
}
102+
}
103+
104+
func TestBucketService_PutOrigin(t *testing.T) {
105+
setup()
106+
defer teardown()
107+
108+
opt := &BucketPutOriginOptions{
109+
XMLName: xml.Name{Local: "OriginConfiguration"},
110+
Rule: []BucketOriginRule{
111+
{
112+
OriginType: "Mirror",
113+
RulePriority: 1,
114+
OriginCondition: &BucketOriginCondition{
115+
HTTPStatusCode: "404",
116+
},
117+
OriginParameter: &BucketOriginParameter{
118+
Protocol: "HTTP",
119+
FollowQueryString: true,
120+
HttpHeader: &BucketOriginHttpHeader{
121+
FollowHttpHeaders: []OriginHttpHeader{
122+
{
123+
Key: "exampleHeaderKey",
124+
},
125+
},
126+
NewHttpHeaders: []OriginHttpHeader{
127+
{
128+
Key: "x-cos",
129+
Value: "exampleHeader",
130+
},
131+
},
132+
},
133+
FollowRedirection: true,
134+
HttpRedirectCode: "302",
135+
},
136+
OriginInfo: &BucketOriginInfo{
137+
HostInfo: "examplebucket-1250000000.cos.ap-shanghai.myqcloud.com",
138+
},
139+
},
140+
},
141+
}
142+
143+
mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
144+
testMethod(t, r, "PUT")
145+
vs := values{
146+
"origin": "",
147+
}
148+
testFormValues(t, r, vs)
149+
150+
body := new(BucketPutOriginOptions)
151+
xml.NewDecoder(r.Body).Decode(body)
152+
want := opt
153+
want.XMLName = xml.Name{Local: "OriginConfiguration"}
154+
if !reflect.DeepEqual(body, want) {
155+
t.Errorf("Bucket.PutOrigin request\n body: %+v\n, want %+v\n", body, want)
156+
}
157+
})
158+
159+
_, err := client.Bucket.PutOrigin(context.Background(), opt)
160+
if err != nil {
161+
t.Fatalf("Bucket.PutOrigin returned error: %v", err)
162+
}
163+
}
164+
165+
func TestBucketService_DeleteOrigin(t *testing.T) {
166+
setup()
167+
defer teardown()
168+
mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
169+
testMethod(t, r, http.MethodDelete)
170+
vs := values{
171+
"origin": "",
172+
}
173+
testFormValues(t, r, vs)
174+
w.WriteHeader(http.StatusNoContent)
175+
})
176+
_, err := client.Bucket.DeleteOrigin(context.Background())
177+
if err != nil {
178+
t.Fatalf("Bucket.DeleteOrigin returned error: %v", err)
179+
}
180+
}

0 commit comments

Comments
 (0)