From f398b20eeeb6c847c11a863a39e81c89bf285645 Mon Sep 17 00:00:00 2001 From: jojoliang Date: Wed, 7 Apr 2021 15:08:15 +0800 Subject: [PATCH] deliver init header to complete in upload --- cos.go | 2 +- helper.go | 32 ++++++++++++++++++++++++++++++++ object.go | 1 + 3 files changed, 34 insertions(+), 1 deletion(-) diff --git a/cos.go b/cos.go index 72df0bf..f6b660c 100644 --- a/cos.go +++ b/cos.go @@ -22,7 +22,7 @@ import ( const ( // Version current go sdk version - Version = "0.7.24" + Version = "0.7.25" userAgent = "cos-go-sdk-v5/" + Version contentTypeXML = "application/xml" defaultServiceBaseURL = "http://service.cos.myqcloud.com" diff --git a/helper.go b/helper.go index a7256d3..6affc2a 100644 --- a/helper.go +++ b/helper.go @@ -6,6 +6,7 @@ import ( "crypto/sha1" "errors" "fmt" + "github.com/mozillazg/go-httpheader" "hash/crc64" "io" "net/http" @@ -260,3 +261,34 @@ func FormatRangeOptions(opt *RangeOptions) string { } return "bytes=-" } + +var deliverHeader = map[string]bool{} + +func isDeliverHeader(key string) bool { + for k, v := range deliverHeader { + if key == k && v { + return true + } + } + return strings.HasPrefix(key, privateHeaderPrefix) +} + +func deliverInitOptions(opt *InitiateMultipartUploadOptions) (*http.Header, error) { + if opt == nil { + return nil, nil + } + h, err := httpheader.Header(opt) + if err != nil { + return nil, err + } + header := &http.Header{} + for key, values := range h { + key = strings.ToLower(key) + if isDeliverHeader(key) { + for _, value := range values { + header.Add(key, value) + } + } + } + return header, nil +} diff --git a/object.go b/object.go index 4aaeced..f3276be 100644 --- a/object.go +++ b/object.go @@ -914,6 +914,7 @@ func (s *ObjectService) Upload(ctx context.Context, name string, filepath string var consumedBytes int64 if opt.OptIni != nil { listener = opt.OptIni.Listener + optcom.XOptionHeader, _ = deliverInitOptions(opt.OptIni) } event := newProgressEvent(ProgressStartedEvent, 0, 0, totalBytes) progressCallback(listener, event)