Quick Start (Ruby SDK)

Updated at:

This topic describes how to use Object Storage Service (OSS) SDK for Ruby to perform routine operations, such as creating a bucket, uploading objects, and downloading objects.

Create a bucket

A bucket is a global namespace in OSS. A bucket is a container that is used to store objects.

require 'aliyun/oss'
client = Aliyun::OSS::Client.new(
  # The following example uses the endpoint of the China (Hangzhou) region. Replace the endpoint with the actual one.
  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 bucket name. Example: examplebucket.
client.create_bucket('examplebucket')

Upload an object

The following code provides an example on how to upload a local file named examplefile.txt to a bucket named examplebucket. The uploaded file is stored as an object named exampleobject.txt in OSS.

require 'aliyun/oss'

client = Aliyun::OSS::Client.new(
  # Set the endpoint. This example uses the China (Hangzhou) region. Specify the endpoint for your actual region.
  endpoint: 'https://oss-cn-hangzhou.aliyuncs.com',
  # Obtain access credentials from environment variables. Before you run this 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']
)
# Specify the bucket name. For example, examplebucket.
bucket = client.get_bucket('examplebucket')
# Upload the file.
bucket.put_object('exampleobject.txt', :file => 'D:\\localpath\\examplefile.txt')

Download an object

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')

List objects

The following sample code provides an example on how to list objects stored in a bucket named examplebucket. By default, 100 objects are listed.

require 'aliyun/oss'

client = Aliyun::OSS::Client.new(
  # Set the endpoint to the one that corresponds to the region where the bucket is located. For example, if the bucket is in the China (Hangzhou) region, set the endpoint to https://oss-cn-hangzhou.aliyuncs.com.
  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 bucket name. Example: examplebucket.
bucket = client.get_bucket('examplebucket')
# List all objects.
objects = bucket.list_objects
objects.each { |o| puts o.key }     

Delete an object

The following code deletes the specified file.

require 'aliyun/oss'
client = Aliyun::OSS::Client.new(
  # Set Endpoint to the region where the bucket is located. For example, if the bucket is in the China (Hangzhou) region, set Endpoint to https://oss-cn-hangzhou.aliyuncs.com.
  endpoint: 'https://oss-cn-hangzhou.aliyuncs.com',
  # Obtain access credentials from environment variables. Before you run this 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 the full path of the object. For example, exampledir/exampleobject.txt. The full path cannot contain the bucket name.
bucket.delete_object('exampledir/exampleobject.txt')            

References

  • For more information about the API operation that you can call to create a bucket, see PutBucket.

  • For more information about the API operation that you can call to upload an object, see PutObject.

  • For more information about the API operation that you can call to download an object, see GetObject.

  • For more information about the API operation that you can call to list objects, see GetBucket (ListObjects).

  • For more information about the API operation that you can call to delete an object, see DeleteObject.