You may fail to download a large object if the network is unstable or other exceptions occur. In some cases, you may fail to download the object even after multiple attempts. To resolve this issue, Object Storage Service (OSS) provides the resumable download feature. In resumable download, OSS splits an object into multiple parts and downloads each part separately. After all parts are downloaded, OSS combines the parts into a complete object.
Implementation method
You can use the Bucket#resumable_download method to perform a resumable download. This method uses the following parameters:
Parameter | Description | Required | Default value |
key | The full path of the OSS file. Note If the ETag value of the file to be downloaded changes, the download fails. | Yes | None |
file | The full path of the local file where the download is saved. | Yes | None |
:cpt_file | The file that records breakpoint information. You must have write permissions for this file. Note
| No |
|
:disable_cpt | Specifies whether to record download progress. Valid values:
| No | false |
:part_size | The size of each shard. | No | 10 MB |
&block | If a block is passed during the call, the download progress is handled by the block. | No | None |
For more information, see the API document.
Sample code
The following code provides an example of how to perform a resumable download.
require 'aliyun/oss'
client = Aliyun::OSS::Client.new(
# The endpoint of China (Hangzhou) is used as an example. Specify the actual endpoint.
endpoint: 'https://oss-cn-hangzhou.aliyuncs.com',
# Obtain access credentials from environment variables. Before running this sample code, make sure the OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET environment variables are set.
access_key_id: ENV['OSS_ACCESS_KEY_ID'],
access_key_secret: ENV['OSS_ACCESS_KEY_SECRET']
)
# Specify the bucket name, for example, examplebucket.
bucket = client.get_bucket('examplebucket')
# Set key to the full path of the object. The full path cannot contain the bucket name. For example, exampledir/example.zip.
# Set file to the full path of the local file. For example, /tmp/example.zip.
bucket.resumable_download('exampledir/example.zip', '/tmp/example.zip') do |p|
puts "Progress: #{p}"
end
bucket.resumable_download(
'exampledir/example.zip', '/tmp/example.zip',
# Set cpt_file to the path of the file that records breakpoint information.
:part_size => 100 * 1024, :cpt_file => '/tmp/example.zip.cpt') { |p|
puts "Progress: #{p}"
}References
For more information about the API operation used for resumable downloads, see GetObject.