By default, if you upload an object that has the same name as an existing object, the existing object is overwritten by the uploaded object. This topic describes how to set the x-oss-forbid-overwrite request header to disable overwrite for objects with the same name when you copy objects or use the simple upload and multipart upload methods.

Simple upload

The following code provides an example on how to disable overwrite for an object with the same name when you use simple upload:

#include <alibabacloud/oss/OssClient.h>
using namespace AlibabaCloud::OSS;

int main(void)
{
    /*Initialize the OSS account information.*/
    std::string AccessKeyId = "yourAccessKeyId";
    std::string AccessKeySecret = "yourAccessKeySecret";
    std::string Endpoint = "yourEndpoint";
    std::string BucketName = "yourBucketName";
    std::string ObjectName = "yourObjectName";

    /*Initialize network resources.*/
    InitializeSdk();

    ClientConfiguration conf;
    OssClient client(Endpoint, AccessKeyId, AccessKeySecret, conf);
    std::shared_ptr<std::iostream> content = std::make_shared<std::stringstream>();
    *content << "test cpp sdk";

    /*Specify whether to overwrite the object with the same name.*/
    /*By default, if x-oss-forbid-overwrite is not specified, the object with the same name is overwritten.*/
    /*If x-oss-forbid-overwrite is set to false, the object with the same name is overwritten.*/
    /*If x-oss-forbid-overwrite is set to true, the object with the same name is not overwritten. If an object with the same name already exists, an error is reported.*/
    ObjectMetaData meta;
    meta.addHeader("x-oss-forbid-overwrite", "true");

    PutObjectRequest request(BucketName, ObjectName, content, meta);

    /*Upload the object.*/
    auto outcome = client.PutObject(request);

    if (! outcome.isSuccess()) {
            /*Handle exceptions.*/
            std::cout << "PutObject fail" <<
        ",code:" << outcome.error().Code() <<
        ",message:" << outcome.error().Message() <<
        ",requestId:" << outcome.error().RequestId() << std::endl;
            ShutdownSdk();
            return -1;
    }

    /*Release network resources.*/
    ShutdownSdk();
    return 0;
}

For more information about simple upload, see PutObject.

