Browse Source

Merge pull request #56 from agin719/fix_object_copy

fix object upload copy versionid
tags/v0.7.8
toranger 6 years ago
committed by GitHub
parent
commit
532ae94625
No known key found for this signature in database GPG Key ID: 4AEE18F83AFDEB23
  1. 14
      costesting/ci_test.go
  2. 13
      object.go

14
costesting/ci_test.go

@ -482,15 +482,23 @@ func (s *CosTestSuite) TestCopyObject() {
expected := "test"
f := strings.NewReader(expected)
_, err = c.Object.Put(context.Background(), source, f, nil)
r, err = c.Object.Put(context.Background(), source, f, nil)
assert.Nil(s.T(), err, "PutObject Failed")
var version_id string
if r.Header["X-Cos-Version-Id"] != nil {
version_id = r.Header.Get("X-Cos-Version-Id")
}
time.Sleep(3 * time.Second)
// Copy file
soruceURL := fmt.Sprintf("%s/%s", iu.Host, source)
dest := source
dest := "test/objectMove1" + time.Now().Format(time.RFC3339)
//opt := &cos.ObjectCopyOptions{}
_, _, err = s.Client.Object.Copy(context.Background(), dest, soruceURL, nil)
if version_id == "" {
_, _, err = s.Client.Object.Copy(context.Background(), dest, soruceURL, nil)
} else {
_, _, err = s.Client.Object.Copy(context.Background(), dest, soruceURL, nil, version_id)
}
assert.Nil(s.T(), err, "PutObjectCopy Failed")
// Check content

13
object.go

@ -214,7 +214,16 @@ type ObjectCopyResult struct {
// 注意:在跨帐号复制的时候,需要先设置被复制文件的权限为公有读,或者对目标帐号赋权,同帐号则不需要。
//
// https://cloud.tencent.com/document/product/436/10881
func (s *ObjectService) Copy(ctx context.Context, name, sourceURL string, opt *ObjectCopyOptions) (*ObjectCopyResult, *Response, error) {
func (s *ObjectService) Copy(ctx context.Context, name, sourceURL string, opt *ObjectCopyOptions, id ...string) (*ObjectCopyResult, *Response, error) {
var u string
if len(id) == 1 {
u = fmt.Sprintf("%s?versionId=%s", encodeURIComponent(sourceURL), id[0])
} else if len(id) == 0 {
u = encodeURIComponent(sourceURL)
} else {
return nil, nil, errors.New("wrong params")
}
var res ObjectCopyResult
if opt == nil {
opt = new(ObjectCopyOptions)
@ -222,7 +231,7 @@ func (s *ObjectService) Copy(ctx context.Context, name, sourceURL string, opt *O
if opt.ObjectCopyHeaderOptions == nil {
opt.ObjectCopyHeaderOptions = new(ObjectCopyHeaderOptions)
}
opt.XCosCopySource = encodeURIComponent(sourceURL)
opt.XCosCopySource = u
sendOpt := sendOptions{
baseURL: s.client.BaseURL.BucketURL,

Loading…
Cancel
Save