Skip to content

Commit 20aaf0e

Browse files
authored
Merge pull request #108 from agin719/cos-v4-dev
Cos v4 dev
2 parents 73c440c + 45e6cb6 commit 20aaf0e

File tree

4 files changed

+39
-6
lines changed

4 files changed

+39
-6
lines changed

cos.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ import (
2222

2323
const (
2424
// Version current go sdk version
25-
Version = "0.7.20"
25+
Version = "0.7.21"
2626
userAgent = "cos-go-sdk-v5/" + Version
2727
contentTypeXML = "application/xml"
2828
defaultServiceBaseURL = "http://service.cos.myqcloud.com"

example/object/upload.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,8 @@ func main() {
5151

5252
// Case1 多线程上传对象
5353
opt := &cos.MultiUploadOptions{
54-
ThreadPoolSize: 3,
54+
EnableVerification: true,
55+
ThreadPoolSize: 5,
5556
}
5657
v, _, err := c.Object.Upload(
5758
context.Background(), "gomulput1G", "./test1G", opt,
@@ -71,4 +72,5 @@ func main() {
7172
)
7273
log_status(err)
7374
fmt.Printf("Case2 done, %v\n", v)
75+
7476
}

helper.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66
"crypto/sha1"
77
"errors"
88
"fmt"
9+
"hash/crc64"
910
"io"
1011
"net/http"
1112
"net/url"
@@ -33,6 +34,17 @@ func calSHA1Digest(msg []byte) []byte {
3334
return m.Sum(nil)
3435
}
3536

37+
func calCRC64(fd io.Reader) (uint64, error) {
38+
tb := crc64.MakeTable(crc64.ECMA)
39+
hash := crc64.New(tb)
40+
_, err := io.Copy(hash, fd)
41+
if err != nil {
42+
return 0, err
43+
}
44+
sum := hash.Sum64()
45+
return sum, nil
46+
}
47+
3648
// cloneRequest returns a clone of the provided *http.Request. The clone is a
3749
// shallow copy of the struct and its Header map.
3850
func cloneRequest(r *http.Request) *http.Request {

object.go

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -524,10 +524,11 @@ type Object struct {
524524
// MultiUploadOptions is the option of the multiupload,
525525
// ThreadPoolSize default is one
526526
type MultiUploadOptions struct {
527-
OptIni *InitiateMultipartUploadOptions
528-
PartSize int64
529-
ThreadPoolSize int
530-
CheckPoint bool
527+
OptIni *InitiateMultipartUploadOptions
528+
PartSize int64
529+
ThreadPoolSize int
530+
CheckPoint bool
531+
EnableVerification bool
531532
}
532533

533534
type Chunk struct {
@@ -738,11 +739,21 @@ func (s *ObjectService) Upload(ctx context.Context, name string, filepath string
738739
if opt == nil {
739740
opt = &MultiUploadOptions{}
740741
}
742+
var localcrc uint64
741743
// 1.Get the file chunk
742744
totalBytes, chunks, partNum, err := SplitFileIntoChunks(filepath, opt.PartSize)
743745
if err != nil {
744746
return nil, nil, err
745747
}
748+
// 校验
749+
if opt.EnableVerification {
750+
fd, err := os.Open(filepath)
751+
if err != nil {
752+
return nil, nil, err
753+
}
754+
defer fd.Close()
755+
localcrc, err = calCRC64(fd)
756+
}
746757
// filesize=0 , use simple upload
747758
if partNum == 0 {
748759
var opt0 *ObjectPutOptions
@@ -881,8 +892,16 @@ func (s *ObjectService) Upload(ctx context.Context, name string, filepath string
881892
v, resp, err := s.CompleteMultipartUpload(context.Background(), name, uploadID, optcom)
882893
if err != nil {
883894
s.AbortMultipartUpload(ctx, name, uploadID)
895+
return v, resp, err
884896
}
885897

898+
if resp != nil && opt.EnableVerification {
899+
scoscrc := resp.Header.Get("x-cos-hash-crc64ecma")
900+
icoscrc, _ := strconv.ParseUint(scoscrc, 10, 64)
901+
if icoscrc != localcrc {
902+
return v, resp, fmt.Errorf("verification failed, want:%v, return:%v", localcrc, icoscrc)
903+
}
904+
}
886905
return v, resp, err
887906
}
888907

0 commit comments

Comments
 (0)