Skip to content

Commit

Permalink
Merge pull request #212 from tencentyun/feature_jojoliang_36173e1d
Browse files Browse the repository at this point in the history
fix copy object, update bucket origin
  • Loading branch information
agin719 authored Oct 18, 2023
2 parents 7fc9595 + a2c02dc commit af6e1f5
Show file tree
Hide file tree
Showing 6 changed files with 123 additions and 19 deletions.
111 changes: 104 additions & 7 deletions bucket_origin.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,11 @@ package cos
import (
"context"
"encoding/xml"
"fmt"
"github.com/clbanning/mxj"
"net/http"
"strconv"
"strings"
)

type BucketPutOriginOptions struct {
Expand Down Expand Up @@ -35,9 +39,10 @@ type BucketOriginParameter struct {

type BucketOriginHttpHeader struct {
// 目前还不支持 FollowAllHeaders
// FollowAllHeaders bool `xml:"FollowAllHeaders,omitempty"`
NewHttpHeaders []OriginHttpHeader `xml:"NewHttpHeaders>Header,omitempty"`
FollowHttpHeaders []OriginHttpHeader `xml:"FollowHttpHeaders>Header,omitempty"`
FollowAllHeaders bool `xml:"FollowAllHeaders,omitempty"`
NewHttpHeaders []OriginHttpHeader `xml:"NewHttpHeaders>Header,omitempty"`
FollowHttpHeaders []OriginHttpHeader `xml:"FollowHttpHeaders>Header,omitempty"`
ForbidFollowHeaders []OriginHttpHeader `xml:"ForbidFollowHeaders>Header,omitempty"`
}

type OriginHttpHeader struct {
Expand All @@ -46,17 +51,109 @@ type OriginHttpHeader struct {
}

type BucketOriginInfo struct {
HostInfo string `xml:"HostInfo>HostName,omitempty"`
HostInfo *BucketOriginHostInfo `xml:"HostInfo,omitempty"`
FileInfo *BucketOriginFileInfo `xml:"FileInfo,omitempty"`
}

type BucketOriginHostInfo struct {
HostName string
Weight int64
StandbyHostName_N []string
}

type BucketOriginFileInfo struct {
PrefixDirective bool `xml:"PrefixDirective,omitempty"`
Prefix string `xml:"Prefix,omitempty"`
Suffix string `xml:"Suffix,omitempty"`
PrefixConfiguration *OriginPrefixConfiguration `xml:"PrefixConfiguration,omitempty"`
SuffixConfiguration *OriginSuffixConfiguration `xml:"SuffixConfiguration,omitempty"`
FixedFileConfiguration *OriginFixedFileConfiguration `xml:"FixedFileConfiguration,omitempty"`
}

type OriginPrefixConfiguration struct {
Prefix string `xml:"Prefix,omitempty"`
}

type OriginSuffixConfiguration struct {
Suffix string `xml:"Suffix,omitempty"`
}

type OriginFixedFileConfiguration struct {
FixedFilePath string `xml:"FixedFilePath,omitempty"`
}

type BucketGetOriginResult BucketPutOriginOptions

func (this *BucketOriginHostInfo) MarshalXML(e *xml.Encoder, start xml.StartElement) error {
if this == nil {
return nil
}
err := e.EncodeToken(start)
if err != nil {
return err
}
if this.HostName != "" {
err = e.EncodeElement(this.HostName, xml.StartElement{Name: xml.Name{Local: "HostName"}})
if err != nil {
return err
}
}
if this.Weight != 0 {
err = e.EncodeElement(this.Weight, xml.StartElement{Name: xml.Name{Local: "Weight"}})
if err != nil {
return err
}
}
for index, standByHostName := range this.StandbyHostName_N {
err = e.EncodeElement(standByHostName, xml.StartElement{Name: xml.Name{Local: fmt.Sprintf("StandbyHostName_%v", index+1)}})
if err != nil {
return err
}
}
return e.EncodeToken(xml.EndElement{Name: start.Name})
}

func (this *BucketOriginHostInfo) UnmarshalXML(d *xml.Decoder, start xml.StartElement) error {
var val struct {
XMLName xml.Name
Inner []byte `xml:",innerxml"`
}
err := d.DecodeElement(&val, &start)
if err != nil {
return err
}
str := "<HostInfo>" + string(val.Inner) + "</HostInfo>"
myMxjMap, err := mxj.NewMapXml([]byte(str))
if err != nil {
return err
}
myMap, ok := myMxjMap["HostInfo"].(map[string]interface{})
if !ok {
return fmt.Errorf("XML HostInfo Parse failed")
}

var total int
for key, value := range myMap {
if key == "HostName" {
this.HostName = value.(string)
}
if key == "Weight" {
v := value.(string)
this.Weight, err = strconv.ParseInt(v, 10, 64)
if err != nil {
return err
}
}
if strings.HasPrefix(key, "StandbyHostName_") {
total++
}
}
// 按顺序执行
for i := 1; i <= total; i++ {
key := fmt.Sprintf("StandbyHostName_%v", i)
this.StandbyHostName_N = append(this.StandbyHostName_N, myMap[key].(string))
}

return nil
}

func (s *BucketService) PutOrigin(ctx context.Context, opt *BucketPutOriginOptions) (*Response, error) {
sendOpt := &sendOptions{
baseURL: s.client.BaseURL.BucketURL,
Expand Down
8 changes: 6 additions & 2 deletions bucket_origin_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,9 @@ func TestBucketService_GetOrigin(t *testing.T) {
HttpRedirectCode: "302",
},
OriginInfo: &BucketOriginInfo{
HostInfo: "examplebucket-1250000000.cos.ap-shanghai.myqcloud.com",
HostInfo: &BucketOriginHostInfo{
HostName: "examplebucket-1250000000.cos.ap-shanghai.myqcloud.com",
},
},
},
},
Expand Down Expand Up @@ -134,7 +136,9 @@ func TestBucketService_PutOrigin(t *testing.T) {
HttpRedirectCode: "302",
},
OriginInfo: &BucketOriginInfo{
HostInfo: "examplebucket-1250000000.cos.ap-shanghai.myqcloud.com",
HostInfo: &BucketOriginHostInfo{
HostName: "examplebucket-1250000000.cos.ap-shanghai.myqcloud.com",
},
},
},
},
Expand Down
7 changes: 5 additions & 2 deletions cos.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ import (

const (
// Version current go sdk version
Version = "0.7.44"
Version = "0.7.45"
UserAgent = "cos-go-sdk-v5/" + Version
contentTypeXML = "application/xml"
defaultServiceBaseURL = "http://service.cos.myqcloud.com"
Expand Down Expand Up @@ -317,7 +317,10 @@ func (c *Client) doAPI(ctx context.Context, req *http.Request, result interface{

if result != nil {
if w, ok := result.(io.Writer); ok {
io.Copy(w, resp.Body)
_, err = io.Copy(w, resp.Body)
if err != nil { // read body failed
return response, err
}
} else {
err = xml.NewDecoder(resp.Body).Decode(result)
if err == io.EOF {
Expand Down
10 changes: 8 additions & 2 deletions example/bucket/origin.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ func main() {
HttpHeader: &cos.BucketOriginHttpHeader{
NewHttpHeaders: []cos.OriginHttpHeader{
{
Key: "x-cos-ContentType",
Key: "Content-Type",
Value: "csv",
},
},
Expand All @@ -75,7 +75,10 @@ func main() {
FollowRedirection: true,
},
OriginInfo: &cos.BucketOriginInfo{
HostInfo: "examplebucket-1250000000.cos.ap-shanghai.myqcloud.com",
HostInfo: &cos.BucketOriginHostInfo{
HostName: "examplebucket-1250000000.cos.ap-shanghai.myqcloud.com",
StandbyHostName_N: []string{"www.qq.com", "www.myqlcoud.com"},
},
},
},
},
Expand All @@ -87,6 +90,9 @@ func main() {
log_status(err)
fmt.Printf("%+v\n", res)
fmt.Printf("%+v\n", res.Rule)
for _, rule := range res.Rule {
fmt.Printf("%+v\n", rule.OriginInfo.HostInfo)
}
_, err = c.Bucket.DeleteOrigin(context.Background())
log_status(err)
}
3 changes: 0 additions & 3 deletions object.go
Original file line number Diff line number Diff line change
Expand Up @@ -494,9 +494,6 @@ func (s *ObjectService) Copy(ctx context.Context, name, sourceURL string, opt *O

if err == nil { // 请求正常
err = xml.Unmarshal(bs.Bytes(), &res) // body 正常返回
if err == io.EOF {
err = nil
}
// If the error occurs during the copy operation, the error response is embedded in the 200 OK response. This means that a 200 OK response can contain either a success or an error.
if resp != nil && resp.StatusCode == 200 {
if err != nil {
Expand Down
3 changes: 0 additions & 3 deletions object_part.go
Original file line number Diff line number Diff line change
Expand Up @@ -290,9 +290,6 @@ func (s *ObjectService) CopyPart(ctx context.Context, name, uploadID string, par

if err == nil { // 请求正常
err = xml.Unmarshal(bs.Bytes(), &res) // body 正常返回
if err == io.EOF {
err = nil
}
// If the error occurs during the copy operation, the error response is embedded in the 200 OK response. This means that a 200 OK response can contain either a success or an error.
if resp != nil && resp.StatusCode == 200 {
if err != nil {
Expand Down

0 comments on commit af6e1f5

Please sign in to comment.