All Products
Search
Document Center

Object Storage Service:Interrupt an object upload

Last Updated:Oct 23, 2023

You can interrupt an in-progress object upload at any time. If the size of the object is small, such as several KB or tens of KB, the upload may not be interrupted.

Usage notes

  • In this topic, the public endpoint of the China (Hangzhou) region is used. If you want to access Object Storage Service (OSS) by using other Alibaba Cloud services in the same region as OSS, use an internal endpoint. For more information about the regions and endpoints supported by OSS, see Regions and endpoints.

  • In this topic, an OSSClient instance is created by using an OSS endpoint. If you want to create an OSSClient instance by using custom domain names or Security Token Service (STS), see Create an OSSClient instance.

  • You can prevent an object upload by interrupting the object upload or canceling the multipart upload. The following items describe the differences between these two methods:

    • If you interrupt an object upload, the upload API call is stopped, and the upload task is interrupted. If you cancel a multipart upload, OSS stops processing the multipart upload task that uses the upload ID.

    • If you interrupt a multipart upload task, you can still use the upload ID to upload an object. If you cancel a multipart upload task, the upload ID is unavailable.

    • If you interrupt an object upload, all upload operations are interrupted. If you cancel a multipart upload, only multipart upload operations are cancelled.

Examples

The following sample code provides an example on how to interrupt an object upload:

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

int main(void)
{
    /* Initialize information about the account that is used to access OSS. */
            
    /* Specify the endpoint of the region in which the bucket is located. For example, if the bucket is located in the China (Hangzhou) region, set the endpoint to https://oss-cn-hangzhou.aliyuncs.com. */
    std::string Endpoint = "yourEndpoint";
    /* Specify the name of the bucket. Example: examplebucket. */
    std::string BucketName = "examplebucket";
    /* Specify the full path of the object. Do not include the bucket name in the full path of the object. Example: exampledir/exampleobject.txt. */
    std::string ObjectName = "exampledir/exampleobject.txt";  
   
    /* Initialize resources, such as network resources. */
    InitializeSdk();

    ClientConfiguration conf;
    /* Obtain access credentials from environment variables. Before you run the sample code, make sure that the OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET environment variables are configured. */
    auto credentialsProvider = std::make_shared<EnvironmentVariableCredentialsProvider>();
    OssClient client(Endpoint, credentialsProvider, conf);
    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));
    }
    std::this_thread::sleep_for(std::chrono::milliseconds(1000));

    /* Interrupt the object upload. */
    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 resources, such as network resources. */
    ShutdownSdk();
    return 0;
}