You may fail to download a large object if the network is unstable or other exceptions occur. In some cases, you may still fail to download the object even after multiple attempts. To resolve this issue, Object Storage Service (OSS) SDK for Go provides the resumable download feature. In resumable download, the object that you want to download is split into multiple parts and each part is separately downloaded. After all parts are downloaded, these parts are combined into a complete object.

Usage notes

  • During resumable download, the download progress is recorded in a checkpoint file. If the download of a part fails, the next download starts from the position recorded in the checkpoint file. The checkpoint file is deleted after resumable download is completed.
  • The SDK records the download progress in the checkpoint file. Make sure that you have write permissions on the checkpoint file.
  • The checkpoint file contains a checksum. Do not modify the checksum. If the checkpoint file is damaged, you must download all parts again.
  • If the ETag of the object is not consistent or parts are lost or modified during the download, the object is downloaded again.

Sample code

package main

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

func main() {
    // Create an OSSClient instance. 
    // Set yourEndpoint to the endpoint of the region in which the bucket is located. For example, if the bucket is located in the China (Hangzhou) region, set yourEndpoint to https://oss-cn-hangzhou.aliyuncs.com. Specify your actual endpoint. 
    // Security risks may arise if you use the AccessKey pair of an Alibaba Cloud account to access OSS because the account has permissions on all API operations. 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. 
    client, err := oss.New("yourEndpoint", "yourAccessKeyId", "yourAccessKeySecret")
    if err != nil {
        fmt.Println("Error:", err)
        os.Exit(-1)
    }

    // Specify the name of the bucket. Example: examplebucket. 
    bucket, err := client.Bucket("examplebucket")
    if err != nil {
        fmt.Println("Error:", err)
        os.Exit(-1)
    }
    
    // Set yourObjectName to the full path of the object. The full path cannot contain the bucket name. Example: exampledir/exampleobject.txt. 
    // Set yourLocalFile to the full path of the local file. Example: D:\\localpath\\examplefile.txt. If the path for the object is not specified, the downloaded object is saved to the path of the project to which the sample program belongs. 
    // Set the size of each part to download to 100 KB (100 x 1024) and set the number of concurrent download parts to 3. 
    // oss.Checkpoint(true, "") indicates that resumable download is enabled and the checkpoint file is the file.cp file contained in the same directory as the destination local file. file is a variable that indicates the local file name. You can also use oss.Checkpoint(true, "your-cp-file.cp") to specify the checkpoint file. 
    err = bucket.DownloadFile("exampledir/exampleobject.txt", "D:\\localpath\\examplefile.txt", 100*1024, oss.Routines(3), oss.Checkpoint(true, ""))
    if err != nil {
        fmt.Println("Error:", err)
        os.Exit(-1)
    }
}        

References

  • For more information about the complete sample code that is used to perform resumable download, visit GitHub.
  • For more information about the API operation that you can call to perform resumable download, see GetObject.