jojoliang
4 years ago
4 changed files with 208 additions and 0 deletions
-
37bucket_accelerate.go
-
74bucket_accelerate_test.go
-
25costesting/ci_test.go
-
72example/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 |
|||
} |
@ -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) |
|||
} |
|||
} |
@ -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) |
|||
} |
Write
Preview
Loading…
Cancel
Save
Reference in new issue