All Products
Search
Document Center

Object Storage Service:Download objects as files

Last Updated:Oct 17, 2023

This topic describes how to download an object from a bucket to your computer.

Examples

The following code provides an example on how to download an object named exampleobject.txt in a bucket named examplebucket to D:\localpath. The downloaded object is stored as a local file named examplefile.txt.

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')
# Download the object to your local computer. 
bucket.get_object('exampleobject.txt', :file => 'D:\\localpath\\examplefile.txt')

References

For more information about the API operation that you can call to download an object as a local file, see GetObject.