Skip to content

Commit 750a7a6

Browse files
authored
Merge pull request #146 from agin719/cos-v4-dev
Cos v4 dev
2 parents cc2aa8a + fd0f5ed commit 750a7a6

File tree

7 files changed

+141
-137
lines changed

7 files changed

+141
-137
lines changed

bucket_domain.go

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,16 @@ import (
66
"net/http"
77
)
88

9+
type BucketDomainRule struct {
10+
Status string `xml:"Status,omitempty"`
11+
Name string `xml:"Name,omitempty"`
12+
Type string `xml:"Type,omitempty"`
13+
ForcedReplacement string `xml:"ForcedReplacement,omitempty"`
14+
}
15+
916
type BucketPutDomainOptions struct {
10-
XMLName xml.Name `xml:"DomainConfiguration"`
11-
Status string `xml:"DomainRule>Status"`
12-
Name string `xml:"DomainRule>Name"`
13-
Type string `xml:"DomainRule>Type"`
14-
ForcedReplacement string `xml:"DomainRule>ForcedReplacement,omitempty"`
17+
XMLName xml.Name `xml:"DomainConfiguration"`
18+
Rules []BucketDomainRule `xml:"DomainRule,omitempty"`
1519
}
1620
type BucketGetDomainResult BucketPutDomainOptions
1721

@@ -37,3 +41,13 @@ func (s *BucketService) GetDomain(ctx context.Context) (*BucketGetDomainResult,
3741
resp, err := s.client.doRetry(ctx, sendOpt)
3842
return &res, resp, err
3943
}
44+
45+
func (s *BucketService) DeleteDomain(ctx context.Context) (*Response, error) {
46+
sendOpt := &sendOptions{
47+
baseURL: s.client.BaseURL.BucketURL,
48+
uri: "/?domain",
49+
method: http.MethodDelete,
50+
}
51+
resp, err := s.client.doRetry(ctx, sendOpt)
52+
return resp, err
53+
}

bucket_domain_test.go

Lines changed: 21 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -41,11 +41,15 @@ func TestBucketService_GetDomain(t *testing.T) {
4141
}
4242

4343
want := &BucketGetDomainResult{
44-
XMLName: xml.Name{Local: "DomainConfiguration"},
45-
Status: "ENABLED",
46-
Name: "www.abc.com",
47-
Type: "REST",
48-
ForcedReplacement: "CNAME",
44+
XMLName: xml.Name{Local: "DomainConfiguration"},
45+
Rules: []BucketDomainRule{
46+
{
47+
Status: "ENABLED",
48+
Name: "www.abc.com",
49+
Type: "REST",
50+
ForcedReplacement: "CNAME",
51+
},
52+
},
4953
}
5054

5155
if !reflect.DeepEqual(res, want) {
@@ -58,11 +62,15 @@ func TestBucketService_PutDomain(t *testing.T) {
5862
defer teardown()
5963

6064
opt := &BucketPutDomainOptions{
61-
XMLName: xml.Name{Local: "DomainConfiguration"},
62-
Status: "ENABLED",
63-
Name: "www.abc.com",
64-
Type: "REST",
65-
ForcedReplacement: "CNAME",
65+
XMLName: xml.Name{Local: "DomainConfiguration"},
66+
Rules: []BucketDomainRule{
67+
{
68+
Status: "ENABLED",
69+
Name: "www.abc.com",
70+
Type: "REST",
71+
ForcedReplacement: "CNAME",
72+
},
73+
},
6674
}
6775

6876
rt := 0
@@ -95,35 +103,18 @@ func TestBucketService_DeleteDomain(t *testing.T) {
95103
setup()
96104
defer teardown()
97105

98-
opt := &BucketPutDomainOptions{}
99-
100-
rt := 0
101106
mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
102-
testMethod(t, r, http.MethodPut)
107+
testMethod(t, r, http.MethodDelete)
103108
vs := values{
104109
"domain": "",
105110
}
106111
testFormValues(t, r, vs)
107-
108-
rt++
109-
if rt < 3 {
110-
w.WriteHeader(http.StatusGatewayTimeout)
111-
return
112-
}
113-
body := new(BucketPutDomainOptions)
114-
xml.NewDecoder(r.Body).Decode(body)
115-
want := opt
116-
want.XMLName = xml.Name{Local: "DomainConfiguration"}
117-
if !reflect.DeepEqual(body, want) {
118-
t.Errorf("Bucket.PutDomain request\n body: %+v\n, want %+v\n", body, want)
119-
}
120-
121112
w.WriteHeader(http.StatusNoContent)
122113
})
123114

124-
_, err := client.Bucket.PutDomain(context.Background(), opt)
115+
_, err := client.Bucket.DeleteDomain(context.Background())
125116
if err != nil {
126-
t.Fatalf("Bucket.PutDomain returned error: %v", err)
117+
t.Fatalf("Bucket.DeleteDomain returned error: %v", err)
127118
}
128119

129120
}

