You need to enable JavaScript to run this app.
导航
分片拷贝(Go SDK)
最近更新时间:2024.02.04 18:31:01首次发布时间:2023.01.18 16:55:22

对象大于 5GiB 时,需要使用 UploadPartCopyV2 来进行分片拷贝。

注意事项

  • 拷贝文件不支持跨区域的桶间拷贝。
  • 拷贝对象时,账号必须具备源对象的读取权限和目标桶的写入权限。
  • 拷贝对象时,可以保留所有元数据(默认值)或指定新的元数据。但 ACL 并未被保留,而是设置为私有。

分片拷贝步骤

  1. 通过 CreateMultipartUploadV2 初始化分片拷贝任务。
  2. 通过 UploadPartCopyV2 进行分片拷贝。
  3. 通过 CompleteMultipartUploadV2 合并分片。

示例代码

package main

import (
   "context"
   "fmt"

   "github.com/volcengine/ve-tos-golang-sdk/v2/tos"
)

func checkErr(err error) {
   if err != nil {
      if serverErr, ok := err.(*tos.TosServerError); ok {
         fmt.Println("Error:", serverErr.Error())
         fmt.Println("Request ID:", serverErr.RequestID)
         fmt.Println("Response Status Code:", serverErr.StatusCode)
         fmt.Println("Response Header:", serverErr.Header)
         fmt.Println("Response Err Code:", serverErr.Code)
         fmt.Println("Response Err Msg:", serverErr.Message)
      } else if clientErr, ok := err.(*tos.TosClientError); ok {
         fmt.Println("Error:", clientErr.Error())
         fmt.Println("Client Cause Err:", clientErr.Cause.Error())
      } else {
         fmt.Println("Error:", err)
      }
      panic(err)
   }
}

func main() {
   var (
      accessKey = os.Getenv("TOS_ACCESS_KEY")
      secretKey = os.Getenv("TOS_SECRET_KEY")
      // Bucket 对应的 Endpoint,以华北2(北京)为例:https://tos-cn-beijing.volces.com
      endpoint = "https://tos-cn-beijing.volces.com"
      region   = "cn-beijing"
      // 填写 BucketName
      srcBucketName = "*** Provide your bucket name ***"
      dstBucketName = "*** Provide your dst bucket name ***"
      // 复制源对象 key
      srcObjectKey = "srcObjectKey"
      dstObjectKey = "objectKey"
      ctx          = context.Background()
   )
   // 初始化客户端
   client, err := tos.NewClientV2(endpoint, tos.WithRegion(region), tos.WithCredentials(tos.NewStaticCredentials(accessKey, secretKey)))
   checkErr(err)
   // 获取桶源桶中已存在对象的大小
   headOutput, err := client.HeadObjectV2(ctx, &tos.HeadObjectV2Input{
      Bucket: srcBucketName,
      Key:    srcObjectKey,
   })
   checkErr(err)
   totalSize := headOutput.ContentLength
   partSize := tos.DefaultPartSize
   createMultiOutput, err := client.CreateMultipartUploadV2(ctx, &tos.CreateMultipartUploadV2Input{
      Bucket: dstBucketName,
      Key:    dstObjectKey,
   })
   checkErr(err)
   uploadId := createMultiOutput.UploadID
   parts := make([]tos.UploadedPartV2, 0)
   partNumber := 1
   offset := int64(0)
   for offset < totalSize {
      numToUpload := partSize
      end := offset + int64(numToUpload) - 1
      output, err := client.UploadPartCopyV2(ctx, &tos.UploadPartCopyV2Input{
         Bucket:               dstBucketName,
         Key:                  dstObjectKey,
         UploadID:             uploadId,
         PartNumber:           partNumber,
         SrcBucket:            srcBucketName,
         SrcKey:               srcObjectKey,
         CopySourceRangeStart: offset,
         CopySourceRangeEnd:   end,
      })
      checkErr(err)
      fmt.Println("UploadPartCopyV2 Request ID:", output.RequestID)
      parts = append(parts, tos.UploadedPartV2{
         PartNumber: output.PartNumber,
         ETag:       output.ETag,
      })
      offset += int64(numToUpload)
      partNumber++

   }
   completeOutput, err := client.CompleteMultipartUploadV2(ctx, &tos.CompleteMultipartUploadV2Input{
      Bucket:   dstBucketName,
      Key:      dstObjectKey,
      UploadID: uploadId,
      Parts:    parts,
   })
   checkErr(err)
   fmt.Println("CompleteMultipartUploadV2 Request ID:", completeOutput.RequestID)
}

相关文档