Skip to content

Commit 7b799af

Browse files
authored
Merge pull request #80 from agin719/common-dev
add object tagging && bucket origin && add stsv3 demo
2 parents 1cc4970 + a0ab0eb commit 7b799af

File tree

7 files changed

+432
-10
lines changed

7 files changed

+432
-10
lines changed

bucket_origin.go

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
package cos
2+
3+
import (
4+
"context"
5+
"encoding/xml"
6+
"net/http"
7+
)
8+
9+
type BucketPutOriginOptions struct {
10+
XMLName xml.Name `xml:"OriginConfiguration"`
11+
Rule []BucketOriginRule `xml:"OriginRule"`
12+
}
13+
14+
type BucketOriginRule struct {
15+
OriginType string `xml:"OriginType"`
16+
OriginCondition *BucketOriginCondition `xml:"OriginCondition"`
17+
OriginParameter *BucketOriginParameter `xml:"OriginParameter"`
18+
OriginInfo *BucketOriginInfo `xml:"OriginInfo"`
19+
}
20+
21+
type BucketOriginCondition struct {
22+
HTTPStatusCode string `xml:"HTTPStatusCode,omitempty"`
23+
Prefix string `xml:"Prefix,omitempty"`
24+
}
25+
26+
type BucketOriginParameter struct {
27+
Protocol string `xml:"Protocol,omitempty"`
28+
FollowQueryString bool `xml:"FollowQueryString,omitempty"`
29+
HttpHeader *BucketOriginHttpHeader `xml:"HttpHeader,omitempty"`
30+
FollowRedirection bool `xml:"FollowRedirection,omitempty"`
31+
HttpRedirectCode string `xml:"HttpRedirectCode,omitempty"`
32+
CopyOriginData bool `xml:"CopyOriginData,omitempty"`
33+
}
34+
35+
type BucketOriginHttpHeader struct {
36+
// 目前还不支持 FollowAllHeaders
37+
// FollowAllHeaders bool `xml:"FollowAllHeaders,omitempty"`
38+
NewHttpHeaders []OriginHttpHeader `xml:"NewHttpHeaders>Header,omitempty"`
39+
FollowHttpHeaders []OriginHttpHeader `xml:"FollowHttpHeaders>Header,omitempty"`
40+
}
41+
42+
type OriginHttpHeader struct {
43+
Key string `xml:"Key,omitempty"`
44+
Value string `xml:"Value,omitempty"`
45+
}
46+
47+
type BucketOriginInfo struct {
48+
HostInfo string `xml:"HostInfo>HostName,omitempty"`
49+
FileInfo *BucketOriginFileInfo `xml:"FileInfo,omitempty"`
50+
}
51+
type BucketOriginFileInfo struct {
52+
PrefixDirective bool `xml:"PrefixDirective,omitempty"`
53+
Prefix string `xml:"Prefix,omitempty"`
54+
Suffix string `xml:"Suffix,omitempty"`
55+
}
56+
57+
type BucketGetOriginResult BucketPutOriginOptions
58+
59+
func (s *BucketService) PutOrigin(ctx context.Context, opt *BucketPutOriginOptions) (*Response, error) {
60+
sendOpt := &sendOptions{
61+
baseURL: s.client.BaseURL.BucketURL,
62+
uri: "/?origin",
63+
method: http.MethodPut,
64+
body: opt,
65+
}
66+
resp, err := s.client.send(ctx, sendOpt)
67+
return resp, err
68+
}
69+
70+
func (s *BucketService) GetOrigin(ctx context.Context) (*BucketGetOriginResult, *Response, error) {
71+
var res BucketGetOriginResult
72+
sendOpt := &sendOptions{
73+
baseURL: s.client.BaseURL.BucketURL,
74+
uri: "/?origin",
75+
method: http.MethodGet,
76+
result: &res,
77+
}
78+
resp, err := s.client.send(ctx, sendOpt)
79+
return &res, resp, err
80+
}
81+
82+
func (s *BucketService) DeleteOrigin(ctx context.Context) (*Response, error) {
83+
sendOpt := &sendOptions{
84+
baseURL: s.client.BaseURL.BucketURL,
85+
uri: "/?origin",
86+
method: http.MethodDelete,
87+
}
88+
resp, err := s.client.send(ctx, sendOpt)
89+
return resp, err
90+
}

