#include "TosClientV2.h"
using namespace VolcengineTos;
std::string subReplace(std::string resourceStr, std::string subStr, std::string newStr) {
std::string dstStr = resourceStr;
std::string::size_type pos = 0;
dstStr.replace(pos, subStr.length(), newStr);
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 srcBucketName = "srcbucket";
std::string dir1 = "exampledir1/";
std::string dir2 = "exampledir2/";
TosClientV2 client(region, accessKey, secretKey);
ListObjectsType2Input input(bucketName);
bool isTruncated = true;
std::string nextContinuationToken = "";
input.setContinuationToken(nextContinuationToken);
auto output = client.listObjectsType2(input);
if (!output.isSuccess()) {
std::cout << "ListObjects failed." << output.error().String() << std::endl;
nextContinuationToken = output.result().getNextContinuationToken();
isTruncated = output.result().isTruncated();
for (const auto& object : output.result().getContents()) {
auto newKey = object.getKey();
newKey = subReplace(newKey,dir1,dir2);
CopyObjectV2Input input(bucketName, newKey, srcBucketName,object.getKey());
auto output = client.copyObject(input);
if (!output.isSuccess()) {
if(output.error().getStatusCode() != 404 && output.error().getCode() != "NoSuchKey"){
std::cout << "CopyObject failed." << output.error().String() << std::endl;
std::cout << "CopyObject success. the object etag:" << output.result().getETag() << std::endl;
DeleteObjectInput deleteInput(srcBucketName,object.getKey());
auto deleteOutput = client.deleteObject(deleteInput);
if (!deleteOutput.isSuccess()) {
if(deleteOutput.error().getStatusCode() != 404 && output.error().getCode() != "NoSuchKey"){
std::cout << "DeleteObject failed:" << deleteOutput.error().String() << std::endl;
std::cout << "DeleteObject success." << std::endl;
std::cout << " version id is:" << deleteOutput.result().getVersionId()
<< " is delete marker:" << deleteOutput.result().isDeleteMarker() << std::endl;