对象大于 5GiB 时,需要使用 UploadPartCopyV2 来进行分片拷贝。
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) }