When working with large objects or processing data incrementally, you can use streaming download to read object content from OSS in chunks to avoid loading the entire object into memory at once, thereby improving the efficiency and performance. This method is suitable for downloading objects that exceed memory limits, processing data in real-time to reduce memory usage, and scenarios where data is retrieved step by step over the network.
Sample code
The following code performs a streaming download of the exampleobject.txt file from examplebucket.
require 'aliyun/oss'
client = Aliyun::OSS::Client.new(
# The endpoint for the China (Hangzhou) region is used as an example. Replace it with the actual endpoint.
endpoint: 'https://oss-cn-hangzhou.aliyuncs.com',
# Obtain access credentials from environment variables. Before you run this code example, 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']
)
# Specify the bucket name, for example, examplebucket.
bucket = client.get_bucket('examplebucket')
# Specify the full path of the object. The full path cannot include the bucket name.
bucket.get_object('exampleobject.txt') do |chunk|
puts "Got a chunk, size: #{chunk.size}."
endReferences
For more information about the streaming download API operation, see GetObject.