All Products
Search
Document Center

Object Storage Service:Resumable upload

Last Updated:Oct 18, 2023

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 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 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 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 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;
    /* 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);

    /* 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.