Browse Source

Add Bucket Accelerate

tags/v0.7.16^2
jojoliang 4 years ago
parent
commit
c07e49771c
  1. 37
      bucket_accelerate.go
  2. 74
      bucket_accelerate_test.go
  3. 25
      costesting/ci_test.go
  4. 72
      example/bucket/accelerate.go

37
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
}

74
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, `<AccelerateConfiguration>
<Status>Enabled</Status>
<Type>COS</Type>
</AccelerateConfiguration>`)
})
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)
}
}

25
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

72
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)
}
Loading…
Cancel
Save