Skip to content

Commit

Permalink
multicopy source url编码
Browse files Browse the repository at this point in the history
  • Loading branch information
jojoliang committed Jun 4, 2024
1 parent 72c2ed4 commit b537ff2
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 16 deletions.
2 changes: 1 addition & 1 deletion cos.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ import (

const (
// Version current go sdk version
Version = "0.7.48"
Version = "0.7.49"
UserAgent = "cos-go-sdk-v5/" + Version
contentTypeXML = "application/xml"
defaultServiceBaseURL = "http://service.cos.myqcloud.com"
Expand Down
9 changes: 9 additions & 0 deletions helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -301,6 +301,15 @@ func CloneCompleteMultipartUploadOptions(opt *CompleteMultipartUploadOptions) *C
return &res
}

func cloneObjectCopyPartOptions(opt *ObjectCopyPartOptions) *ObjectCopyPartOptions {
var res ObjectCopyPartOptions
if opt != nil {
res = *opt
res.XOptionHeader = cloneHeader(opt.XOptionHeader)
}
return &res
}

type RangeOptions struct {
HasStart bool
HasEnd bool
Expand Down
36 changes: 21 additions & 15 deletions object_part.go
Original file line number Diff line number Diff line change
Expand Up @@ -338,11 +338,25 @@ type CopyPartResult struct {
//
// https://www.qcloud.com/document/product/436/7750
func (s *ObjectService) CopyPart(ctx context.Context, name, uploadID string, partNumber int, sourceURL string, opt *ObjectCopyPartOptions) (*CopyPartResult, *Response, error) {
if opt == nil {
opt = &ObjectCopyPartOptions{}
if strings.HasPrefix(sourceURL, "http://") || strings.HasPrefix(sourceURL, "https://") {
return nil, nil, errors.New("sourceURL format is invalid.")
}
opt.XCosCopySource = sourceURL
u := fmt.Sprintf("/%s?partNumber=%d&uploadId=%s", encodeURIComponent(name), partNumber, uploadID)
surl := strings.SplitN(sourceURL, "/", 2)
if len(surl) < 2 {
return nil, nil, errors.New(fmt.Sprintf("x-cos-copy-source format error: %s", sourceURL))
}
var u string
keyAndVer := strings.SplitN(surl[1], "?", 2)
if len(keyAndVer) < 2 {
u = fmt.Sprintf("%s/%s", surl[0], encodeURIComponent(surl[1], []byte{'/'}))
} else {
u = fmt.Sprintf("%v/%v?%v", surl[0], encodeURIComponent(keyAndVer[0], []byte{'/'}), encodeURIComponent(keyAndVer[1], []byte{'='}))
}

opt = cloneObjectCopyPartOptions(opt)
opt.XCosCopySource = u

u = fmt.Sprintf("/%s?partNumber=%d&uploadId=%s", encodeURIComponent(name), partNumber, uploadID)
var res CopyPartResult
var bs bytes.Buffer
sendOpt := sendOptions{
Expand Down Expand Up @@ -511,20 +525,11 @@ func (s *ObjectService) MultiCopy(ctx context.Context, name string, sourceURL st
return nil, nil, err
}
totalBytes := resp.ContentLength
surl := strings.SplitN(sourceURL, "/", 2)
if len(surl) < 2 {
return nil, nil, errors.New(fmt.Sprintf("x-cos-copy-source format error: %s", sourceURL))
}
var u string
if len(id) == 1 {
u = fmt.Sprintf("%s/%s?versionId=%s", surl[0], encodeURIComponent(surl[1]), id[0])
u = fmt.Sprintf("%s?versionId=%s", sourceURL, id[0])
} else if len(id) == 0 {
keyAndVer := strings.SplitN(surl[1], "?", 2)
if len(keyAndVer) < 2 {
u = fmt.Sprintf("%s/%s", surl[0], encodeURIComponent(surl[1]))
} else {
u = fmt.Sprintf("%v/%v?%v", surl[0], encodeURIComponent(keyAndVer[0], []byte{'/'}), encodeURIComponent(keyAndVer[1], []byte{'='}))
}
u = sourceURL
} else {
return nil, nil, errors.New("wrong params")
}
Expand All @@ -536,6 +541,7 @@ func (s *ObjectService) MultiCopy(ctx context.Context, name string, sourceURL st
if err != nil {
return nil, nil, err
}

if partNum == 0 || (totalBytes <= singleUploadMaxLength && !opt.useMulti) {
if len(id) > 0 {
return s.Copy(ctx, name, sourceURL, opt.OptCopy, id[0])
Expand Down
11 changes: 11 additions & 0 deletions object_part_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -561,6 +561,17 @@ func TestObjectService_CopyPart(t *testing.T) {
if err == nil {
t.Fatalf("Object.CopyPart returned error is nil")
}
_, _, err = client.Object.CopyPart(context.Background(),
name, uploadID, partNumber, "http://"+sourceUrl, opt)
if err == nil {
t.Fatalf("Object.CopyPart returned error is nil")
}
_, _, err = client.Object.CopyPart(context.Background(),
name, uploadID, partNumber, "test-1253846586.cos.ap-guangzhou.myqcloud.com", opt)
if err == nil {
t.Fatalf("Object.CopyPart returned error is nil")
}

}

func TestObjectService_MultiCopy(t *testing.T) {
Expand Down

0 comments on commit b537ff2

Please sign in to comment.