Abort an upload (C++ SDK)

更新时间: 2026-03-20 15:55:40

Abort an in-progress file upload at any time using DisableRequest(). Note that if the file is small (a few KB), the upload may complete before the abort takes effect.

Usage notes

  • The sample code uses the public endpoint for the China (Hangzhou) region. To access OSS from other Alibaba Cloud services in the same region, use an internal endpoint instead. For more information, see Regions and endpoints.

  • The sample creates an OSSClient instance using an OSS endpoint. To create an OSSClient using a custom domain name or Security Token Service (STS), see Create an OSSClient instance.

  • There are two ways to stop an upload: aborting the upload and canceling the multipart upload. The differences are:

    Abort uploadCancel multipart upload
    What it doesCancels the current upload task initiated by an API callNotifies OSS to stop accepting parts for a specific UploadID
    UploadID behaviorUploadID remains valid; the upload can be restartedUploadID becomes invalid
    Applies toAll upload APIsMultipart uploads only

Sample code

The following sample code submits four concurrent PutObject requests, waits one second, then aborts all of them using DisableRequest().

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

int main(void)
{
    /* Set the endpoint of the region where the bucket is located.
       Example: https://oss-cn-hangzhou.aliyuncs.com for China (Hangzhou). */
    std::string Endpoint = "yourEndpoint";
    /* Set the region ID. Example: cn-hangzhou. */
    std::string Region = "yourRegion";
    /* Set the bucket name. Example: examplebucket. */
    std::string BucketName = "examplebucket";
    /* Set the full object path, excluding the bucket name.
       Example: exampledir/exampleobject.txt. */
    std::string ObjectName = "exampledir/exampleobject.txt";

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

    ClientConfiguration conf;
    conf.signatureVersion = SignatureVersionType::V4;
    /* Read credentials from environment variables OSS_ACCESS_KEY_ID and
       OSS_ACCESS_KEY_SECRET to avoid hardcoding sensitive information. */
    auto credentialsProvider = std::make_shared<EnvironmentVariableCredentialsProvider>();
    OssClient client(Endpoint, credentialsProvider, conf);
    client.SetRegion(Region);

    /* Submit four concurrent PutObject requests asynchronously. */
    std::vector<PutObjectOutcomeCallable> Callables;
    for (int i = 0; i < 4; i++) {
        std::shared_ptr<std::iostream> content =
            std::make_shared<std::fstream>("yourLocalFilename", std::ios::in | std::ios::binary);
        PutObjectRequest request(BucketName, ObjectName, content);
        auto outcomeCallable = client.PutObjectCallable(request);
        Callables.emplace_back(std::move(outcomeCallable));
    }

    /* Wait briefly before aborting to give the uploads time to start. */
    std::this_thread::sleep_for(std::chrono::milliseconds(1000));

    /* Abort all in-progress upload requests. */
    client.DisableRequest();

    for (size_t i = 0; i < Callables.size(); i++) {
        auto outcome = Callables[i].get();
        if (outcome.error().Code() == "ClientError:100002" ||
            outcome.error().Code() == "ClientError:200042") {
            std::cout << "disable putobject success" << std::endl;
        }
    }

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

Replace the following placeholders with actual values:

PlaceholderDescriptionExample
yourEndpointEndpoint of the region where your bucket is locatedhttps://oss-cn-hangzhou.aliyuncs.com
yourRegionRegion ID where your bucket is locatedcn-hangzhou
examplebucketYour bucket nameexamplebucket
exampledir/exampleobject.txtFull object path, excluding the bucket nameexampledir/exampleobject.txt
yourLocalFilenamePath to the local file to upload/home/user/data/sample.txt
上一篇: Multipart upload (C++ SDK) 下一篇: Upload progress bar (C++ SDK)
阿里云首页 对象存储 相关技术圈