Abort an upload (C++ SDK)
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
OSSClientinstance using an OSS endpoint. To create anOSSClientusing 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 upload Cancel multipart upload What it does Cancels the current upload task initiated by an API call Notifies OSS to stop accepting parts for a specific UploadID UploadID behavior UploadID remains valid; the upload can be restarted UploadID becomes invalid Applies to All upload APIs Multipart 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:
| Placeholder | Description | Example |
|---|---|---|
yourEndpoint | Endpoint of the region where your bucket is located | https://oss-cn-hangzhou.aliyuncs.com |
yourRegion | Region ID where your bucket is located | cn-hangzhou |
examplebucket | Your bucket name | examplebucket |
exampledir/exampleobject.txt | Full object path, excluding the bucket name | exampledir/exampleobject.txt |
yourLocalFilename | Path to the local file to upload | /home/user/data/sample.txt |