When you upload an object to Object Storage Service (OSS) by using resumable upload, you can specify a directory for the checkpoint file that stores resumable upload progress. If an object fails to be uploaded because of a network exception or program error, the upload task is resumed from the position recorded in the checkpoint file.
Implementation
A resumable upload splits a file into multiple parts and uploads them separately. After all parts are uploaded, they are merged to form the complete file.
You can use the Bucket#resumable_upload method to perform a resumable upload. This method includes the following parameters:
|
Parameter |
Description |
Required |
Default value |
|
key |
The full path of the file to upload to OSS. |
Yes |
None |
|
file |
The full path of the local file to upload. Note
If the ETag value of the local file changes during the upload, the upload fails. |
Yes |
None |
|
:cpt_file |
The file used to record breakpoint information. You must have write permission for this file. Note
|
No |
The |
|
:disable_cpt |
Specifies whether to record the upload progress. Valid values:
|
No |
false |
|
:part_size |
The size of each part. |
No |
4 MB |
|
&block |
If a block is passed during the call, the upload progress is handled by the block. |
No |
None |
Sample code
The following code provides an example of how to perform a resumable upload.
require 'aliyun/oss'
client = Aliyun::OSS::Client.new(
# The endpoint of the China (Hangzhou) region is used as an example. Specify the actual endpoint.
endpoint: 'https://oss-cn-hangzhou.aliyuncs.com',
# Obtain access credentials from environment variables. Before you run the sample code, make sure that 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']
)
# Set 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_upload('exampledir/example.zip', '/tmp/example.zip') do |p|
puts "Progress: #{p}"
end
bucket.resumable_upload(
'exampledir/example.zip', '/tmp/example.zip',
:part_size => 100 * 1024, :cpt_file => '/tmp/example.zip.cpt') { |p|
puts "Progress: #{p}"
}