All Products
Search
Document Center

Object Storage Service:Streaming download

Last Updated:Sep 11, 2023

If you want to download a large object or it takes a long time to download an object at a time, you can use streaming download to download the object incrementally until the entire object is downloaded.

Sample code

The following code provides an example on how to perform streaming download to download an object named exampleobject.txt from a bucket named examplebucket:

require 'aliyun/oss'

client = Aliyun::OSS::Client.new(
  # In this example, the endpoint of the China (Hangzhou) region is used. Specify your 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 configured. 
  access_key_id: ENV['OSS_ACCESS_KEY_ID'],
  access_key_secret: ENV['OSS_ACCESS_KEY_SECRET']
)

# Specify the name of the bucket. Example: examplebucket. 
bucket = client.get_bucket('examplebucket')
# Specify the full path of the object. Do not include the bucket name in the full path. 
bucket.get_object('exampleobject.txt') do |chunk|  
  puts "Got a chunk, size: #{chunk.size}."
end

References

For more information about the API operation that you can call to perform streaming download, see GetObject.