All Products
Search
Document Center

Object Storage Service:Resumable upload (Ruby SDK)

Last Updated:Nov 29, 2025

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
  • If a part fails to upload, the next upload attempt resumes from the breakpoint recorded in the .cpt file. When you call Bucket#resumable_upload again, specify the same .cpt file. The .cpt file is deleted after the upload is complete.

  • The .cpt file records the intermediate state of the upload and includes a validation feature. Do not edit the .cpt file. If the .cpt file is corrupted, the upload cannot be resumed.

No

The file.cpt file in the same directory as the local file. file represents the name of the local file.

:disable_cpt

Specifies whether to record the upload progress. Valid values:

  • true: The upload progress is not recorded. The upload cannot be resumed if it fails.

  • false: The upload progress is recorded. If the upload fails, it resumes from the breakpoint.

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}"
}