From c07e49771c809fab640ac9f2c31d776de9dea23d Mon Sep 17 00:00:00 2001 From: jojoliang Date: Fri, 25 Dec 2020 10:58:17 +0800 Subject: [PATCH] Add Bucket Accelerate --- bucket_accelerate.go | 37 ++++++++++++++++++++++ bucket_accelerate_test.go | 74 ++++++++++++++++++++++++++++++++++++++++++++ costesting/ci_test.go | 25 +++++++++++++++ example/bucket/accelerate.go | 72 ++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 208 insertions(+) create mode 100644 bucket_accelerate.go create mode 100644 bucket_accelerate_test.go create mode 100644 example/bucket/accelerate.go diff --git a/bucket_accelerate.go b/bucket_accelerate.go new file mode 100644 index 0000000..0eb4c44 --- /dev/null +++ b/bucket_accelerate.go @@ -0,0 +1,37 @@ +package cos + +import ( + "context" + "encoding/xml" + "net/http" +) + +type BucketPutAccelerateOptions struct { + XMLName xml.Name `xml:"AccelerateConfiguration"` + Status string `xml:"Status,omitempty"` + Type string `xml:"Type,omitempty"` +} +type BucketGetAccelerateResult BucketPutAccelerateOptions + +func (s *BucketService) PutAccelerate(ctx context.Context, opt *BucketPutAccelerateOptions) (*Response, error) { + sendOpt := &sendOptions{ + baseURL: s.client.BaseURL.BucketURL, + uri: "/?accelerate", + method: http.MethodPut, + body: opt, + } + resp, err := s.client.send(ctx, sendOpt) + return resp, err +} + +func (s *BucketService) GetAccelerate(ctx context.Context) (*BucketGetAccelerateResult, *Response, error) { + var res BucketGetAccelerateResult + sendOpt := &sendOptions{ + baseURL: s.client.BaseURL.BucketURL, + uri: "/?accelerate", + method: http.MethodGet, + result: &res, + } + resp, err := s.client.send(ctx, sendOpt) + return &res, resp, err +} diff --git a/bucket_accelerate_test.go b/bucket_accelerate_test.go new file mode 100644 index 0000000..3573bb6 --- /dev/null +++ b/bucket_accelerate_test.go @@ -0,0 +1,74 @@ +package cos + +import ( + "context" + "encoding/xml" + "fmt" + "net/http" + "reflect" + "testing" +) + +func TestBucketService_GetAccelerate(t *testing.T) { + setup() + defer teardown() + + mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) { + testMethod(t, r, "GET") + vs := values{ + "accelerate": "", + } + testFormValues(t, r, vs) + fmt.Fprint(w, ` + Enabled + COS +`) + }) + + res, _, err := client.Bucket.GetAccelerate(context.Background()) + if err != nil { + t.Fatalf("Bucket.GetAccelerate returned error %v", err) + } + + want := &BucketGetAccelerateResult{ + XMLName: xml.Name{Local: "AccelerateConfiguration"}, + Status: "Enabled", + Type: "COS", + } + + if !reflect.DeepEqual(res, want) { + t.Errorf("Bucket.GetAccelerate returned %+v, want %+v", res, want) + } +} + +func TestBucketService_PutAccelerate(t *testing.T) { + setup() + defer teardown() + + opt := &BucketPutAccelerateOptions{ + XMLName: xml.Name{Local: "AccelerateConfiguration"}, + Status: "Enabled", + Type: "COS", + } + + mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) { + testMethod(t, r, "PUT") + vs := values{ + "accelerate": "", + } + testFormValues(t, r, vs) + + body := new(BucketPutAccelerateOptions) + xml.NewDecoder(r.Body).Decode(body) + want := opt + want.XMLName = xml.Name{Local: "AccelerateConfiguration"} + if !reflect.DeepEqual(body, want) { + t.Errorf("Bucket.PutAccelerate request\n body: %+v\n, want %+v\n", body, want) + } + }) + + _, err := client.Bucket.PutAccelerate(context.Background(), opt) + if err != nil { + t.Fatalf("Bucket.PutAccelerate returned error: %v", err) + } +} diff --git a/costesting/ci_test.go b/costesting/ci_test.go index dcc85f9..bc5c905 100644 --- a/costesting/ci_test.go +++ b/costesting/ci_test.go @@ -882,6 +882,31 @@ func (s *CosTestSuite) TestReferer() { assert.Equal(s.T(), opt.EmptyReferConfiguration, res.EmptyReferConfiguration, "GetReferer Failed") } +func (s *CosTestSuite) TestAccelerate() { + opt := &cos.BucketPutAccelerateOptions{ + Status: "Enabled", + Type: "COS", + } + _, err := s.Client.Bucket.PutAccelerate(context.Background(), opt) + assert.Nil(s.T(), err, "PutAccelerate Failed") + + time.Sleep(time.Second) + res, _, err := s.Client.Bucket.GetAccelerate(context.Background()) + assert.Nil(s.T(), err, "GetAccelerate Failed") + assert.Equal(s.T(), opt.Status, res.Status, "GetAccelerate Failed") + assert.Equal(s.T(), opt.Type, res.Type, "GetAccelerate Failed") + + opt.Status = "Suspended" + _, err = s.Client.Bucket.PutAccelerate(context.Background(), opt) + assert.Nil(s.T(), err, "PutAccelerate Failed") + + time.Sleep(time.Second) + res, _, err = s.Client.Bucket.GetAccelerate(context.Background()) + assert.Nil(s.T(), err, "GetAccelerate Failed") + assert.Equal(s.T(), opt.Status, res.Status, "GetAccelerate Failed") + assert.Equal(s.T(), opt.Type, res.Type, "GetAccelerate Failed") +} + // End of api test // All methods that begin with "Test" are run as tests within a diff --git a/example/bucket/accelerate.go b/example/bucket/accelerate.go new file mode 100644 index 0000000..49c2ff4 --- /dev/null +++ b/example/bucket/accelerate.go @@ -0,0 +1,72 @@ +package main + +import ( + "context" + "fmt" + "net/http" + "net/url" + "os" + + "github.com/tencentyun/cos-go-sdk-v5" + "github.com/tencentyun/cos-go-sdk-v5/debug" +) + +func log_status(err error) { + if err == nil { + return + } + if cos.IsNotFoundError(err) { + // WARN + fmt.Println("Resource is not existed") + } else if e, ok := cos.IsCOSError(err); ok { + fmt.Printf("Code: %v\n", e.Code) + fmt.Printf("Message: %v\n", e.Message) + fmt.Printf("Resource: %v\n", e.Resource) + fmt.Printf("RequestId: %v\n", e.RequestID) + // ERROR + } else { + fmt.Println(err) + // ERROR + } +} + +func main() { + u, _ := url.Parse("https://test-1259654469.cos.ap-guangzhou.myqcloud.com") + b := &cos.BaseURL{ + BucketURL: u, + } + c := cos.NewClient(b, &http.Client{ + Transport: &cos.AuthorizationTransport{ + SecretID: os.Getenv("COS_SECRETID"), + SecretKey: os.Getenv("COS_SECRETKEY"), + Transport: &debug.DebugRequestTransport{ + RequestHeader: true, + RequestBody: true, + ResponseHeader: true, + ResponseBody: true, + }, + }, + }) + res, _, err := c.Bucket.GetAccelerate(context.Background()) + log_status(err) + fmt.Printf("%+v\n", res) + + opt := &cos.BucketPutAccelerateOptions{ + Status: "Enabled", + Type: "COS", + } + _, err = c.Bucket.PutAccelerate(context.Background(), opt) + log_status(err) + + res, _, err = c.Bucket.GetAccelerate(context.Background()) + log_status(err) + fmt.Printf("%+v\n", res) + + opt.Status = "Suspended" + _, err = c.Bucket.PutAccelerate(context.Background(), opt) + log_status(err) + + res, _, err = c.Bucket.GetAccelerate(context.Background()) + log_status(err) + fmt.Printf("%+v\n", res) +}