Skip to content

Commit 3ac0afd

Browse files
authored
Merge pull request #257 from tencentyun/feature_jojoliang_dab0b9cb
multicopy source url编码
2 parents 5549f93 + b537ff2 commit 3ac0afd

File tree

4 files changed

+42
-16
lines changed

4 files changed

+42
-16
lines changed

cos.go

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

2626
const (
2727
// Version current go sdk version
28-
Version = "0.7.48"
28+
Version = "0.7.49"
2929
UserAgent = "cos-go-sdk-v5/" + Version
3030
contentTypeXML = "application/xml"
3131
defaultServiceBaseURL = "http://service.cos.myqcloud.com"

helper.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -301,6 +301,15 @@ func CloneCompleteMultipartUploadOptions(opt *CompleteMultipartUploadOptions) *C
301301
return &res
302302
}
303303

304+
func cloneObjectCopyPartOptions(opt *ObjectCopyPartOptions) *ObjectCopyPartOptions {
305+
var res ObjectCopyPartOptions
306+
if opt != nil {
307+
res = *opt
308+
res.XOptionHeader = cloneHeader(opt.XOptionHeader)
309+
}
310+
return &res
311+
}
312+
304313
type RangeOptions struct {
305314
HasStart bool
306315
HasEnd bool

object_part.go

Lines changed: 21 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -338,11 +338,25 @@ type CopyPartResult struct {
338338
//
339339
// https://www.qcloud.com/document/product/436/7750
340340
func (s *ObjectService) CopyPart(ctx context.Context, name, uploadID string, partNumber int, sourceURL string, opt *ObjectCopyPartOptions) (*CopyPartResult, *Response, error) {
341-
if opt == nil {
342-
opt = &ObjectCopyPartOptions{}
341+
if strings.HasPrefix(sourceURL, "http://") || strings.HasPrefix(sourceURL, "https://") {
342+
return nil, nil, errors.New("sourceURL format is invalid.")
343343
}
344-
opt.XCosCopySource = sourceURL
345-
u := fmt.Sprintf("/%s?partNumber=%d&uploadId=%s", encodeURIComponent(name), partNumber, uploadID)
344+
surl := strings.SplitN(sourceURL, "/", 2)
345+
if len(surl) < 2 {
346+
return nil, nil, errors.New(fmt.Sprintf("x-cos-copy-source format error: %s", sourceURL))
347+
}
348+
var u string
349+
keyAndVer := strings.SplitN(surl[1], "?", 2)
350+
if len(keyAndVer) < 2 {
351+
u = fmt.Sprintf("%s/%s", surl[0], encodeURIComponent(surl[1], []byte{'/'}))
352+
} else {
353+
u = fmt.Sprintf("%v/%v?%v", surl[0], encodeURIComponent(keyAndVer[0], []byte{'/'}), encodeURIComponent(keyAndVer[1], []byte{'='}))
354+
}
355+
356+
opt = cloneObjectCopyPartOptions(opt)
357+
opt.XCosCopySource = u
358+
359+
u = fmt.Sprintf("/%s?partNumber=%d&uploadId=%s", encodeURIComponent(name), partNumber, uploadID)
346360
var res CopyPartResult
347361
var bs bytes.Buffer
348362
sendOpt := sendOptions{
@@ -511,20 +525,11 @@ func (s *ObjectService) MultiCopy(ctx context.Context, name string, sourceURL st
511525
return nil, nil, err
512526
}
513527
totalBytes := resp.ContentLength
514-
surl := strings.SplitN(sourceURL, "/", 2)
515-
if len(surl) < 2 {
516-
return nil, nil, errors.New(fmt.Sprintf("x-cos-copy-source format error: %s", sourceURL))
517-
}
518528
var u string
519529
if len(id) == 1 {
520-
u = fmt.Sprintf("%s/%s?versionId=%s", surl[0], encodeURIComponent(surl[1]), id[0])
530+
u = fmt.Sprintf("%s?versionId=%s", sourceURL, id[0])
521531
} else if len(id) == 0 {
522-
keyAndVer := strings.SplitN(surl[1], "?", 2)
523-
if len(keyAndVer) < 2 {
524-
u = fmt.Sprintf("%s/%s", surl[0], encodeURIComponent(surl[1]))
525-
} else {
526-
u = fmt.Sprintf("%v/%v?%v", surl[0], encodeURIComponent(keyAndVer[0], []byte{'/'}), encodeURIComponent(keyAndVer[1], []byte{'='}))
527-
}
532+
u = sourceURL
528533
} else {
529534
return nil, nil, errors.New("wrong params")
530535
}
@@ -536,6 +541,7 @@ func (s *ObjectService) MultiCopy(ctx context.Context, name string, sourceURL st
536541
if err != nil {
537542
return nil, nil, err
538543
}
544+
539545
if partNum == 0 || (totalBytes <= singleUploadMaxLength && !opt.useMulti) {
540546
if len(id) > 0 {
541547
return s.Copy(ctx, name, sourceURL, opt.OptCopy, id[0])

object_part_test.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -561,6 +561,17 @@ func TestObjectService_CopyPart(t *testing.T) {
561561
if err == nil {
562562
t.Fatalf("Object.CopyPart returned error is nil")
563563
}
564+
_, _, err = client.Object.CopyPart(context.Background(),
565+
name, uploadID, partNumber, "http://"+sourceUrl, opt)
566+
if err == nil {
567+
t.Fatalf("Object.CopyPart returned error is nil")
568+
}
569+
_, _, err = client.Object.CopyPart(context.Background(),
570+
name, uploadID, partNumber, "test-1253846586.cos.ap-guangzhou.myqcloud.com", opt)
571+
if err == nil {
572+
t.Fatalf("Object.CopyPart returned error is nil")
573+
}
574+
564575
}
565576

566577
func TestObjectService_MultiCopy(t *testing.T) {

0 commit comments

Comments
 (0)