Skip to content

Commit a2c02dc

Browse files
author
jojoliang
committed
fix copy object, update bucket origin
1 parent 3cb0aa9 commit a2c02dc

File tree

6 files changed

+123
-19
lines changed

6 files changed

+123
-19
lines changed

bucket_origin.go

Lines changed: 104 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,11 @@ package cos
33
import (
44
"context"
55
"encoding/xml"
6+
"fmt"
7+
"github.com/clbanning/mxj"
68
"net/http"
9+
"strconv"
10+
"strings"
711
)
812

913
type BucketPutOriginOptions struct {
@@ -35,9 +39,10 @@ type BucketOriginParameter struct {
3539

3640
type BucketOriginHttpHeader struct {
3741
// 目前还不支持 FollowAllHeaders
38-
// FollowAllHeaders bool `xml:"FollowAllHeaders,omitempty"`
39-
NewHttpHeaders []OriginHttpHeader `xml:"NewHttpHeaders>Header,omitempty"`
40-
FollowHttpHeaders []OriginHttpHeader `xml:"FollowHttpHeaders>Header,omitempty"`
42+
FollowAllHeaders bool `xml:"FollowAllHeaders,omitempty"`
43+
NewHttpHeaders []OriginHttpHeader `xml:"NewHttpHeaders>Header,omitempty"`
44+
FollowHttpHeaders []OriginHttpHeader `xml:"FollowHttpHeaders>Header,omitempty"`
45+
ForbidFollowHeaders []OriginHttpHeader `xml:"ForbidFollowHeaders>Header,omitempty"`
4146
}
4247

4348
type OriginHttpHeader struct {
@@ -46,17 +51,109 @@ type OriginHttpHeader struct {
4651
}
4752

4853
type BucketOriginInfo struct {
49-
HostInfo string `xml:"HostInfo>HostName,omitempty"`
54+
HostInfo *BucketOriginHostInfo `xml:"HostInfo,omitempty"`
5055
FileInfo *BucketOriginFileInfo `xml:"FileInfo,omitempty"`
5156
}
57+
58+
type BucketOriginHostInfo struct {
59+
HostName string
60+
Weight int64
61+
StandbyHostName_N []string
62+
}
63+
5264
type BucketOriginFileInfo struct {
53-
PrefixDirective bool `xml:"PrefixDirective,omitempty"`
54-
Prefix string `xml:"Prefix,omitempty"`
55-
Suffix string `xml:"Suffix,omitempty"`
65+
PrefixConfiguration *OriginPrefixConfiguration `xml:"PrefixConfiguration,omitempty"`
66+
SuffixConfiguration *OriginSuffixConfiguration `xml:"SuffixConfiguration,omitempty"`
67+
FixedFileConfiguration *OriginFixedFileConfiguration `xml:"FixedFileConfiguration,omitempty"`
68+
}
69+
70+
type OriginPrefixConfiguration struct {
71+
Prefix string `xml:"Prefix,omitempty"`
72+
}
73+
74+
type OriginSuffixConfiguration struct {
75+
Suffix string `xml:"Suffix,omitempty"`
76+
}
77+
78+
type OriginFixedFileConfiguration struct {
79+
FixedFilePath string `xml:"FixedFilePath,omitempty"`
5680
}
5781

5882
type BucketGetOriginResult BucketPutOriginOptions
5983

84+
func (this *BucketOriginHostInfo) MarshalXML(e *xml.Encoder, start xml.StartElement) error {
85+
if this == nil {
86+
return nil
87+
}
88+
err := e.EncodeToken(start)
89+
if err != nil {
90+
return err
91+
}
92+
if this.HostName != "" {
93+
err = e.EncodeElement(this.HostName, xml.StartElement{Name: xml.Name{Local: "HostName"}})
94+
if err != nil {
95+
return err
96+
}
97+
}
98+
if this.Weight != 0 {
99+
err = e.EncodeElement(this.Weight, xml.StartElement{Name: xml.Name{Local: "Weight"}})
100+
if err != nil {
101+
return err
102+
}
103+
}
104+
for index, standByHostName := range this.StandbyHostName_N {
105+
err = e.EncodeElement(standByHostName, xml.StartElement{Name: xml.Name{Local: fmt.Sprintf("StandbyHostName_%v", index+1)}})
106+
if err != nil {
107+
return err
108+
}
109+
}
110+
return e.EncodeToken(xml.EndElement{Name: start.Name})
111+
}
112+
113+
func (this *BucketOriginHostInfo) UnmarshalXML(d *xml.Decoder, start xml.StartElement) error {
114+
var val struct {
115+
XMLName xml.Name
116+
Inner []byte `xml:",innerxml"`
117+
}
118+
err := d.DecodeElement(&val, &start)
119+
if err != nil {
120+
return err
121+
}
122+
str := "<HostInfo>" + string(val.Inner) + "</HostInfo>"
123+
myMxjMap, err := mxj.NewMapXml([]byte(str))
124+
if err != nil {
125+
return err
126+
}
127+
myMap, ok := myMxjMap["HostInfo"].(map[string]interface{})
128+
if !ok {
129+
return fmt.Errorf("XML HostInfo Parse failed")
130+
}
131+
132+
var total int
133+
for key, value := range myMap {
134+
if key == "HostName" {
135+
this.HostName = value.(string)
136+
}
137+
if key == "Weight" {
138+
v := value.(string)
139+
this.Weight, err = strconv.ParseInt(v, 10, 64)
140+
if err != nil {
141+
return err
142+
}
143+
}
144+
if strings.HasPrefix(key, "StandbyHostName_") {
145+
total++
146+
}
147+
}
148+
// 按顺序执行
149+
for i := 1; i <= total; i++ {
150+
key := fmt.Sprintf("StandbyHostName_%v", i)
151+
this.StandbyHostName_N = append(this.StandbyHostName_N, myMap[key].(string))
152+
}
153+
154+
return nil
155+
}
156+
60157
func (s *BucketService) PutOrigin(ctx context.Context, opt *BucketPutOriginOptions) (*Response, error) {
61158
sendOpt := &sendOptions{
62159
baseURL: s.client.BaseURL.BucketURL,

bucket_origin_test.go

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,9 @@ func TestBucketService_GetOrigin(t *testing.T) {
9090
HttpRedirectCode: "302",
9191
},
9292
OriginInfo: &BucketOriginInfo{
93-
HostInfo: "examplebucket-1250000000.cos.ap-shanghai.myqcloud.com",
93+
HostInfo: &BucketOriginHostInfo{
94+
HostName: "examplebucket-1250000000.cos.ap-shanghai.myqcloud.com",
95+
},
9496
},
9597
},
9698
},
@@ -134,7 +136,9 @@ func TestBucketService_PutOrigin(t *testing.T) {
134136
HttpRedirectCode: "302",
135137
},
136138
OriginInfo: &BucketOriginInfo{
137-
HostInfo: "examplebucket-1250000000.cos.ap-shanghai.myqcloud.com",
139+
HostInfo: &BucketOriginHostInfo{
140+
HostName: "examplebucket-1250000000.cos.ap-shanghai.myqcloud.com",
141+
},
138142
},
139143
},
140144
},

cos.go

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ import (
2424

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

318318
if result != nil {
319319
if w, ok := result.(io.Writer); ok {
320-
io.Copy(w, resp.Body)
320+
_, err = io.Copy(w, resp.Body)
321+
if err != nil { // read body failed
322+
return response, err
323+
}
321324
} else {
322325
err = xml.NewDecoder(resp.Body).Decode(result)
323326
if err == io.EOF {

example/bucket/origin.go

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ func main() {
6262
HttpHeader: &cos.BucketOriginHttpHeader{
6363
NewHttpHeaders: []cos.OriginHttpHeader{
6464
{
65-
Key: "x-cos-ContentType",
65+
Key: "Content-Type",
6666
Value: "csv",
6767
},
6868
},
@@ -75,7 +75,10 @@ func main() {
7575
FollowRedirection: true,
7676
},
7777
OriginInfo: &cos.BucketOriginInfo{
78-
HostInfo: "examplebucket-1250000000.cos.ap-shanghai.myqcloud.com",
78+
HostInfo: &cos.BucketOriginHostInfo{
79+
HostName: "examplebucket-1250000000.cos.ap-shanghai.myqcloud.com",
80+
StandbyHostName_N: []string{"www.qq.com", "www.myqlcoud.com"},
81+
},
7982
},
8083
},
8184
},
@@ -87,6 +90,9 @@ func main() {
8790
log_status(err)
8891
fmt.Printf("%+v\n", res)
8992
fmt.Printf("%+v\n", res.Rule)
93+
for _, rule := range res.Rule {
94+
fmt.Printf("%+v\n", rule.OriginInfo.HostInfo)
95+
}
9096
_, err = c.Bucket.DeleteOrigin(context.Background())
9197
log_status(err)
9298
}

object.go

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -494,9 +494,6 @@ func (s *ObjectService) Copy(ctx context.Context, name, sourceURL string, opt *O
494494

495495
if err == nil { // 请求正常
496496
err = xml.Unmarshal(bs.Bytes(), &res) // body 正常返回
497-
if err == io.EOF {
498-
err = nil
499-
}
500497
// 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.
501498
if resp != nil && resp.StatusCode == 200 {
502499
if err != nil {

object_part.go

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -290,9 +290,6 @@ func (s *ObjectService) CopyPart(ctx context.Context, name, uploadID string, par
290290

291291
if err == nil { // 请求正常
292292
err = xml.Unmarshal(bs.Bytes(), &res) // body 正常返回
293-
if err == io.EOF {
294-
err = nil
295-
}
296293
// 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.
297294
if resp != nil && resp.StatusCode == 200 {
298295
if err != nil {

0 commit comments

Comments
 (0)