When you upload an object to Object Storage Service (OSS) by using resumable upload, you can specify a directory for the checkpoint file that stores resumable upload progress. If an object fails to be uploaded because of a network exception or program error, the upload task is resumed from the position recorded in the checkpoint file.
Usage notes
In this topic, the public endpoint of the China (Hangzhou) region is used. If you want to access OSS from other Alibaba Cloud services in the same region as OSS, use an internal endpoint. For more information about OSS regions and endpoints, see Regions and endpoints.
In this topic, an OSSClient instance is created by using an OSS endpoint. If you want to create an OSSClient by using custom domain names or Security Token Service (STS), see Create an OSSClient instance.
To use resumable upload, you must have the
oss:PutObjectpermission. For more information, see Attach a custom policy to a RAM user.When you use resumable upload, the upload progress is recorded in a checkpoint file. If a part fails to upload, the upload resumes from the point recorded in the checkpoint file. After the upload is complete, the checkpoint file is deleted.
The checkpoint file contains a checksum. This checksum cannot be modified. If the checkpoint file is damaged, you must re-upload all parts of the object.
If the local file is modified during the upload, you must re-upload all parts of the object.
Implementation
You can use the client.ResumableUploadObject method to perform a resumable upload. The UploadObjectRequest in this method contains the following parameters:
Parameter | Description | Required | Default value | How to set |
bucket | The bucket name. | Yes | None | Set in the constructor. |
key | The name of the file to upload to OSS. | Yes | None | |
filePath | The name of the local file to upload. | No | None | |
partSize | The size of each part. The value must be in the range of 100 KB to 5 GB. | No | 8 MB | Use setPartSize. |
threadNum | The number of concurrent threads for multipart upload. | No | 3 | Set in the constructor or use setThreadNum. |
checkpointDir | The file that records upload progress. If an upload is interrupted, it resumes from the point recorded in this file. The file is deleted after the upload is complete. | No | The same folder as DownloadFile. | Set in the constructor or use setCheckpointDir. |
Sample code
The following sample code provides an example on how to perform resumable upload:
#include <alibabacloud/oss/OssClient.h>
using namespace AlibabaCloud::OSS;
int main(void)
{
/* Initialize OSS account information. */
/* Set yourEndpoint to the endpoint of the region where the bucket is located. For example, if the bucket is in the China (Hangzhou) region, set the endpoint to https://oss-cn-hangzhou.aliyuncs.com. */
std::string Endpoint = "yourEndpoint";
/* Set yourRegion to the region where the bucket is located. For example, if the bucket is in the China (Hangzhou) region, set the region to cn-hangzhou. */
std::string Region = "yourRegion";
/* Specify the bucket name, for example, examplebucket. */
std::string BucketName = "examplebucket";
/* Specify the full path of the object. The full path cannot contain the bucket name. For example, exampledir/exampleobject.txt. */
std::string ObjectName = "exampledir/exampleobject.txt";
/* Specify the full path of the local file, for example, D:\\localpath\\examplefile.txt. If you do not specify a local path, the file is uploaded from the local path that corresponds to the project where the sample program is located. */
std::string UploadFilePath = "D:\\localpath\\examplefile.txt";
/* The file that records the results of local part uploads. The upload progress is saved in this file. If a part fails to upload, the next upload attempt resumes from the breakpoint recorded in the file. After the upload is complete, this file is deleted. */
/* Set the directory where the checkpoint file is stored and make sure the specified directory exists, for example, D:\\local. If this value is not set, checkpoint information is not recorded and resumable upload is not used. */
std::string CheckpointFilePath = "D:\\local";
/* Initialize network and other resources. */
InitializeSdk();
ClientConfiguration conf;
conf.signatureVersion = SignatureVersionType::V4;
/* Obtain access credentials from environment variables. Before running this sample code, make sure the OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET environment variables are set. */
auto credentialsProvider = std::make_shared<EnvironmentVariableCredentialsProvider>();
OssClient client(Endpoint, credentialsProvider, conf);
client.SetRegion(Region);
/* Perform a resumable upload. */
UploadObjectRequest request(BucketName, ObjectName, UploadFilePath, CheckpointFilePath);
auto outcome = client.ResumableUploadObject(request);
if (!outcome.isSuccess()) {
/* Handle exceptions. */
std::cout << "ResumableUploadObject fail" <<
",code:" << outcome.error().Code() <<
",message:" << outcome.error().Message() <<
",requestId:" << outcome.error().RequestId() << std::endl;
return -1;
}
/* Release network and other resources. */
ShutdownSdk();
return 0;
}References
For the complete sample code for resumable upload, see GitHub sample.