cos.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ import (
2121

2222
const (
2323
// Version current go sdk version
24-
Version = "0.7.6"
24+
Version = "0.7.7"
2525
userAgent = "cos-go-sdk-v5/" + Version
2626
contentTypeXML = "application/xml"
2727
defaultServiceBaseURL = "http://service.cos.myqcloud.com"
@@ -329,6 +329,8 @@ type ACLHeaderOptions struct {
329329
XCosGrantRead string `header:"x-cos-grant-read,omitempty" url:"-" xml:"-"`
330330
XCosGrantWrite string `header:"x-cos-grant-write,omitempty" url:"-" xml:"-"`
331331
XCosGrantFullControl string `header:"x-cos-grant-full-control,omitempty" url:"-" xml:"-"`
332+
XCosGrantReadACP string `header:"x-cos-grant-read-acp,omitempty" url:"-" xml:"-"`
333+
XCosGrantWriteACP string `header:"x-cos-grant-write-acp,omitempty" url:"-" xml:"-"`
332334
}
333335

334336
// ACLGrantee is the param of ACLGrant

example/bucket/getObjectVersion.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
package main
22

33
import (
4-
"context"
4+
"context"
55
"fmt"
66
"os"
77

@@ -19,7 +19,7 @@ func log_status(err error) {
1919
}
2020
if cos.IsNotFoundError(err) {
2121
// WARN
22-
fmt.Println("WARN: Resource is not existed")
22+
fmt.Println("WARN: Resource is not existed")
2323
} else if e, ok := cos.IsCOSError(err); ok {
2424
fmt.Printf("ERROR: Code: %v\n", e.Code)
2525
fmt.Printf("ERROR: Message: %v\n", e.Message)
@@ -50,12 +50,12 @@ func main() {
5050
},
5151
})
5252

53-
opt := &cos.BucketGetObjectVersionsOptions {
54-
Delimiter: "/",
55-
MaxKeys: 1,
53+
opt := &cos.BucketGetObjectVersionsOptions{
54+
Delimiter: "/",
55+
MaxKeys: 1,
5656
}
5757
v, _, err := c.Bucket.GetObjectVersions(context.Background(), opt)
58-
log_status(err)
58+
log_status(err)
5959

6060
for _, c := range v.Version {
6161
fmt.Printf("%v, %v, %v\n", c.Key, c.Size, c.IsLatest)

example/bucket/origin.go

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
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: true,
45+
ResponseHeader: true,
46+
ResponseBody: true,
47+
},
48+
},
49+
})
50+
51+
opt := &cos.BucketPutOriginOptions{
52+
Rule: []cos.BucketOriginRule{
53+
{
54+
OriginType: "Proxy",
55+
OriginCondition: &cos.BucketOriginCondition{
56+
HTTPStatusCode: "404",
57+
Prefix: "",
58+
},
59+
OriginParameter: &cos.BucketOriginParameter{
60+
Protocol: "FOLLOW",
61+
FollowQueryString: true,
62+
HttpHeader: &cos.BucketOriginHttpHeader{
63+
NewHttpHeaders: []cos.OriginHttpHeader{
64+
{
65+
Key: "x-cos-ContentType",
66+
Value: "csv",
67+
},
68+
},
69+
FollowHttpHeaders: []cos.OriginHttpHeader{
70+
{
71+
Key: "Content-Type",
72+
},
73+
},
74+
},
75+
FollowRedirection: true,
76+
},
77+
OriginInfo: &cos.BucketOriginInfo{
78+
HostInfo: "examplebucket-1250000000.cos.ap-shanghai.myqcloud.com",
79+
},
80+
},
81+
},
82+
}
83+
84+
_, err := c.Bucket.PutOrigin(context.Background(), opt)
85+
log_status(err)
86+
res, _, err := c.Bucket.GetOrigin(context.Background())
87+
log_status(err)
88+
fmt.Printf("%+v\n", res)
89+
fmt.Printf("%+v\n", res.Rule)
90+
_, err = c.Bucket.DeleteOrigin(context.Background())
91+
log_status(err)
92+
}

example/object/tagging.go

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
package main
2+
3+
import (
4+
"context"
5+
"fmt"
6+
"net/url"
7+
"os"
8+
9+
"net/http"
10+
11+
"github.com/tencentyun/cos-go-sdk-v5"
12+
"github.com/tencentyun/cos-go-sdk-v5/debug"
13+
)
14+
15+
func log_status(err error) {
16+
if err == nil {
17+
return
18+
}
19+
if cos.IsNotFoundError(err) {
20+
// WARN
21+
fmt.Println("WARN: Resource is not existed")
22+
} else if e, ok := cos.IsCOSError(err); ok {
23+
fmt.Printf("ERROR: Code: %v\n", e.Code)
24+
fmt.Printf("ERROR: Message: %v\n", e.Message)
25+
fmt.Printf("ERROR: Resource: %v\n", e.Resource)
26+
fmt.Printf("ERROR: RequestId: %v\n", e.RequestID)
27+
// ERROR
28+
} else {
29+
fmt.Printf("ERROR: %v\n", err)
30+
// ERROR
31+
}
32+
}
33+
34+
func main() {
35+
u, _ := url.Parse("https://test-1259654469.cos.ap-guangzhou.myqcloud.com")
36+
b := &cos.BaseURL{
37+
BucketURL: u,
38+
}
39+
c := cos.NewClient(b, &http.Client{
40+
Transport: &cos.AuthorizationTransport{
41+
SecretID: os.Getenv("COS_SECRETID"),
42+
SecretKey: os.Getenv("COS_SECRETKEY"),
43+
Transport: &debug.DebugRequestTransport{
44+
RequestHeader: true,
45+
RequestBody: true,
46+
ResponseHeader: true,
47+
ResponseBody: true,
48+
},
49+
},
50+
})
51+
name := "test"
52+
53+
opt := &cos.ObjectPutTaggingOptions{
54+
TagSet: []cos.ObjectTaggingTag{
55+
{
56+
Key: "test_k2",
57+
Value: "test_v2",
58+
},
59+
{
60+
Key: "test_k3",
61+
Value: "test_v3",
62+
},
63+
},
64+
}
65+
66+
_, err := c.Object.PutTagging(context.Background(), name, opt)
67+
log_status(err)
68+
69+
res, _, err := c.Object.GetTagging(context.Background(), name)
70+
log_status(err)
71+
fmt.Printf("%v\n", res.TagSet)
72+
73+
_, err = c.Object.DeleteTagging(context.Background(), name)
74+
log_status(err)
75+
}

0 commit comments

Comments
 (0)