Skip to content

Commit 649bd02

Browse files
authored
Merge pull request #85 from agin719/common-dev
add bucket intelligenttiering
2 parents 31af2de + 1468391 commit 649bd02

File tree

3 files changed

+187
-0
lines changed

3 files changed

+187
-0
lines changed

bucket_intelligenttiering.go

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
package cos
2+
3+
import (
4+
"context"
5+
"encoding/xml"
6+
"net/http"
7+
)
8+
9+
type BucketIntelligentTieringTransition struct {
10+
Days int `xml:"Days,omitempty"`
11+
RequestFrequent int `xml:"RequestFrequent,omitempty"`
12+
}
13+
14+
type BucketPutIntelligentTieringOptions struct {
15+
XMLName xml.Name `xml:"IntelligentTieringConfiguration"`
16+
Status string `xml:"Status,omitempty"`
17+
Transition *BucketIntelligentTieringTransition `xml:"Transition,omitempty"`
18+
}
19+
20+
type BucketGetIntelligentTieringResult BucketPutIntelligentTieringOptions
21+
22+
func (s *BucketService) PutIntelligentTiering(ctx context.Context, opt *BucketPutIntelligentTieringOptions) (*Response, error) {
23+
if opt != nil && opt.Transition != nil {
24+
opt.Transition.RequestFrequent = 1
25+
}
26+
sendOpt := sendOptions{
27+
baseURL: s.client.BaseURL.BucketURL,
28+
uri: "/?intelligenttiering",
29+
method: http.MethodPut,
30+
body: opt,
31+
}
32+
resp, err := s.client.send(ctx, &sendOpt)
33+
return resp, err
34+
}
35+
36+
func (s *BucketService) GetIntelligentTiering(ctx context.Context) (*BucketGetIntelligentTieringResult, *Response, error) {
37+
var res BucketGetIntelligentTieringResult
38+
sendOpt := sendOptions{
39+
baseURL: s.client.BaseURL.BucketURL,
40+
uri: "/?intelligenttiering",
41+
method: http.MethodGet,
42+
result: &res,
43+
}
44+
resp, err := s.client.send(ctx, &sendOpt)
45+
return &res, resp, err
46+
47+
}

bucket_intelligenttiering_test.go

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
package cos
2+
3+
import (
4+
"context"
5+
"encoding/xml"
6+
"fmt"
7+
"net/http"
8+
"reflect"
9+
"testing"
10+
)
11+
12+
func TestBucketService_PutIntelligentTiering(t *testing.T) {
13+
setup()
14+
defer teardown()
15+
opt := &BucketPutIntelligentTieringOptions{
16+
Status: "Enabled",
17+
Transition: &BucketIntelligentTieringTransition{
18+
Days: 30,
19+
},
20+
}
21+
mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
22+
testMethod(t, r, http.MethodPut)
23+
vs := values{
24+
"intelligenttiering": "",
25+
}
26+
testFormValues(t, r, vs)
27+
28+
body := &BucketPutIntelligentTieringOptions{}
29+
xml.NewDecoder(r.Body).Decode(body)
30+
want := opt
31+
want.XMLName = xml.Name{Local: "IntelligentTieringConfiguration"}
32+
if !reflect.DeepEqual(want, body) {
33+
t.Fatalf("Bucket.PutIntelligentTiering request\n body: %+v\n, want %+v\n", body, want)
34+
}
35+
})
36+
37+
_, err := client.Bucket.PutIntelligentTiering(context.Background(), opt)
38+
if err != nil {
39+
t.Fatalf("Bucket.PutIntelligentTiering failed, error: %v", err)
40+
}
41+
}
42+
43+
func TestBucketService_GetIntelligentTiering(t *testing.T) {
44+
setup()
45+
defer teardown()
46+
47+
mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
48+
testMethod(t, r, http.MethodGet)
49+
vs := values{
50+
"intelligenttiering": "",
51+
}
52+
testFormValues(t, r, vs)
53+
54+
fmt.Fprint(w, `<IntelligentTieringConfiguration>
55+
<Status>Enabled</Status>
56+
<Transition>
57+
<Days>30</Days>
58+
</Transition>
59+
</IntelligentTieringConfiguration>`)
60+
})
61+
res, _, err := client.Bucket.GetIntelligentTiering(context.Background())
62+
if err != nil {
63+
t.Fatalf("Bucket.GetIntelligentTiering failed, error: %v", err)
64+
}
65+
want := &BucketGetIntelligentTieringResult{
66+
XMLName: xml.Name{Local: "IntelligentTieringConfiguration"},
67+
Status: "Enabled",
68+
Transition: &BucketIntelligentTieringTransition{
69+
Days: 30,
70+
},
71+
}
72+
73+
if !reflect.DeepEqual(res, want) {
74+
t.Errorf("Bucket.GetIntelligentTiering returned\n%+v, want\n%+v", res, want)
75+
}
76+
}
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
package main
2+
3+
import (
4+
"context"
5+
"fmt"
6+
"net/http"
7+
"net/url"
8+
"os"
9+
10+
"github.com/tencentyun/cos-go-sdk-v5"
11+
"github.com/tencentyun/cos-go-sdk-v5/debug"
12+
)
13+
14+
func log_status(err error) {
15+
if err == nil {
16+
return
17+
}
18+
if cos.IsNotFoundError(err) {
19+
// WARN
20+
fmt.Println("Resource is not existed")
21+
} else if e, ok := cos.IsCOSError(err); ok {
22+
fmt.Printf("Code: %v\n", e.Code)
23+
fmt.Printf("Message: %v\n", e.Message)
24+
fmt.Printf("Resource: %v\n", e.Resource)
25+
fmt.Printf("RequestId: %v\n", e.RequestID)
26+
// ERROR
27+
} else {
28+
fmt.Println(err)
29+
// ERROR
30+
}
31+
}
32+
33+
func main() {
34+
u, _ := url.Parse("https://test-1259654469.cos.ap-guangzhou.myqcloud.com")
35+
b := &cos.BaseURL{
36+
BucketURL: u,
37+
}
38+
c := cos.NewClient(b, &http.Client{
39+
Transport: &cos.AuthorizationTransport{
40+
SecretID: os.Getenv("COS_SECRETID"),
41+
SecretKey: os.Getenv("COS_SECRETKEY"),
42+
Transport: &debug.DebugRequestTransport{
43+
RequestHeader: true,
44+
RequestBody: false,
45+
ResponseHeader: true,
46+
ResponseBody: false,
47+
},
48+
},
49+
})
50+
51+
opt := &cos.BucketPutIntelligentTieringOptions {
52+
Status: "Enabled",
53+
Transition: &cos.BucketIntelligentTieringTransition {
54+
Days: 30,
55+
},
56+
}
57+
_, err := c.Bucket.PutIntelligentTiering(context.Background(), opt)
58+
log_status(err)
59+
res, _, err := c.Bucket.GetIntelligentTiering(context.Background())
60+
log_status(err)
61+
fmt.Printf("%+v\n", res)
62+
fmt.Printf("%+v\n", res.Status)
63+
fmt.Printf("%+v\n", res.Transition.Days)
64+
}

0 commit comments

Comments
 (0)