From ae3e31a216ebebfe1e858a9eec0bc5e859764d19 Mon Sep 17 00:00:00 2001 From: jojoliang Date: Sun, 25 Apr 2021 16:05:18 +0800 Subject: [PATCH] update bucket lifecycle,presignedURL,error format --- bucket_lifecycle.go | 9 ++++++++- bucket_lifecycle_test.go | 19 ++++++++++++++++--- error.go | 6 +++++- example/object/presigned_url_with_token.go | 26 +++++++++++++++++++++++++- object.go | 11 +++++++++++ 5 files changed, 65 insertions(+), 6 deletions(-) diff --git a/bucket_lifecycle.go b/bucket_lifecycle.go index d5d9096..bea3c16 100644 --- a/bucket_lifecycle.go +++ b/bucket_lifecycle.go @@ -6,9 +6,16 @@ import ( "net/http" ) +type BucketLifecycleAndOperator struct { + Prefix string `xml:"Prefix,omitempty"` + Tag []BucketTaggingTag `xml:"Tag,omitempty"` +} + // BucketLifecycleFilter is the param of BucketLifecycleRule type BucketLifecycleFilter struct { - Prefix string `xml:"Prefix,omitempty"` + Prefix string `xml:"Prefix,omitempty"` + Tag *BucketTaggingTag `xml:"Tag,omitempty"` + And *BucketLifecycleAndOperator `xml:"And,omitempty"` } // BucketLifecycleExpiration is the param of BucketLifecycleRule diff --git a/bucket_lifecycle_test.go b/bucket_lifecycle_test.go index fdbb422..fa7c479 100644 --- a/bucket_lifecycle_test.go +++ b/bucket_lifecycle_test.go @@ -23,7 +23,13 @@ func TestBucketService_GetLifecycle(t *testing.T) { 1234 - test + + test + + key + value + + Enabled @@ -53,8 +59,15 @@ func TestBucketService_GetLifecycle(t *testing.T) { XMLName: xml.Name{Local: "LifecycleConfiguration"}, Rules: []BucketLifecycleRule{ { - ID: "1234", - Filter: &BucketLifecycleFilter{Prefix: "test"}, + ID: "1234", + Filter: &BucketLifecycleFilter{ + And: &BucketLifecycleAndOperator{ + Prefix: "test", + Tag: []BucketTaggingTag{ + {Key: "key", Value: "value"}, + }, + }, + }, Status: "Enabled", Transition: &BucketLifecycleTransition{Days: 10, StorageClass: "Standard"}, }, diff --git a/error.go b/error.go index 23741e7..c101339 100644 --- a/error.go +++ b/error.go @@ -30,8 +30,12 @@ func (r *ErrorResponse) Error() string { if TraceID == "" { TraceID = r.Response.Header.Get("X-Cos-Trace-Id") } + decodeURL, err := decodeURIComponent(r.Response.Request.URL.String()) + if err != nil { + decodeURL = r.Response.Request.URL.String() + } return fmt.Sprintf("%v %v: %d %v(Message: %v, RequestId: %v, TraceId: %v)", - r.Response.Request.Method, r.Response.Request.URL, + r.Response.Request.Method, decodeURL, r.Response.StatusCode, r.Code, r.Message, RequestID, TraceID) } diff --git a/example/object/presigned_url_with_token.go b/example/object/presigned_url_with_token.go index 200a815..0e92187 100644 --- a/example/object/presigned_url_with_token.go +++ b/example/object/presigned_url_with_token.go @@ -29,6 +29,7 @@ func main() { name := "exampleobject" ctx := context.Background() + // 方法1 通过 tag 设置 x-cos-security-token // Get presigned presignedURL, err := c.Object.GetPresignedURL(ctx, http.MethodGet, name, tak, tsk, time.Hour, token) if err != nil { @@ -36,9 +37,32 @@ func main() { return } // Get object by presinged url - _, err = http.Get(presignedURL.String()) + resp, err := http.Get(presignedURL.String()) if err != nil { fmt.Printf("Error: %v\n", err) } + defer resp.Body.Close() fmt.Println(presignedURL.String()) + fmt.Printf("resp:%v\n", resp) + + // 方法2 通过 PresignedURLOptions 设置 x-cos-security-token + opt := &cos.PresignedURLOptions{ + Query: &url.Values{}, + Header: &http.Header{}, + } + opt.Query.Add("x-cos-security-token", "") + // Get presigned + presignedURL, err = c.Object.GetPresignedURL(ctx, http.MethodGet, name, tak, tsk, time.Hour, opt) + if err != nil { + fmt.Printf("Error: %v\n", err) + return + } + // Get object by presinged url + resp, err = http.Get(presignedURL.String()) + if err != nil { + fmt.Printf("Error: %v\n", err) + } + defer resp.Body.Close() + fmt.Println(presignedURL.String()) + fmt.Printf("resp:%v\n", resp) } diff --git a/object.go b/object.go index f3276be..a91bffc 100644 --- a/object.go +++ b/object.go @@ -106,6 +106,11 @@ func (s *ObjectService) GetToFile(ctx context.Context, name, localpath string, o return resp, nil } +type PresignedURLOptions struct { + Query *url.Values `xml:"-" url:"-" header:"-"` + Header *http.Header `header:"-,omitempty" url:"-" xml:"-"` +} + // GetPresignedURL get the object presigned to down or upload file by url func (s *ObjectService) GetPresignedURL(ctx context.Context, httpMethod, name, ak, sk string, expired time.Duration, opt interface{}) (*url.URL, error) { sendOpt := sendOptions{ @@ -115,6 +120,12 @@ func (s *ObjectService) GetPresignedURL(ctx context.Context, httpMethod, name, a optQuery: opt, optHeader: opt, } + if popt, ok := opt.(*PresignedURLOptions); ok { + qs := popt.Query.Encode() + if qs != "" { + sendOpt.uri = fmt.Sprintf("%s?%s", sendOpt.uri, qs) + } + } req, err := s.client.newRequest(ctx, sendOpt.baseURL, sendOpt.uri, sendOpt.method, sendOpt.body, sendOpt.optQuery, sendOpt.optHeader) if err != nil { return nil, err