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 records. 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 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 STS, see Create an OSSClient instance.
- The
oss:PutObject
permission is required to perform resumable upload. For more information, see Attach a custom policy to a RAM user. - During resumable upload, the upload progress is recorded in a checkpoint file. If a part fails to be uploaded, the next upload starts from the position that is recorded in the checkpoint file. After the object is uploaded, the checkpoint file is deleted.
- When you use OSS SDK for Go to perform resumable upload, the upload progress is recorded in the checkpoint file. Make sure that your program is granted write permissions on the checkpoint file.
- You cannot modify the checksum that is contained in the checkpoint file. If the checkpoint file is damaged, you must upload all parts of the object again.
- If the local file is modified during the upload, you must upload all parts of the object again.
Implementation method
You can use client.ResumableUploadObject to perform resumable upload. The following table describes the parameters you can configure for UploadObjectRequest.
Parameter | Description | Required | Default value | Configuration method |
---|---|---|---|---|
bucket | The name of the bucket. | Yes | No default value | Constructor |
key | The name of the object uploaded to OSS. | Yes | No default value | |
filePath | The name of the local file to upload. | No | No default value | |
partSize | The part size. Valid values: 100 KB to 5 GB. | No | 8 MB | setPartSize |
threadNum | The number of parts to upload simultaneously. | No | 3 | Constructor or setThreadNum |
checkpointDir | The checkpoint file that records the results of the multipart upload task. This file stores information about upload progress. If a part fails to be uploaded, the task can be resumed based on the progress recorded in the checkpoint file. After the local file is uploaded, the checkpoint file is deleted. | No | The directory in which the local file to upload is stored | Constructor or setCheckpointDir |
Examples
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 the information about the account that is used to access OSS. */
/* The AccessKey pair of an Alibaba Cloud account has permissions on all API operations. Using these credentials to perform operations in OSS is a high-risk operation. We recommend that you use a RAM user to call API operations or perform routine O&M. To create a RAM user, log on to the RAM console. */
std::string AccessKeyId = "yourAccessKeyId";
std::string AccessKeySecret = "yourAccessKeySecret";
/* 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. Example: exampledir/exampleobject.txt. */
std::string ObjectName = "exampledir/exampleobject.txt";
/* Specify the full path of the local file. Example: D:\\localpath\\examplefile.txt. By default, if you do not specify the full path of the local file, the local file is uploaded from the path of the project to which the sample program belongs. */
std::string UploadFilePath = "D:\\localpath\\examplefile.txt";
/* Specify the checkpoint file. This file stores information about upload progress. If a part fails to be uploaded, the task can be resumed based on the progress recorded in the checkpoint file. After the local file is uploaded, the checkpoint file is deleted. */
/* By default, if you do not specify the path of the checkpoint file, the checkpoint file and the local file to upload share the same path. */
std::string CheckpointFilePath = "yourCheckpointFilepath"
/* Initialize resources, such as network resources. */
InitializeSdk();
ClientConfiguration conf;
OssClient client(Endpoint, AccessKeyId, AccessKeySecret, conf);
/* Start 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 resources, such as network resources. */
ShutdownSdk();
return 0;
}
References
For more information about the complete sample code for resumable upload, visit GitHub.