Skip to content

Commit

Permalink
add upload verification
Browse files Browse the repository at this point in the history
  • Loading branch information
jojoliang committed Mar 8, 2021
1 parent 685c58a commit 2bfb93f
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 6 deletions.
2 changes: 1 addition & 1 deletion cos.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import (

const (
// Version current go sdk version
Version = "0.7.20"
Version = "0.7.21"
userAgent = "cos-go-sdk-v5/" + Version
contentTypeXML = "application/xml"
defaultServiceBaseURL = "http://service.cos.myqcloud.com"
Expand Down
4 changes: 3 additions & 1 deletion example/object/upload.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,8 @@ func main() {

// Case1 多线程上传对象
opt := &cos.MultiUploadOptions{
ThreadPoolSize: 3,
EnableVerification: true,
ThreadPoolSize: 5,
}
v, _, err := c.Object.Upload(
context.Background(), "gomulput1G", "./test1G", opt,
Expand All @@ -71,4 +72,5 @@ func main() {
)
log_status(err)
fmt.Printf("Case2 done, %v\n", v)

}
12 changes: 12 additions & 0 deletions helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"crypto/sha1"
"errors"
"fmt"
"hash/crc64"
"io"
"net/http"
"net/url"
Expand Down Expand Up @@ -33,6 +34,17 @@ func calSHA1Digest(msg []byte) []byte {
return m.Sum(nil)
}

func calCRC64(fd io.Reader) (uint64, error) {
tb := crc64.MakeTable(crc64.ECMA)
hash := crc64.New(tb)
_, err := io.Copy(hash, fd)
if err != nil {
return 0, err
}
sum := hash.Sum64()
return sum, nil
}

// cloneRequest returns a clone of the provided *http.Request. The clone is a
// shallow copy of the struct and its Header map.
func cloneRequest(r *http.Request) *http.Request {
Expand Down
27 changes: 23 additions & 4 deletions object.go
Original file line number Diff line number Diff line change
Expand Up @@ -524,10 +524,11 @@ type Object struct {
// MultiUploadOptions is the option of the multiupload,
// ThreadPoolSize default is one
type MultiUploadOptions struct {
OptIni *InitiateMultipartUploadOptions
PartSize int64
ThreadPoolSize int
CheckPoint bool
OptIni *InitiateMultipartUploadOptions
PartSize int64
ThreadPoolSize int
CheckPoint bool
EnableVerification bool
}

type Chunk struct {
Expand Down Expand Up @@ -738,11 +739,21 @@ func (s *ObjectService) Upload(ctx context.Context, name string, filepath string
if opt == nil {
opt = &MultiUploadOptions{}
}
var localcrc uint64
// 1.Get the file chunk
totalBytes, chunks, partNum, err := SplitFileIntoChunks(filepath, opt.PartSize)
if err != nil {
return nil, nil, err
}
// 校验
if opt.EnableVerification {
fd, err := os.Open(filepath)
if err != nil {
return nil, nil, err
}
defer fd.Close()
localcrc, err = calCRC64(fd)
}
// filesize=0 , use simple upload
if partNum == 0 {
var opt0 *ObjectPutOptions
Expand Down Expand Up @@ -881,8 +892,16 @@ func (s *ObjectService) Upload(ctx context.Context, name string, filepath string
v, resp, err := s.CompleteMultipartUpload(context.Background(), name, uploadID, optcom)
if err != nil {
s.AbortMultipartUpload(ctx, name, uploadID)
return v, resp, err
}

if opt.EnableVerification {
scoscrc := resp.Header.Get("x-cos-hash-crc64ecma")
icoscrc, _ := strconv.ParseUint(scoscrc, 10, 64)
if icoscrc != localcrc {
return v, resp, fmt.Errorf("verification failed, want:%v, return:%v", localcrc, icoscrc)
}
}
return v, resp, err
}

Expand Down

0 comments on commit 2bfb93f

Please sign in to comment.