Copy objects

  • Copy a small object

    The following code provides an example on how to copy a small object without overwriting the object with the same object name:

    #include <alibabacloud/oss/OssClient.h>
    using namespace AlibabaCloud::OSS;
    
    int main(void)
    {
        /*Initialize the OSS account information.*/
        std::string AccessKeyId = "yourAccessKeyId";
        std::string AccessKeySecret = "yourAccessKeySecret";
        std::string Endpoint = "yourEndpoint";
        std::string SourceBucketName = "yourSourceBucketName";
        std::string CopyBucketName = "yourCopyBucketName";
        std::string SourceObjectName = "yourSourceObjectName";
        std::string CopyObjectName = "yourCopyObjectName";
    
        /*Initialize network resources.*/
        InitializeSdk();
    
        ClientConfiguration conf;
        OssClient client(Endpoint, AccessKeyId, AccessKeySecret, conf);
    
        /*Specify whether to overwrite the object with the same name.*/
        /*By default, if x-oss-forbid-overwrite is not specified, the object with the same name is overwritten.*/
        /*If x-oss-forbid-overwrite is set to false, the object with the same name is overwritten.*/
        /*If x-oss-forbid-overwrite is set to true, the object with the same name is not overwritten. If an object with the same name already exists, an error is reported.*/
        ObjectMetaData meta;
        meta.addHeader("x-oss-forbid-overwrite", "true");
    
        CopyObjectRequest request(CopyBucketName, CopyObjectName, meta);
        request.setCopySource(SourceBucketName, SourceObjectName);
    
        /*Copy the object.*/
        auto outcome = client.CopyObject(request);
    
        if (! outcome.isSuccess()) {
                /*Handle exceptions.*/
                std::cout << "CopyObject fail" <<
            ",code:" << outcome.error().Code() <<
            ",message:" << outcome.error().Message() <<
            ",requestId:" << outcome.error().RequestId() << std::endl;
                ShutdownSdk();
                return -1;
        }
    
        /*Release network resources.*/
        ShutdownSdk();
        return 0;
    }
  • Copy a large object

    The following code provides an example on how to disable overwrite for the object with the same name when you copy a large object by using multipart copy:

    #include <alibabacloud/oss/OssClient.h>
    #include <sstream>
    using namespace AlibabaCloud::OSS;
    
    int main(void)
    {
        /*Initialize the OSS account information.*/
        std::string AccessKeyId = "yourAccessKeyId";
        std::string AccessKeySecret = "yourAccessKeySecret";
        std::string Endpoint = "yourEndpoint";
        std::string SourceBucketName = "yourSourceBucketName";
        std::string CopyBucketName = "yourCopyBucketName";
        std::string SourceObjectName = "yourSourceObjectName";
        std::string CopyObjectName = "yourCopyObjectName";
    
        /*Initialize network resources.*/
        InitializeSdk();
    
        ClientConfiguration conf;
        OssClient client(Endpoint, AccessKeyId, AccessKeySecret, conf);
    
        auto getObjectMetaReq = GetObjectMetaRequest(SourceBucketName, SourceObjectName);
        auto getObjectMetaResult = GetObjectMeta(getObjectMetaReq);
        if (! getObjectMetaResult.isSuccess()) {
            std::cout << "GetObjectMeta fail" <<
            ",code:" << outcome.error().Code() <<
            ",message:" << outcome.error().Message() <<
            ",requestId:" << outcome.error().RequestId() << std::endl;
            ShutdownSdk();
            return -1;
        }
            /*Query the size of the object to copy.*/
        auto objectSize = getObjectMetaResult.result().ContentLength();
    
        /*Copy the large object.*/
        ObjectMetaData metaData;
        std::stringstream ssDesc;
        ssDesc << "/" << SourceBucketName << "/" << SourceObjectName;
        std::string value = ssDesc.str();
        metaData.addHeader("x-oss-copy-source", value);
    
        /*Specify whether to overwrite the object with the same name.*/
        /*By default, if x-oss-forbid-overwrite is not specified, the object with the same name is overwritten.*/
        /*If x-oss-forbid-overwrite is set to false, the object with the same name is overwritten.*/
        /*If x-oss-forbid-overwrite is set to true, the object with the same name is not overwritten. If an object with the same name already exists, an error is reported.*/
        metaData.addHeader("x-oss-forbid-overwrite", "true");
    
        InitiateMultipartUploadRequest initUploadRequest(BucketName, ObjectName, metaData);
    
        /*Initiate a multipart copy task.*/
        auto uploadIdResult = client.InitiateMultipartUpload(initUploadRequest);
        auto uploadId = uploadIdResult.result().UploadId();
        int64_t partSize = 100 * 1024;
        PartList partETagList;
        int partCount = static_cast<int>(objectSize / partSize);
        /*Calculate the number of parts.*/
        if (objectSize % partSize ! = 0) {
            partCount++;
        }
    
        /*Copy each part sequentially.*/
        for (int i = 1; i <= partCount; i++) {
            auto skipBytes = partSize * (i - 1);
            auto size = (partSize < objectSize - skipBytes) ? partSize : (objectSize - skipBytes);
            auto uploadPartCopyReq = UploadPartCopyRequest(CopyBucketName, CopyObjectName, SourceBucketName, SourceObjectName,uploadId, i + 1);
            uploadPartCopyReq.setCopySourceRange(skipBytes, skipBytes + size -1);
            auto uploadPartOutcome = client.UploadPartCopy(uploadPartCopyReq);
            if (uploadPartOutcome.isSuccess()) {
                Part part(i, uploadPartOutcome.result().ETag());
                partETagList.push_back(part);
            }
            else {
                std::cout << "UploadPartCopy fail" <<
                ",code:" << outcome.error().Code() <<
                ",message:" << outcome.error().Message() <<
                ",requestId:" << outcome.error().RequestId() << std::endl;
            }
    
        }
    
        /*Complete the multipart copy task.*/
        CompleteMultipartUploadRequest request(CopyBucketName, CopyObjectName, partETagList, uploadId);
    
        /*Specify whether to overwrite the object with the same name.*/
        /*By default, if x-oss-forbid-overwrite is not specified, the object with the same name is overwritten.*/
        /*If x-oss-forbid-overwrite is set to false, the object with the same name is overwritten.*/
        /*If x-oss-forbid-overwrite is set to true, the object with the same name is not overwritten. If an object with the same name already exists, an error is reported.*/ 
        request.MetaData().addHeader("x-oss-forbid-overwrite", "true");
    
        auto outcome = client.CompleteMultipartUpload(request);
    
        if (! outcome.isSuccess()) {
            /*Handle exceptions.*/
            std::cout << "CompleteMultipartUpload fail" <<
            ",code:" << outcome.error().Code() <<
            ",message:" << outcome.error().Message() <<
            ",requestId:" << outcome.error().RequestId() << std::endl;
            ShutdownSdk();
            return -1;
        }
    
        /*Release network resources.*/
        ShutdownSdk();
        return 0;
    }