example/bucket/domain.go

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
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("WARN: Resource is not existed")
21+
} else if e, ok := cos.IsCOSError(err); ok {
22+
fmt.Printf("ERROR: Code: %v\n", e.Code)
23+
fmt.Printf("ERROR: Message: %v\n", e.Message)
24+
fmt.Printf("ERROR: Resource: %v\n", e.Resource)
25+
fmt.Printf("ERROR: RequestId: %v\n", e.RequestID)
26+
// ERROR
27+
} else {
28+
fmt.Printf("ERROR: %v\n", 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.BucketPutDomainOptions{
52+
Rules: []cos.BucketDomainRule{
53+
{
54+
Status: "ENABLED",
55+
Name: "www.qq.com",
56+
Type: "REST",
57+
ForcedReplacement: "CNAME",
58+
},
59+
},
60+
}
61+
62+
_, err := c.Bucket.PutDomain(context.Background(), opt)
63+
log_status(err)
64+
65+
res, _, err := c.Bucket.GetDomain(context.Background())
66+
log_status(err)
67+
fmt.Printf("%+v\n", res)
68+
69+
_, err = c.Bucket.DeleteDomain(context.Background())
70+
log_status(err)
71+
}

example/bucket/getDomain.go

Lines changed: 0 additions & 35 deletions
This file was deleted.

example/bucket/putDomain.go

Lines changed: 0 additions & 42 deletions
This file was deleted.

object.go

Lines changed: 18 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -732,6 +732,7 @@ func worker(ctx context.Context, s *ObjectService, jobs <-chan *Jobs, results ch
732732
results <- &res
733733
break
734734
}
735+
time.Sleep(time.Millisecond)
735736
continue
736737
}
737738
results <- &res
@@ -757,26 +758,32 @@ func downloadWorker(ctx context.Context, s *ObjectService, jobs <-chan *Jobs, re
757758
res.err = err
758759
res.Resp = resp
759760
if err != nil {
760-
rt--
761-
if rt == 0 {
762-
results <- &res
763-
break
764-
}
765-
continue
761+
results <- &res
762+
break
766763
}
767-
defer resp.Body.Close()
768764
fd, err := os.OpenFile(j.FilePath, os.O_WRONLY, 0660)
769765
if err != nil {
766+
resp.Body.Close()
770767
res.err = err
771768
results <- &res
772769
break
773770
}
774771
fd.Seek(j.Chunk.OffSet, os.SEEK_SET)
775772
n, err := io.Copy(fd, LimitReadCloser(resp.Body, j.Chunk.Size))
776773
if n != j.Chunk.Size || err != nil {
777-
res.err = fmt.Errorf("io.Copy Failed, nread:%v, want:%v, err:%v", n, j.Chunk.Size, err)
774+
fd.Close()
775+
resp.Body.Close()
776+
rt--
777+
if rt == 0 {
778+
res.err = fmt.Errorf("io.Copy Failed, nread:%v, want:%v, err:%v", n, j.Chunk.Size, err)
779+
results <- &res
780+
break
781+
}
782+
time.Sleep(time.Millisecond)
783+
continue
778784
}
779785
fd.Close()
786+
resp.Body.Close()
780787
results <- &res
781788
break
782789
}
@@ -818,7 +825,7 @@ func SplitFileIntoChunks(filePath string, partSize int64) (int64, []Chunk, int,
818825
return 0, nil, 0, errors.New("Too many parts, out of 10000")
819826
}
820827
} else {
821-
partNum, partSize = DividePart(stat.Size(), 64)
828+
partNum, partSize = DividePart(stat.Size(), 16)
822829
}
823830

824831
var chunks []Chunk
@@ -1119,7 +1126,7 @@ func SplitSizeIntoChunks(totalBytes int64, partSize int64) ([]Chunk, int, error)
11191126
return nil, 0, errors.New("Too manry parts, out of 10000")
11201127
}
11211128
} else {
1122-
partNum, partSize = DividePart(totalBytes, 64)
1129+
partNum, partSize = DividePart(totalBytes, 16)
11231130
}
11241131

11251132
var chunks []Chunk
@@ -1293,7 +1300,7 @@ func (s *ObjectService) Download(ctx context.Context, name string, filepath stri
12931300
}
12941301
job := &Jobs{
12951302
Name: name,
1296-
RetryTimes: 1,
1303+
RetryTimes: 3,
12971304
FilePath: filepath,
12981305
Chunk: chunk,
12991306
DownOpt: &downOpt,

0 commit comments

Comments
 (0)