You need to enable JavaScript to run this app.
Torch Object Storage

Torch Object Storage

Copy page
Download PDF
Copy Object
Standard Copy (C++ SDK)
Copy page
Download PDF
Standard Copy (C++ SDK)
The copyObject operation enables copying objects between buckets within the same region, as well as copying objects within a single bucket. During copying, you can specify new metadata or retain the metadata of the source object.​
Precautions​
  • File copying does not support cross-region bucket-to-bucket copying.​
  • When copying an object, the account must have read permission for the source object and write permission for the destination bucket.​
  • When copying an object, you can retain all metadata (by default) or specify new metadata. However, the ACL was not retained; instead, it was set to private.​
Sample Code​
Standard Copy​
When the object is smaller than 5 GiB, you can use standard copy. The following code copies the srcdir/exampleobject.txt object from the srcbucket bucket to the destbucket bucket and sets the object name to destdir/exampleobject.txt.​
​
#include "TosClientV2.h"​
using namespace VolcengineTos;​
​
int main(void){​
​
​
std::string region = "Your Region";​
std::string accessKey = std::getenv("TOS_ACCESS_KEY");​
std::string secretKey = std::getenv("TOS_SECRET_KEY");​
​
std::string bucketName = "destbucket";​
​
std::string objectName = "destdir/exampleobject.txt";​
​
std::string srcBucketName = "srcbucket";​
​
std::string srcObjectName = "srcdir/exampleobject.txt";​
​
​
InitializeClient();​
​
TosClientV2 client(region, accessKey, secretKey);​
​
CopyObjectV2Input input(bucketName, objectName, srcBucketName,srcObjectName);​
​
input.setAcl(ACLType::PublicRead);​
input.setStorageClass(StorageClassType::STANDARD);​
input.setMetadataDirective(MetadataDirectiveType::REPLACE);​
auto output = client.copyObject(input);​
if (!output.isSuccess()) {​
​
std::cout << "CopyObject failed." << output.error().String() << std::endl;​
​
CloseClient();​
return -1;​
}​
​
std::cout << "CopyObject success. the object etag:" << output.result().getETag() << std::endl;​
​
​
CloseClient();​
return 0;​
}​
​
Inherit source object metadata during copy​
​
#include "TosClientV2.h"​
using namespace VolcengineTos;​
​
int main(void){​
​
std::string region = "Your Region";​
std::string accessKey = std::getenv("TOS_ACCESS_KEY");​
std::string secretKey = std::getenv("TOS_SECRET_KEY");​
​
std::string bucketName = "destbucket";​
​
std::string objectName = "destdir/exampleobject.txt";​
​
std::string srcBucketName = "srcbucket";​
​
std::string srcObjectName = "srcdir/exampleobject.txt";​
​
​
InitializeClient();​
​
TosClientV2 client(region, accessKey, secretKey);​
​
CopyObjectV2Input input(bucketName, objectName, srcBucketName,srcObjectName);​
auto output = client.copyObject(input);​
if (!output.isSuccess()) {​
​
std::cout << "CopyObject failed." << output.error().String() << std::endl;​
​
CloseClient();​
return -1;​
}​
​
std::cout << "CopyObject success. the object etag:" << output.result().getETag() << std::endl;​
​
​
CloseClient();​
return 0;​
}​
​
Overwrite object metadata during copy​
​
#include "TosClientV2.h"​
using namespace VolcengineTos;​
​
int main(void){​
​
​
std::string region = "Your Region";​
std::string accessKey = std::getenv("TOS_ACCESS_KEY");​
std::string secretKey = std::getenv("TOS_SECRET_KEY");​
​
std::string bucketName = "destbucket";​
​
std::string objectName = "destdir/exampleobject.txt";​
​
std::string srcBucketName = "srcbucket";​
​
std::string srcObjectName = "srcdir/exampleobject.txt";​
​
​
InitializeClient();​
​
TosClientV2 client(region, accessKey, secretKey);​
​
CopyObjectV2Input input(bucketName, objectName, srcBucketName,srcObjectName);​
​
input.setMetadataDirective(MetadataDirectiveType::REPLACE);​
input.setMeta({{"self-test", "yes"}});​
auto output = client.copyObject(input);​
if (!output.isSuccess()) {​
​
std::cout << "CopyObject failed." << output.error().String() << std::endl;​
​
CloseClient();​
return -1;​
}​
​
std::cout << "CopyObject success. the object etag:" << output.result().getETag() << std::endl;​
​
​
CloseClient();​
return 0;​
}​
​
Relevant Documentation​
For the API documentation on standard copy operations, refer to CopyObject.​
Last updated: 2025.09.25 14:22:47