All Products
Search
Document Center

Object Storage Service:Resumable upload

Last Updated:Dec 01, 2024

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:PutObject permission. 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 recorded in the checkpoint file. The checkpoint file will be deleted after the resumable upload task is completed.

  • The upload progress is recorded in the checkpoint file. Make sure that you have write permissions on the checkpoint file.

  • 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 method

You can use the client.ResumableUploadObject method to implement resumable upload. The following table describes the parameters that you can configure for UploadObjectRequest.

Parameter

Description

Required

Default value

Method

bucket

The name of the bucket.

Yes

None

Constructor

key

The name of the object uploaded to OSS.

Yes

None

filePath

The name of the local file to upload.

No

None

partSize

The size of each part. 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 path of the checkpoint file that records the results of the multipart upload task. This file stores information about the upload progress. If a part fails to be uploaded, the task can be resumed from the position 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

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 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 region in which the bucket is located. For example, if the bucket is located in the China (Hangzhou) region, set the region to cn-hangzhou. */
    std::string Region = "yourRegion";
    /* 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 a 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. The upload progress information is stored in this file. If a part fails to be uploaded, the upload continues from the position recorded in the file. After the local file is uploaded, the checkpoint file is deleted. */
    /* Specify the directory where the checkpoint file is stored and make sure that the specified directory exists, such as D:\\local. If you do not specify this parameter, this checkpoint file shares the same directory as the local file that you want to upload. */
    std::string CheckpointFilePath = "D:\\local";

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

    ClientConfiguration conf;
    conf.signatureVersion = SignatureVersionType::V4;
    /* 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);
    client.SetRegion(Region);

    /* 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 the complete sample code that is used to perform resumable upload, visit GitHub.