-
Notifications
You must be signed in to change notification settings - Fork 92
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #80 from agin719/common-dev
add object tagging && bucket origin && add stsv3 demo
- Loading branch information
Showing
7 changed files
with
432 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
package cos | ||
|
||
import ( | ||
"context" | ||
"encoding/xml" | ||
"net/http" | ||
) | ||
|
||
type BucketPutOriginOptions struct { | ||
XMLName xml.Name `xml:"OriginConfiguration"` | ||
Rule []BucketOriginRule `xml:"OriginRule"` | ||
} | ||
|
||
type BucketOriginRule struct { | ||
OriginType string `xml:"OriginType"` | ||
OriginCondition *BucketOriginCondition `xml:"OriginCondition"` | ||
OriginParameter *BucketOriginParameter `xml:"OriginParameter"` | ||
OriginInfo *BucketOriginInfo `xml:"OriginInfo"` | ||
} | ||
|
||
type BucketOriginCondition struct { | ||
HTTPStatusCode string `xml:"HTTPStatusCode,omitempty"` | ||
Prefix string `xml:"Prefix,omitempty"` | ||
} | ||
|
||
type BucketOriginParameter struct { | ||
Protocol string `xml:"Protocol,omitempty"` | ||
FollowQueryString bool `xml:"FollowQueryString,omitempty"` | ||
HttpHeader *BucketOriginHttpHeader `xml:"HttpHeader,omitempty"` | ||
FollowRedirection bool `xml:"FollowRedirection,omitempty"` | ||
HttpRedirectCode string `xml:"HttpRedirectCode,omitempty"` | ||
CopyOriginData bool `xml:"CopyOriginData,omitempty"` | ||
} | ||
|
||
type BucketOriginHttpHeader struct { | ||
// 目前还不支持 FollowAllHeaders | ||
// FollowAllHeaders bool `xml:"FollowAllHeaders,omitempty"` | ||
NewHttpHeaders []OriginHttpHeader `xml:"NewHttpHeaders>Header,omitempty"` | ||
FollowHttpHeaders []OriginHttpHeader `xml:"FollowHttpHeaders>Header,omitempty"` | ||
} | ||
|
||
type OriginHttpHeader struct { | ||
Key string `xml:"Key,omitempty"` | ||
Value string `xml:"Value,omitempty"` | ||
} | ||
|
||
type BucketOriginInfo struct { | ||
HostInfo string `xml:"HostInfo>HostName,omitempty"` | ||
FileInfo *BucketOriginFileInfo `xml:"FileInfo,omitempty"` | ||
} | ||
type BucketOriginFileInfo struct { | ||
PrefixDirective bool `xml:"PrefixDirective,omitempty"` | ||
Prefix string `xml:"Prefix,omitempty"` | ||
Suffix string `xml:"Suffix,omitempty"` | ||
} | ||
|
||
type BucketGetOriginResult BucketPutOriginOptions | ||
|
||
func (s *BucketService) PutOrigin(ctx context.Context, opt *BucketPutOriginOptions) (*Response, error) { | ||
sendOpt := &sendOptions{ | ||
baseURL: s.client.BaseURL.BucketURL, | ||
uri: "/?origin", | ||
method: http.MethodPut, | ||
body: opt, | ||
} | ||
resp, err := s.client.send(ctx, sendOpt) | ||
return resp, err | ||
} | ||
|
||
func (s *BucketService) GetOrigin(ctx context.Context) (*BucketGetOriginResult, *Response, error) { | ||
var res BucketGetOriginResult | ||
sendOpt := &sendOptions{ | ||
baseURL: s.client.BaseURL.BucketURL, | ||
uri: "/?origin", | ||
method: http.MethodGet, | ||
result: &res, | ||
} | ||
resp, err := s.client.send(ctx, sendOpt) | ||
return &res, resp, err | ||
} | ||
|
||
func (s *BucketService) DeleteOrigin(ctx context.Context) (*Response, error) { | ||
sendOpt := &sendOptions{ | ||
baseURL: s.client.BaseURL.BucketURL, | ||
uri: "/?origin", | ||
method: http.MethodDelete, | ||
} | ||
resp, err := s.client.send(ctx, sendOpt) | ||
return resp, err | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
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, | ||
}, | ||
}, | ||
}) | ||
|
||
opt := &cos.BucketPutOriginOptions{ | ||
Rule: []cos.BucketOriginRule{ | ||
{ | ||
OriginType: "Proxy", | ||
OriginCondition: &cos.BucketOriginCondition{ | ||
HTTPStatusCode: "404", | ||
Prefix: "", | ||
}, | ||
OriginParameter: &cos.BucketOriginParameter{ | ||
Protocol: "FOLLOW", | ||
FollowQueryString: true, | ||
HttpHeader: &cos.BucketOriginHttpHeader{ | ||
NewHttpHeaders: []cos.OriginHttpHeader{ | ||
{ | ||
Key: "x-cos-ContentType", | ||
Value: "csv", | ||
}, | ||
}, | ||
FollowHttpHeaders: []cos.OriginHttpHeader{ | ||
{ | ||
Key: "Content-Type", | ||
}, | ||
}, | ||
}, | ||
FollowRedirection: true, | ||
}, | ||
OriginInfo: &cos.BucketOriginInfo{ | ||
HostInfo: "examplebucket-1250000000.cos.ap-shanghai.myqcloud.com", | ||
}, | ||
}, | ||
}, | ||
} | ||
|
||
_, err := c.Bucket.PutOrigin(context.Background(), opt) | ||
log_status(err) | ||
res, _, err := c.Bucket.GetOrigin(context.Background()) | ||
log_status(err) | ||
fmt.Printf("%+v\n", res) | ||
fmt.Printf("%+v\n", res.Rule) | ||
_, err = c.Bucket.DeleteOrigin(context.Background()) | ||
log_status(err) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
package main | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"net/url" | ||
"os" | ||
|
||
"net/http" | ||
|
||
"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("WARN: Resource is not existed") | ||
} else if e, ok := cos.IsCOSError(err); ok { | ||
fmt.Printf("ERROR: Code: %v\n", e.Code) | ||
fmt.Printf("ERROR: Message: %v\n", e.Message) | ||
fmt.Printf("ERROR: Resource: %v\n", e.Resource) | ||
fmt.Printf("ERROR: RequestId: %v\n", e.RequestID) | ||
// ERROR | ||
} else { | ||
fmt.Printf("ERROR: %v\n", 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, | ||
}, | ||
}, | ||
}) | ||
name := "test" | ||
|
||
opt := &cos.ObjectPutTaggingOptions{ | ||
TagSet: []cos.ObjectTaggingTag{ | ||
{ | ||
Key: "test_k2", | ||
Value: "test_v2", | ||
}, | ||
{ | ||
Key: "test_k3", | ||
Value: "test_v3", | ||
}, | ||
}, | ||
} | ||
|
||
_, err := c.Object.PutTagging(context.Background(), name, opt) | ||
log_status(err) | ||
|
||
res, _, err := c.Object.GetTagging(context.Background(), name) | ||
log_status(err) | ||
fmt.Printf("%v\n", res.TagSet) | ||
|
||
_, err = c.Object.DeleteTagging(context.Background(), name) | ||
log_status(err) | ||
} |
Oops, something went wrong.