Skip to content

Commit da7dafc

Browse files
author
jojoliang
committed
update download and put bucket
1 parent 4d15821 commit da7dafc

File tree

3 files changed

+114
-5
lines changed

3 files changed

+114
-5
lines changed

bucket.go

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,19 @@ func (s *BucketService) Get(ctx context.Context, opt *BucketGetOptions) (*Bucket
5050
}
5151

5252
// BucketPutOptions is same to the ACLHeaderOptions
53-
type BucketPutOptions ACLHeaderOptions
53+
type BucketPutOptions struct {
54+
XCosACL string `header:"x-cos-acl,omitempty" url:"-" xml:"-"`
55+
XCosGrantRead string `header:"x-cos-grant-read,omitempty" url:"-" xml:"-"`
56+
XCosGrantWrite string `header:"x-cos-grant-write,omitempty" url:"-" xml:"-"`
57+
XCosGrantFullControl string `header:"x-cos-grant-full-control,omitempty" url:"-" xml:"-"`
58+
XCosGrantReadACP string `header:"x-cos-grant-read-acp,omitempty" url:"-" xml:"-"`
59+
XCosGrantWriteACP string `header:"x-cos-grant-write-acp,omitempty" url:"-" xml:"-"`
60+
CreateBucketConfiguration *CreateBucketConfiguration `header:"-" url:"-" xml:"-"`
61+
}
62+
type CreateBucketConfiguration struct {
63+
XMLName xml.Name `xml:"CreateBucketConfiguration"`
64+
BucketAZConfig string `xml:"BucketAZConfig,omitempty"`
65+
}
5466

5567
// Put Bucket请求可以在指定账号下创建一个Bucket。
5668
//
@@ -62,6 +74,9 @@ func (s *BucketService) Put(ctx context.Context, opt *BucketPutOptions) (*Respon
6274
method: http.MethodPut,
6375
optHeader: opt,
6476
}
77+
if opt != nil && opt.CreateBucketConfiguration != nil {
78+
sendOpt.body = opt.CreateBucketConfiguration
79+
}
6580
resp, err := s.client.send(ctx, &sendOpt)
6681
return resp, err
6782
}

object.go

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1239,7 +1239,10 @@ func (s *ObjectService) Download(ctx context.Context, name string, filepath stri
12391239
if opt.CheckPoint {
12401240
cpfd.Truncate(0)
12411241
cpfd.Seek(0, os.SEEK_SET)
1242-
resumableInfo.DownloadedBlocks = append(resumableInfo.DownloadedBlocks, DownloadedBlock{From: chunks[i].OffSet, To: chunks[i].OffSet + chunks[i].Size - 1})
1242+
resumableInfo.DownloadedBlocks = append(resumableInfo.DownloadedBlocks, DownloadedBlock{
1243+
From: chunks[res.PartNumber-1].OffSet,
1244+
To: chunks[res.PartNumber-1].OffSet + chunks[res.PartNumber-1].Size - 1,
1245+
})
12431246
json.NewEncoder(cpfd).Encode(resumableInfo)
12441247
}
12451248
}
@@ -1250,7 +1253,7 @@ func (s *ObjectService) Download(ctx context.Context, name string, filepath stri
12501253
if err != nil {
12511254
return nil, err
12521255
}
1253-
// 下载成功,删除checkpoint文件
1256+
// 下载成功,删除checkpoint文件
12541257
if opt.CheckPoint {
12551258
os.Remove(cpfile)
12561259
}

object_test.go

Lines changed: 93 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -564,6 +564,7 @@ func TestObjectService_Upload2(t *testing.T) {
564564
}
565565
}
566566

