Large-sized objects may fail to be downloaded because the network is unstable or the program exits abnormally and the entire object needs to be downloaded again. However, the object may still fail to be downloaded after multiple attempts. Therefore, OSS provides resumable download.

To enable resumable download, you need to split the object you want to download into multiple parts and download them separately. After you have downloaded all parts, these parts will be combined into a complete object.

The progress information of the current download is recorded in a checkpoint file. If a part fails to be download, the next download starts from the part based on the progress information recorded in the checkpoint file, achieving the resumable download. After the object is downloaded, the checkpoint file is deleted.

Note
  • OSS SDK records download progress information in a checkpoint file. Therefore, make sure that the program has the write permission to the checkpoint file. Do not modify the checkpoint file because it includes verification information. If the checkpoint file for an object is corrupted, the entire object is downloaded again.
  • If the object being downloaded is modified (the etag of the object changes) during the download, or part files are missing or modified, the entire object is download again.

You can use Bucket.DownloadFile to enable resumable download. DownloadFileRequest contains the following parameters:

Parameter Description
objectKey Specifies the name of the object to be downloaded.
filePath Specifies the path of the local file that you download the object to.
partSize Specifies the size of a part. The value can be between 1 byte to 5 GB.
options Specifies download options, including:
  • Routines: Specifies the number of parts you need to download simultaneously. The default value is 1.
  • Checkpoint: Specifies whether the resumable upload is enabled and configures the checkpoint file. The resumable upload is disabled by default. For example, oss.Checkpoint(true, "") indicates that the resumable download is enabled, and the checkpoint file is file.cp, in which file is the name of the local file. The checkpoint file is located in the directory where the local file is located. You can use oss.Checkpoint(true, "your-cp-file.cp") to specify the checkpoint file.
  • If you want to set conditions for the download, see Conditional download.

Run the following code for resumable download:

package main

import (
	"fmt"
	"os"
	"github.com/aliyun/aliyun-oss-go-sdk/oss"
)

func main() {
	// Create an OSSClient instance.
	client, err := oss.New("<yourEndpoint>", "<yourAccessKeyId>", "<yourAccessKeySecret>")
	if err ! = nil {
		fmt.Println("Error:", err)
		os.Exit(-1)
	}

	// Obtain the bucket.
	bucket, err := client.Bucket("<yourBucketName>")
	if err ! = nil {
		fmt.Println("Error:", err)
		os.Exit(-1)
	}

	// Start the resumable download. Three parts are downloaded simultaneously.
	// <yourObjectName>" is the objectKey, "LocalFile" is the filePath, and 100*1024 is the partSize.
	err = bucket.DownloadFile("<yourObjectName>", "LocalFile", 100*1024, oss.Routines(3), oss.Checkpoint(true, ""))
	if err ! = nil {
		fmt.Println("Error:", err)
		os.Exit(-1)
	}
}