tencent cloud

Cloud Object Storage

Release Notes and Announcements
Release Notes
Announcements
Product Introduction
Overview
Features
Use Cases
Strengths
Concepts
Regions and Access Endpoints
Specifications and Limits
Service Regions and Service Providers
Billing
Billing Overview
Billing Method
Billable Items
Free Tier
Billing Examples
Viewing and Downloading Bill
Payment Overdue
FAQs
Getting Started
Console
Getting Started with COSBrowser
User Guide
Creating Request
Bucket
Object
Data Management
Batch Operation
Global Acceleration
Monitoring and Alarms
Operations Center
Data Processing
Content Moderation
Smart Toolbox
Data Processing Workflow
Application Integration
User Tools
Tool Overview
Installation and Configuration of Environment
COSBrowser
COSCLI (Beta)
COSCMD
COS Migration
FTP Server
Hadoop
COSDistCp
HDFS TO COS
GooseFS-Lite
Online Tools
Diagnostic Tool
Use Cases
Overview
Access Control and Permission Management
Performance Optimization
Accessing COS with AWS S3 SDK
Data Disaster Recovery and Backup
Domain Name Management Practice
Image Processing
Audio/Video Practices
Workflow
Direct Data Upload
Content Moderation
Data Security
Data Verification
Big Data Practice
COS Cost Optimization Solutions
Using COS in the Third-party Applications
Migration Guide
Migrating Local Data to COS
Migrating Data from Third-Party Cloud Storage Service to COS
Migrating Data from URL to COS
Migrating Data Within COS
Migrating Data Between HDFS and COS
Data Lake Storage
Cloud Native Datalake Storage
Metadata Accelerator
GooseFS
Data Processing
Data Processing Overview
Image Processing
Media Processing
Content Moderation
File Processing Service
File Preview
Troubleshooting
Obtaining RequestId
Slow Upload over Public Network
403 Error for COS Access
Resource Access Error
POST Object Common Exceptions
API Documentation
Introduction
Common Request Headers
Common Response Headers
Error Codes
Request Signature
Action List
Service APIs
Bucket APIs
Object APIs
Batch Operation APIs
Data Processing APIs
Job and Workflow
Content Moderation APIs
Cloud Antivirus API
SDK Documentation
SDK Overview
Preparations
Android SDK
C SDK
C++ SDK
.NET(C#) SDK
Flutter SDK
Go SDK
iOS SDK
Java SDK
JavaScript SDK
Node.js SDK
PHP SDK
Python SDK
React Native SDK
Mini Program SDK
Error Codes
Harmony SDK
Endpoint SDK Quality Optimization
Security and Compliance
Data Disaster Recovery
Data Security
Cloud Access Management
FAQs
Popular Questions
General
Billing
Domain Name Compliance Issues
Bucket Configuration
Domain Names and CDN
Object Operations
Logging and Monitoring
Permission Management
Data Processing
Data Security
Pre-signed URL Issues
SDKs
Tools
APIs
Agreements
Service Level Agreement
Privacy Policy
Data Processing And Security Agreement
Contact Us
Glossary

Copy and Move Objects

PDF
Focus Mode
Font Size
Last updated: 2026-03-12 10:28:19

Introduction

This article introduces example code and descriptions for implementing the object copy and move features in COS using the C++ SDK, including three parts: Advanced Copy API, Simple Copy API, and Moving objects.

Must-Knows

If you use the Simple API to copy objects, you need to have read permissions for the source object and write permissions for the destination object. When you configure the authorization policy, the authorization action for the destination object needs to be set to cos:PutObject, and the authorization action for the source object needs to be set to cos:GetObject. For more authorization, see CAM-supported business APIs.
If you use the Advanced API for automatic multipart copy or use the Simple API's multipart operations to implement multipart copy, you need to have read permissions for the source object and permissions for the destination object to initiate multipart upload, upload object, and complete multipart upload. When you configure the authorization policy, the authorization action for the destination object needs to be set to cos:InitiateMultipartUpload, cos:PutObject, cos:CompleteMultipartUpload, and the authorization action for the source object needs to be set to cos:GetObject. For more authorization, see CAM-supported business APIs.
If you use the move object API, you need to have read and delete permissions for the source object, and write permissions for the destination object. When you configure the authorization policy, the authorization action for the destination object needs to be set to cos:PutObject, and the authorization actions for the source object need to be set to cos:GetObject and cos:DeleteObject. For more authorization, see CAM-supported business APIs.

Related Examples

Function Name
Description
Example code
Advanced Copy
The Advanced Copy API encapsulates the Simple Copy and Multipart Copy APIs, intelligently selecting the method to copy objects based on file size.
Simple Copy
The Simple Copy API can copy an object to a specified bucket and does not support automatic multipart copy. It supports copying objects up to 5GB in size. For objects larger than 5GB, please use the Advanced API to copy.
Move Object
The Move Object API encapsulates copy and delete operations. It only supports moving objects within the current bucket. For cross-bucket movement, you can use the Advanced Copy API and Delete Object API to achieve this.

Advanced Copy (Recommended)

Note:
Automatically determines whether to use simple copy or multipart copy. If the source file is less than 5G, use simple copy; otherwise, use multipart copy.
The chunk size can be configured by users and defaults to 20MB.
The copy thread pool size allows users to configure it, defaulting to 5.

Preliminary Preparation: Create CosAPI

Before calling the COS API, you must first create an instance of CosAPI to make subsequent call requests.
qcloud_cos::CosAPI InitCosAPI() {
uint64_t appid = 12500000000;
std::string region = "ap-guangzhou";// Region of the bucket, see https://www.tencentcloud.com/document/product/436/62?from_cn_redirect=1
std::string secret_id = "************************************"; // User's SecretId. It is recommended to use sub-account keys, with authorization following the least privilege principle to mitigate usage risks. For information on how to obtain sub-account keys, see https://www.tencentcloud.com/document/product/598/37140?from_cn_redirect=1
std::string secret_key = "************************************"; // User's SecretKey. It is recommended to use sub-account keys, with authorization following the least privilege principle to mitigate usage risks. For information on how to obtain sub-account keys, see https://www.tencentcloud.com/document/product/598/37140?from_cn_redirect=1
qcloud_cos::CosConfig config(appid, secret_id, secret_key, region);
qcloud_cos::CosAPI cos_tmp(config);
return cos_tmp;
}

Preparations: Using Temporary Keys to Create CosAPI

To access COS with a temporary key, you need to create a CosAPI instance using the temporary key.
qcloud_cos::CosAPI InitCosAPI() {
// You need to have obtained the temporary key results: tmp_secret_id, tmp_secret_key,
// For generating temporary keys, see https://www.tencentcloud.com/document/product/436/14048?from_cn_redirect=1#cos-sts-sdk
uint64_t appid = 12500000000;
std::string region = "ap-guangzhou";
std::string tmp_secret_id = "************************************";
std::string tmp_secret_key = "************************************";
std::string tmp_token = "token";
qcloud_cos::CosConfig config(appid, tmp_secret_id, tmp_secret_key, region);
config.SetTmpToken(tmp_token);
qcloud_cos::CosAPI cos_tmp(config);
return cos_tmp;
}

Use Case: Copy Object

Note:
This Demo demonstrates how to use the advanced copying API to copy objects.
The thread pool size and chunk size can be set globally. This thread pool is dedicated to each download and shares the same configuration with advanced uploads.

Method Prototype

CosResult CosAPI::Copy(const CopyReq& req, CopyResp* resp)

Request Example

void CopyDemo(qcloud_cos::CosAPI& cos) {
std::string object_name = "test_dst.txt"; // Object name after copying
std::string source = bucket_name + ".cos." + region + ".myqcloud.com/test_src.txt"; ; // Source object for copying

// The part size for multipart copy is set to 20MB by default, with a maximum supported size of 5GB and a minimum of 1MB
// This configuration is global. Once explicitly set, all subsequently initialized CopyReq instances will use this configuration.
CosSysConfig::SetUploadCopyPartSize(20 * 1024 * 1024);
qcloud_cos::CopyReq req(bucket_name, object_name);// Target bucket name and object name
qcloud_cos::CopyResp resp;
req.SetXCosCopySource(source);
qcloud_cos::CosResult result = cos.Copy(req, &resp);
std::cout << "===========================Copy=============================" << std::endl;
PrintResult(result, resp);
std::cout << "============================================================" << std::endl;
}

Parameter Description

Parameter Name
Description
Type
req
Copy file request
CopyReq
resp
Copy file response
CopyResp
CopyReq Member or Function Description:
Member or Function
Description
Parameter Type
bucket_name
Target bucket name, which can be set via the constructor or set method.
The naming format for buckets is BucketName-APPID. For details, see Naming Conventions
string
object_name
Target object key (Key), which can be set via the constructor or set method.
is the unique identifier of the object in the bucket. For example, in the object access domain name examplebucket-1250000000.cos.ap-guangzhou.myqcloud.com/doc/picture.jpg, the object key is doc/picture.jpg. For details, see Object Key
string
SetXCosCopySource
Used to set the source object location for copying.
string
CopyResp Member Function Description:
Member functions
Description
Return Type
GetVersionId
Get the version number of the Object. If versioning is not enabled for the Bucket, an empty string is returned.
string
GetXCosServerSideEncryption
Obtain the server-side encryption algorithm used
string
GetEtag
Obtain the Etag of the stored object.
string
GetXCosRequestId
Obtain the request ID.
string

The CosSysConfig class is used for related configurations. In this sample, the main member functions are as follows:
Member functions
Description
Parameter Type
SetUploadCopyPartSize
Set the part size for multipart copy, unit: byte (Byte), default: 20MB.
The setting method is CosSysConfig::SetUploadCopyPartSize(20 * 1024 * 1024).
uint64_t
SetUploadThreadPoolSize
Set the download thread pool size, defaulting to 5.
The setting method is CosSysConfig::SetUploadThreadPoolSize(5).
unsigned

Returning Description

CosResult main member functions are as follows:
Member functions
Description
Return Type
IsSucc
Indicates whether the operation is successful; returns true for success, false for failure.
bool
GetHttpStatus
Obtain the http status code.
int
GetErrorCode
Obtain the error code when the request fails.
string
GetErrorMsg
Obtain the error message when the request fails.
string
GetXCosRequestId
Obtain the request ID.
string
Usage examples for CosResult are as follows. Users may choose to utilize them based on their needs:
void PrintResult(const qcloud_cos::CosResult& result, const qcloud_cos::BaseResp& resp) {
if (result.IsSucc()) {
std::cout << "Request Succ." << std::endl;
std::cout << resp.DebugString() << std::endl;
} else {
std::cout << "ErrorMsg=" << result.GetErrorMsg() << std::endl;
std::cout << "HttpStatus=" << result.GetHttpStatus() << std::endl;
std::cout << "ErrorCode=" << result.GetErrorCode() << std::endl;
std::cout << "ErrorMsg=" << result.GetErrorMsg() << std::endl;
std::cout << "ResourceAddr=" << result.GetResourceAddr() << std::endl;
std::cout << "XCosRequestId=" << result.GetXCosRequestId() << std::endl;
std::cout << "XCosTraceId=" << result.GetXCosTraceId() << std::endl;
}
}

Simple Copy

Preliminary Preparation: Create CosAPI

Before calling the COS API, you must first create an instance of CosAPI to make subsequent call requests.
qcloud_cos::CosAPI InitCosAPI() {
uint64_t appid = 12500000000;
std::string region = "ap-guangzhou";// Region of the bucket, see https://www.tencentcloud.com/document/product/436/62?from_cn_redirect=1
std::string secret_id = "************************************"; // User's SecretId. It is recommended to use sub-account keys, with authorization following the least privilege principle to mitigate usage risks. For information on how to obtain sub-account keys, see https://www.tencentcloud.com/document/product/598/37140?from_cn_redirect=1
std::string secret_key = "************************************"; // User's SecretKey. It is recommended to use sub-account keys, with authorization following the least privilege principle to mitigate usage risks. For information on how to obtain sub-account keys, see https://www.tencentcloud.com/document/product/598/37140?from_cn_redirect=1
qcloud_cos::CosConfig config(appid, secret_id, secret_key, region);
qcloud_cos::CosAPI cos_tmp(config);
return cos_tmp;
}

Preparations: Using Temporary Keys to Create CosAPI

To access COS with a temporary key, you need to create a CosAPI instance using the temporary key.
qcloud_cos::CosAPI InitCosAPI() {
// You need to have obtained the temporary key results: tmp_secret_id, tmp_secret_key,
// For generating temporary keys, see https://www.tencentcloud.com/document/product/436/14048?from_cn_redirect=1#cos-sts-sdk
uint64_t appid = 12500000000;
std::string region = "ap-guangzhou";
std::string tmp_secret_id = "************************************";
std::string tmp_secret_key = "************************************";
std::string tmp_token = "token";
qcloud_cos::CosConfig config(appid, tmp_secret_id, tmp_secret_key, region);
config.SetTmpToken(tmp_token);
qcloud_cos::CosAPI cos_tmp(config);
return cos_tmp;
}

Use Case:Simple Copy Object

Note:
This Demo demonstrates how to perform simple copy operations using the COS C++ SDK.
supports copying objects up to 5GB in size

Method Prototype

CosResult CosAPI::PutObjectCopy(const PutObjectCopyReq& req, PutObjectCopyResp* resp)

Request Example

void PutObjectCopyDemo(qcloud_cos::CosAPI& cos) {
std::string object_name = "test_dst.txt"; // Target object name for copying
std::string source = bucket_name + ".cos." + region + ".myqcloud.com/test.txt"; // Source object for copying
qcloud_cos::PutObjectCopyReq req(bucket_name, object_name);// Target bucket name and object name
req.SetXCosCopySource(source);
qcloud_cos::PutObjectCopyResp resp;
qcloud_cos::CosResult result = cos.PutObjectCopy(req, &resp);
std::cout << "===================PutObjectCopyResponse====================" << std::endl;
PrintResult(result, resp);
std::cout << "============================================================" << std::endl;
}

Parameter Description

Parameter Name
Description
Type
req
Copy file request
PutObjectCopyReq
resp
Copy file response
PutObjectCopyResp
PutObjectCopyReq Member or Function Description:
Member or Function
Description
Parameter Type
bucket_name
Target bucket name, which can be set via the constructor or set method.
The naming format for buckets is BucketName-APPID. For details, see Naming Conventions
string
object_name
Target object key (Key), which can be set via the constructor or set method.
is the unique identifier of the object in the bucket. For example, in the object access domain name examplebucket-1250000000.cos.ap-guangzhou.myqcloud.com/doc/picture.jpg, the object key is doc/picture.jpg. For details, see Object Key
string
SetXCosCopySource
Used to set the source object location for copying.
string
PutObjectCopyResp Member Function Description:
Member functions
Description
Return Type
GetVersionId
Get the version number of the Object. If versioning is not enabled for the Bucket, an empty string is returned.
string
GetXCosServerSideEncryption
Obtain the server-side encryption algorithm used
string
GetEtag
Obtain the Etag of the stored object.
string
GetXCosRequestId
Obtain the request ID.
string

Returning Description

CosResult main member functions are as follows:
Member functions
Description
Return Type
IsSucc
Indicates whether the operation is successful; returns true for success, false for failure.
bool
GetHttpStatus
Obtain the http status code.
int
GetErrorCode
The error code can be obtained when the request fails.
string
GetErrorMsg
Obtain the error message when the request fails.
string
GetXCosRequestId
Obtain the request ID.
string
Usage examples for CosResult are as follows. Users may choose to utilize them based on their needs:
void PrintResult(const qcloud_cos::CosResult& result, const qcloud_cos::BaseResp& resp) {
if (result.IsSucc()) {
std::cout << "Request Succ." << std::endl;
std::cout << resp.DebugString() << std::endl;
} else {
std::cout << "ErrorMsg=" << result.GetErrorMsg() << std::endl;
std::cout << "HttpStatus=" << result.GetHttpStatus() << std::endl;
std::cout << "ErrorCode=" << result.GetErrorCode() << std::endl;
std::cout << "ErrorMsg=" << result.GetErrorMsg() << std::endl;
std::cout << "ResourceAddr=" << result.GetResourceAddr() << std::endl;
std::cout << "XCosRequestId=" << result.GetXCosRequestId() << std::endl;
std::cout << "XCosTraceId=" << result.GetXCosTraceId() << std::endl;
}
}

Move Object

Preliminary Preparation: Create CosAPI

Before calling the COS API, you must first create an instance of CosAPI to make subsequent call requests.
qcloud_cos::CosAPI InitCosAPI() {
uint64_t appid = 12500000000;
std::string region = "ap-guangzhou";// Region of the bucket, see https://www.tencentcloud.com/document/product/436/62?from_cn_redirect=1
std::string secret_id = "************************************"; // User's SecretId. It is recommended to use sub-account keys, with authorization following the least privilege principle to mitigate usage risks. For information on how to obtain sub-account keys, see https://www.tencentcloud.com/document/product/598/37140?from_cn_redirect=1
std::string secret_key = "************************************"; // User's SecretKey. It is recommended to use sub-account keys, with authorization following the least privilege principle to mitigate usage risks. For information on how to obtain sub-account keys, see https://www.tencentcloud.com/document/product/598/37140?from_cn_redirect=1
qcloud_cos::CosConfig config(appid, secret_id, secret_key, region);
qcloud_cos::CosAPI cos_tmp(config);
return cos_tmp;
}

Preparations: Using Temporary Keys to Create CosAPI

To access COS with a temporary key, you need to create a CosAPI instance using the temporary key.
qcloud_cos::CosAPI InitCosAPI() {
// You need to have obtained the temporary key results: tmp_secret_id, tmp_secret_key,
// For generating temporary keys, see https://www.tencentcloud.com/document/product/436/14048?from_cn_redirect=1#cos-sts-sdk
uint64_t appid = 12500000000;
std::string region = "ap-guangzhou";
std::string tmp_secret_id = "************************************";
std::string tmp_secret_key = "************************************";
std::string tmp_token = "token";
qcloud_cos::CosConfig config(appid, tmp_secret_id, tmp_secret_key, region);
config.SetTmpToken(tmp_token);
qcloud_cos::CosAPI cos_tmp(config);
return cos_tmp;
}

Use Case: Moving Objects

Note:
This Demo demonstrates how to use the COS C++ SDK for object relocation.
Moving objects is only supported within the current bucket. For cross-bucket movement, you can achieve this using the Advanced Copy API and Delete Object API.

Method Prototype

CosResult CosAPI::MoveObject(const MoveObjectReq& req)

Request Example

void MoveObjectDemo(qcloud_cos::CosAPI& cos) {
std::string src_object = "test_src.txt";
std::string dst_object = "test_dst.txt";
qcloud_cos::MoveObjectReq req(bucket_name, src_object, dst_object);

qcloud_cos::CosResult result = cos.MoveObject(req);

std::cout << "========================MoveObject==========================" << std::endl;
if (result.IsSucc()) {
std::cout << "MoveObject Succ." << std::endl;
} else {
std::cout << "MoveObject Fail, ErrorMsg: " << result.GetErrorMsg() << std::endl;
}
std::cout << "============================================================" << std::endl;
}

Parameter Description

Parameter Name
Description
Type
req
Move Object Request
MoveObjectReq
MoveObjectReq Member or Function Description:
Member or Function
Description
Parameter Type
bucket_name
Target and source bucket names, which can be set via the constructor or set method.
The naming format for buckets is BucketName-APPID. For details, see Naming Conventions
string
src_object
Source object key (Key), which can be set via the constructor or set method.
is the unique identifier of the object in the bucket. For example, in the object access domain name examplebucket-1250000000.cos.ap-guangzhou.myqcloud.com/doc/picture.jpg, the object key is doc/picture.jpg. For details, see Object Key
string
dst_object
Target Object Key
string

Returning Description

CosResult main member functions are as follows:
Member functions
Description
Return Type
IsSucc
Indicates whether the operation is successful; returns true for success, false for failure.
bool
GetHttpStatus
Obtain the http status code.
int
GetErrorCode
The error code can be obtained when the request fails.
string
GetErrorMsg
Obtain the error message when the request fails.
string
GetXCosRequestId
Obtain the request ID.
string
Usage examples for CosResult are as follows. Users may choose to utilize them based on their needs:
void PrintResult(const qcloud_cos::CosResult& result, const qcloud_cos::BaseResp& resp) {
if (result.IsSucc()) {
std::cout << "Request Succ." << std::endl;
std::cout << resp.DebugString() << std::endl;
} else {
std::cout << "ErrorMsg=" << result.GetErrorMsg() << std::endl;
std::cout << "HttpStatus=" << result.GetHttpStatus() << std::endl;
std::cout << "ErrorCode=" << result.GetErrorCode() << std::endl;
std::cout << "ErrorMsg=" << result.GetErrorMsg() << std::endl;
std::cout << "ResourceAddr=" << result.GetResourceAddr() << std::endl;
std::cout << "XCosRequestId=" << result.GetXCosRequestId() << std::endl;
std::cout << "XCosTraceId=" << result.GetXCosTraceId() << std::endl;
}
}

API Operations

For the API documentation on simple operations, see the PUT Object - Copy document.


Help and Support

Was this page helpful?

Help us improve! Rate your documentation experience in 5 mins.

Feedback