Help Center> Object Storage Service> SDK Reference> Go> Objects-Related APIs> Downloading Objects - Resumable Transfer

Downloading Objects - Resumable Transfer

API Description

Downloading large files often fails due to poor network conditions or program breakdowns. Downloading large files often fails due to poor network conditions or program breakdowns. To resolve such issues, you can use the API for resumable download, whose working principle is to divide the to-be-downloaded file into multiple parts and download them separately. The download result of each part is recorded in a checkpoint file in real time. Only when all parts are successfully downloaded, the result indicating a successful download is returned. Otherwise, an error message is returned to remind you of calling the API again for re-downloading. Based on the download status of each part recorded in the checkpoint file, the re-downloading will download the parts failed to be downloaded previously, instead of downloading all parts. By virtue of this, resources are saved and efficiency is improved.

You can call ObsClient.DownloadFile to perform a resumable download. The following table describes the parameters involved in this API.

Parameter

Description

Bucket

(Mandatory) Bucket name

Key

(Mandatory) Object name

DownloadFile

Full path of the local directory to which the object is downloaded If the value is empty, the downloaded object is saved in the directory where the program is executed.

PartSize

Part size, in bytes.

TaskNum

Maximum number of files that can be downloaded concurrently in multipart mode.

EnableCheckpoint

Whether to enable the resumable upload mode. The default value is False, indicating that this mode is disabled.

CheckpointFile

File used to record the download progress. This parameter is effective only in the resumable download mode. If the value is empty, the file is in the same local directory as the downloaded object.

VersionId

Version ID of the object to be downloaded.

Method Definition

func (obsClient ObsClient) DownloadFile(input *DownloadFileInput) (output *GetObjectMetadataOutput, err error)

Request Parameters

Field

Type

Optional or Mandatory

input

*DownloadFileInput

Mandatory

Returned Results

Field

Type

output

*GetObjectMetadataOutput

err

error

Sample Code

func main() {
       input := &obs.DownloadFileInput{}
       input.Bucket = "bucketname"
       input.Key = "objectname"
       input.DownloadFile = "localfile"   // localfile is the full path to which objects are downloaded.
       input.EnableCheckpoint = true    // Enable the resumable download mode.
       input.PartSize = 9 * 1024 * 1024  // Set the part size to 9 MB.
       input.TaskNum = 5  // Specify the maximum number of parts that can be concurrently downloaded.
       output, err := obsClient.DownloadFile(input)
       if err == nil {
              fmt.Printf("RequestId:%s\n", output.RequestId)
       } else if obsError, ok := err.(obs.ObsError); ok {
              fmt.Printf("Code:%s\n", obsError.Code)
              fmt.Printf("Message:%s\n", obsError.Message)
       }
}
  • The API for resumable download, which is implemented based on partial download, is an encapsulated and enhanced version of partial download.
  • This API saves resources and improves efficiency upon the re-download, and speeds up the download process by concurrently downloading parts. Because this API is invisible to users, users are unaware of internal service details, such as the creation and deletion of checkpoint files, division of objects, and concurrent download of parts.
  • The default value of the EnableCheckpoint parameter is False, which indicates that the resumable download mode is disabled. In such cases, the API for resumable download degrades to the simple encapsulation of partial download, and no checkpoint file will be generated.
  • CheckpointFile is effective only when EnableCheckpoint is True.