567+
/*
567568
func TestObjectService_Download(t *testing.T) {
568569
setup()
569570
defer teardown()
@@ -636,7 +637,97 @@ func TestObjectService_Download(t *testing.T) {
636637
t.Fatalf("Object.Upload returned error: %v", err)
637638
}
638639
}
640+
*/
641+
func TestObjectService_DownloadWithCheckPoint(t *testing.T) {
642+
setup()
643+
defer teardown()
644+
645+
filePath := "rsp.file" + time.Now().Format(time.RFC3339)
646+
newfile, err := os.Create(filePath)
647+
if err != nil {
648+
t.Fatalf("create tmp file failed")
649+
}
650+
defer os.Remove(filePath)
651+
// 源文件内容
652+
totalBytes := int64(1024*1024*9 + 123)
653+
partSize := 1024 * 1024
654+
b := make([]byte, totalBytes)
655+
_, err = rand.Read(b)
656+
newfile.Write(b)
657+
newfile.Close()
658+
tb := crc64.MakeTable(crc64.ECMA)
659+
localcrc := strconv.FormatUint(crc64.Update(0, tb, b), 10)
639660

661+
oddok := false
662+
var oddcount, evencount int
663+
mux.HandleFunc("/test.go.download", func(w http.ResponseWriter, r *http.Request) {
664+
if r.Method == http.MethodHead {
665+
w.Header().Add("Content-Length", strconv.FormatInt(totalBytes, 10))
666+
w.Header().Add("x-cos-hash-crc64ecma", localcrc)
667+
return
668+
}
669+
strRange := r.Header.Get("Range")
670+
slice1 := strings.Split(strRange, "=")
671+
slice2 := strings.Split(slice1[1], "-")
672+
start, _ := strconv.ParseInt(slice2[0], 10, 64)
673+
end, _ := strconv.ParseInt(slice2[1], 10, 64)
674+
if (start/int64(partSize))%2 == 1 {
675+
if oddok {
676+
io.Copy(w, bytes.NewBuffer(b[start:end+1]))
677+
} else {
678+
// 数据校验失败, Download不会做重试
679+
io.Copy(w, bytes.NewBuffer(b[start:end]))
680+
}
681+
oddcount++
682+
} else {
683+
io.Copy(w, bytes.NewBuffer(b[start:end+1]))
684+
evencount++
685+
}
686+
})
687+
688+
opt := &MultiDownloadOptions{
689+
ThreadPoolSize: 3,
690+
PartSize: 1,
691+
CheckPoint: true,
692+
}
693+
downPath := "down.file" + time.Now().Format(time.RFC3339)
694+
defer os.Remove(downPath)
695+
_, err = client.Object.Download(context.Background(), "test.go.download", downPath, opt)
696+
if err == nil {
697+
// 偶数块下载完成,奇数块下载失败
698+
t.Fatalf("Object.Download returned error: %v", err)
699+
}
700+
fd, err := os.Open(downPath)
701+
if err != nil {
702+
t.Fatalf("Object Download Open File Failed:%v", err)
703+
}
704+
offset := 0
705+
for i := 0; i < 10; i++ {
706+
bs, _ := ioutil.ReadAll(io.LimitReader(fd, int64(partSize)))
707+
offset += len(bs)
708+
if i%2 == 1 {
709+
bs[len(bs)-1] = b[offset-1]
710+
}
711+
if bytes.Compare(bs, b[i*partSize:offset]) != 0 {
712+
t.Fatalf("Compare Error, index:%v, len:%v, offset:%v", i, len(bs), offset)
713+
}
714+
}
715+
fd.Close()
716+
717+
if oddcount != 5 || evencount != 5 {
718+
t.Fatalf("Object.Download failed, odd:%v, even:%v", oddcount, evencount)
719+
}
720+
// 设置奇数块OK
721+
oddok = true
722+
_, err = client.Object.Download(context.Background(), "test.go.download", downPath, opt)
723+
if err != nil {
724+
// 下载成功
725+
t.Fatalf("Object.Download returned error: %v", err)
726+
}
727+
if oddcount != 10 || evencount != 5 {
728+
t.Fatalf("Object.Download failed, odd:%v, even:%v", oddcount, evencount)
729+
}
730+
}
640731
func TestObjectService_GetTagging(t *testing.T) {
641732
setup()
642733
defer teardown()
@@ -666,7 +757,7 @@ func TestObjectService_GetTagging(t *testing.T) {
666757
t.Fatalf("Object.GetTagging returned error %v", err)
667758
}
668759

669-
want := &ObjectGetTaggingResult{
760+
want := &ObjectGetTaggingResult{
670761
XMLName: xml.Name{Local: "Tagging"},
671762
TagSet: []ObjectTaggingTag{
672763
{"test_k2", "test_v2"},
@@ -735,7 +826,7 @@ func TestObjectService_DeleteTagging(t *testing.T) {
735826
w.WriteHeader(http.StatusNoContent)
736827
})
737828

738-
_, err := client.Object.DeleteTagging(context.Background(), "test")
829+
_, err := client.Object.DeleteTagging(context.Background(), "test")
739830
if err != nil {
740831
t.Fatalf("Object.DeleteTagging returned error: %v", err)
741832
}

0 commit comments

Comments
 (0)