When you download a large Object Storage Service (OSS) file (larger than 5 GB) to a local device, the download may fail. This can happen due to network interruptions or unexpected program exits. If the download continues to fail after multiple retries, you can use resumable download. Resumable download splits the large file into smaller shards and downloads them concurrently to accelerate the process. If a shard fails to download, the next attempt resumes from the breakpoint recorded in a checkpoint file. You do not need to re-download the entire file. After the download is complete, all shards are merged into a single, complete file.
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, access credentials are obtained from environment variables. For more information about how to configure access credentials, see Configure access credentials.
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 Configure OSSClient instances.
To use resumable download, you must have the
oss:GetObjectpermission. For more information, see Attach a custom policy to a RAM user.When you use resumable download, the download progress is recorded in a checkpoint file. If a shard fails to download, the next attempt resumes from the breakpoint recorded in the checkpoint file. The checkpoint file is deleted after the download is complete.
The SDK records the download status in the checkpoint file. Ensure that your program has write permissions for this file.
Do not modify the verification information in the checkpoint file. If the checkpoint file is corrupted, the entire file is downloaded again.
If the ETag of the file changes, or if a shard is lost or modified during the download, the entire file is downloaded again.
Sample code
package main
import (
"fmt"
"os"
"github.com/aliyun/aliyun-oss-go-sdk/oss"
)
func main() {
// Obtain access credentials from environment variables. Before running this sample code, make sure that the OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET environment variables are set.
provider, err := oss.NewEnvironmentVariableCredentialsProvider()
if err != nil {
fmt.Println("Error:", err)
os.Exit(-1)
}
// Create an OSSClient instance.
// Set yourEndpoint to the endpoint of the bucket. For example, for a bucket in the China (Hangzhou) region, set the endpoint to https://oss-cn-hangzhou.aliyuncs.com. For other regions, use the actual endpoint.
// Set yourRegion to the region where the bucket is located. For example, for a bucket in the China (Hangzhou) region, set the region to cn-hangzhou. For other regions, use the actual region.
clientOptions := []oss.ClientOption{oss.SetCredentialsProvider(&provider)}
clientOptions = append(clientOptions, oss.Region("yourRegion"))
// Set the signature version.
clientOptions = append(clientOptions, oss.AuthVersion(oss.AuthV4))
client, err := oss.New("yourEndpoint", "", "", clientOptions...)
if err != nil {
fmt.Println("Error:", err)
os.Exit(-1)
}
// Specify the bucket name, for 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. For example, set it to exampledir/exampleobject.txt.
// Set yourLocalFile to the full path of the local file, for example, D:\\localpath\\examplefile.txt. If you do not specify a local path, the downloaded file is saved to the local path of the project where the sample program resides.
// Set the shard size to 100 KB (100 * 1024) and the number of concurrent download threads to 3.
// oss.Checkpoint(true, "") enables resumable download. By default, the checkpoint file that records the breakpoints is in the same folder as the downloaded local file. The checkpoint file has the same name as the local file but with a .temp extension. You can also use oss.Checkpoint(true, "your-cp-file.temp") to specify a checkpoint file.
err = bucket.DownloadFile("file.zip", "D:\\file.zip", 100*1024, oss.Routines(3), oss.Checkpoint(true, ""))
if err != nil {
fmt.Println("Error:", err)
os.Exit(-1)
}
}
References
For the complete sample code for resumable download, see the GitHub example.
For more information about the API operation that you can call to perform resumable download, see GetObject.