Skip to content

Commit e8b4da1

Browse files
authored
Merge pull request #120 from agin719/cos-dev-v5
update bucket lifecycle,presignedURL,error format
2 parents ca08d6e + ae3e31a commit e8b4da1

File tree

5 files changed

+65
-6
lines changed

5 files changed

+65
-6
lines changed

bucket_lifecycle.go

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,16 @@ import (
66
"net/http"
77
)
88

9+
type BucketLifecycleAndOperator struct {
10+
Prefix string `xml:"Prefix,omitempty"`
11+
Tag []BucketTaggingTag `xml:"Tag,omitempty"`
12+
}
13+
914
// BucketLifecycleFilter is the param of BucketLifecycleRule
1015
type BucketLifecycleFilter struct {
11-
Prefix string `xml:"Prefix,omitempty"`
16+
Prefix string `xml:"Prefix,omitempty"`
17+
Tag *BucketTaggingTag `xml:"Tag,omitempty"`
18+
And *BucketLifecycleAndOperator `xml:"And,omitempty"`
1219
}
1320

1421
// BucketLifecycleExpiration is the param of BucketLifecycleRule

bucket_lifecycle_test.go

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,13 @@ func TestBucketService_GetLifecycle(t *testing.T) {
2323
<Rule>
2424
<ID>1234</ID>
2525
<Filter>
26-
<Prefix>test</Prefix>
26+
<And>
27+
<Prefix>test</Prefix>
28+
<Tag>
29+
<Key>key</Key>
30+
<Value>value</Value>
31+
</Tag>
32+
</And>
2733
</Filter>
2834
<Status>Enabled</Status>
2935
<Transition>
@@ -53,8 +59,15 @@ func TestBucketService_GetLifecycle(t *testing.T) {
5359
XMLName: xml.Name{Local: "LifecycleConfiguration"},
5460
Rules: []BucketLifecycleRule{
5561
{
56-
ID: "1234",
57-
Filter: &BucketLifecycleFilter{Prefix: "test"},
62+
ID: "1234",
63+
Filter: &BucketLifecycleFilter{
64+
And: &BucketLifecycleAndOperator{
65+
Prefix: "test",
66+
Tag: []BucketTaggingTag{
67+
{Key: "key", Value: "value"},
68+
},
69+
},
70+
},
5871
Status: "Enabled",
5972
Transition: &BucketLifecycleTransition{Days: 10, StorageClass: "Standard"},
6073
},

error.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,12 @@ func (r *ErrorResponse) Error() string {
3030
if TraceID == "" {
3131
TraceID = r.Response.Header.Get("X-Cos-Trace-Id")
3232
}
33+
decodeURL, err := decodeURIComponent(r.Response.Request.URL.String())
34+
if err != nil {
35+
decodeURL = r.Response.Request.URL.String()
36+
}
3337
return fmt.Sprintf("%v %v: %d %v(Message: %v, RequestId: %v, TraceId: %v)",
34-
r.Response.Request.Method, r.Response.Request.URL,
38+
r.Response.Request.Method, decodeURL,
3539
r.Response.StatusCode, r.Code, r.Message, RequestID, TraceID)
3640
}
3741

example/object/presigned_url_with_token.go

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,16 +29,40 @@ func main() {
2929
name := "exampleobject"
3030
ctx := context.Background()
3131

32+
// 方法1 通过 tag 设置 x-cos-security-token
3233
// Get presigned
3334
presignedURL, err := c.Object.GetPresignedURL(ctx, http.MethodGet, name, tak, tsk, time.Hour, token)
3435
if err != nil {
3536
fmt.Printf("Error: %v\n", err)
3637
return
3738
}
3839
// Get object by presinged url
39-
_, err = http.Get(presignedURL.String())
40+
resp, err := http.Get(presignedURL.String())
4041
if err != nil {
4142
fmt.Printf("Error: %v\n", err)
4243
}
44+
defer resp.Body.Close()
4345
fmt.Println(presignedURL.String())
46+
fmt.Printf("resp:%v\n", resp)
47+
48+
// 方法2 通过 PresignedURLOptions 设置 x-cos-security-token
49+
opt := &cos.PresignedURLOptions{
50+
Query: &url.Values{},
51+
Header: &http.Header{},
52+
}
53+
opt.Query.Add("x-cos-security-token", "<token>")
54+
// Get presigned
55+
presignedURL, err = c.Object.GetPresignedURL(ctx, http.MethodGet, name, tak, tsk, time.Hour, opt)
56+
if err != nil {
57+
fmt.Printf("Error: %v\n", err)
58+
return
59+
}
60+
// Get object by presinged url
61+
resp, err = http.Get(presignedURL.String())
62+
if err != nil {
63+
fmt.Printf("Error: %v\n", err)
64+
}
65+
defer resp.Body.Close()
66+
fmt.Println(presignedURL.String())
67+
fmt.Printf("resp:%v\n", resp)
4468
}

object.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,11 @@ func (s *ObjectService) GetToFile(ctx context.Context, name, localpath string, o
106106
return resp, nil
107107
}
108108

109+
type PresignedURLOptions struct {
110+
Query *url.Values `xml:"-" url:"-" header:"-"`
111+
Header *http.Header `header:"-,omitempty" url:"-" xml:"-"`
112+
}
113+
109114
// GetPresignedURL get the object presigned to down or upload file by url
110115
func (s *ObjectService) GetPresignedURL(ctx context.Context, httpMethod, name, ak, sk string, expired time.Duration, opt interface{}) (*url.URL, error) {
111116
sendOpt := sendOptions{
@@ -115,6 +120,12 @@ func (s *ObjectService) GetPresignedURL(ctx context.Context, httpMethod, name, a
115120
optQuery: opt,
116121
optHeader: opt,
117122
}
123+
if popt, ok := opt.(*PresignedURLOptions); ok {
124+
qs := popt.Query.Encode()
125+
if qs != "" {
126+
sendOpt.uri = fmt.Sprintf("%s?%s", sendOpt.uri, qs)
127+
}
128+
}
118129
req, err := s.client.newRequest(ctx, sendOpt.baseURL, sendOpt.uri, sendOpt.method, sendOpt.body, sendOpt.optQuery, sendOpt.optHeader)
119130
if err != nil {
120131
return nil, err

0 commit comments

Comments
 (0)