For more information about how to copy an object, see CopyObject.

Multipart upload

The following code provides an example on how to disable overwrite for the object with the same name when you use multipart upload:

#include <alibabacloud/oss/OssClient.h>

int64_t getFileSize(const std::string& file)
{
        std::fstream f(file, std::ios::in | std::ios::binary);
    f.seekg(0, f.end);
    int64_t size = f.tellg();
    f.close();
    return size;
}

using namespace AlibabaCloud::OSS;

int main(void)
{
    /*Initialize the OSS account information.*/
    std::string AccessKeyId = "yourAccessKeyId";
    std::string AccessKeySecret = "yourAccessKeySecret";
    std::string Endpoint = "yourEndpoint";
    std::string BucketName = "yourBucketName";
    std::string ObjectName = "yourObjectName";

    /*Initialize network resources.*/
    InitializeSdk();

    ClientConfiguration conf;
    OssClient client(Endpoint, AccessKeyId, AccessKeySecret, conf);

    /*Specify whether to overwrite the object with the same name.*/
    /*By default, if x-oss-forbid-overwrite is not specified, the object with the same name is overwritten.*/
    /*If x-oss-forbid-overwrite is set to false, the object with the same name is overwritten.*/
    /*If x-oss-forbid-overwrite is set to true, the object with the same name is not overwritten. If an object with the same name already exists, an error is reported.*/
    ObjectMetaData meta;
    data.addHeader("x-oss-forbid-overwrite", "true");

    InitiateMultipartUploadRequest initUploadRequest(BucketName, ObjectName, data);

    /*Initiate the multipart upload task.*/
    auto uploadIdResult = client.InitiateMultipartUpload(initUploadRequest);
    auto uploadId = uploadIdResult.result().UploadId();
    std::string fileToUpload = "yourLocalFilename";
    int64_t partSize = 100 * 1024;
    PartList partETagList;
    auto fileSize = getFileSize(fileToUpload);
    int partCount = static_cast<int>(fileSize / partSize);
    /*Calculate the number of parts.*/
    if (fileSize % partSize ! = 0) {
        partCount++;
    }

    /*Upload each part sequentially.*/
    for (int i = 1; i <= partCount; i++) {
        auto skipBytes = partSize * (i - 1);
        auto size = (partSize < fileSize - skipBytes) ? partSize : (fileSize - skipBytes);
        std::shared_ptr<std::iostream> content = std::make_shared<std::fstream>(fileToUpload, std::ios::in|std::ios::binary);
        content->seekg(skipBytes, std::ios::beg);

        UploadPartRequest uploadPartRequest(BucketName, ObjectName, content);
        uploadPartRequest.setContentLength(size);
        uploadPartRequest.setUploadId(uploadId);
        uploadPartRequest.setPartNumber(i);
        auto uploadPartOutcome = client.UploadPart(uploadPartRequest);
        if (uploadPartOutcome.isSuccess()) {
                Part part(i, uploadPartOutcome.result().ETag());
                partETagList.push_back(part);
        }
        else {
                std::cout << "uploadPart fail" <<
            ",code:" << uploadPartOutcome.error().Code() <<
            ",message:" << uploadPartOutcome.error().Message() <<
            ",requestId:" << uploadPartOutcome.error().RequestId() << std::endl;
        }

    }

    /*Complete the multipart upload task.*/
    CompleteMultipartUploadRequest request(BucketName, ObjectName);
    request.setUploadId(uploadId);
    request.setPartList(partETagList);

    /*Specify whether to overwrite the object with the same name.*/
    /*By default, if x-oss-forbid-overwrite is not specified, the object with the same name is overwritten.*/
    /*If x-oss-forbid-overwrite is set to false, the object with the same name is overwritten.*/
    /*If x-oss-forbid-overwrite is set to true, the object with the same name is not overwritten. If an object with the same name already exists, an error is reported.*/ 
    request.MetaData().addHeader("x-oss-forbid-overwrite", "true");
    auto outcome = client.CompleteMultipartUpload(request);

    if (! outcome.isSuccess()) {
            /*Handle exceptions.*/
            std::cout << "CompleteMultipartUpload fail" <<
        ",code:" << outcome.error().Code() <<
        ",message:" << outcome.error().Message() <<
        ",requestId:" << outcome.error().RequestId() << std::endl;
            ShutdownSdk();
            return -1;
    }

      /*Release network resources.*/
    ShutdownSdk();
    return 0;
}

For more information about multipart upload, see InitiateMultipartUpload and CompleteMultipartUpload.