edit-icon download-icon

Quick start

Last Updated: Nov 03, 2017

This section of OSS documentation describes how to use the OSS Ruby SDK to access OSS services to view the bucket list and object list, to upload, download, and delete the objects. For usage convenience, the following operations are performed in irb which is Ruby’s interactive command-line interface (CLI).

Initialize the client

Input the following content on the CLI and press the Enter:

  1. irb

Enter the Ruby interactive CLI mode. Introduce the SDK package with require:

  1. > require 'aliyun/oss'
  2. => true

Note: In the following demonstration, the > symbol introduces the command that the user enters, and the => symbol introduces the content returned by a program.

Run the following command to create an OSSClient:

  1. > client = Aliyun::OSS::Client.new(
  2. > endpoint: 'endpoint',
  3. > access_key_id: 'AccessKeyId',
  4. > access_key_secret: 'AccessKeySecret')
  5. => #<Aliyun::OSS::Client...

Replace the parameters with the actual endpoint, AccessKeyID, and AccessKeySecret.

View the bucket list

Run the following command to view the bucket list:

  1. > buckets = client.list_buckets
  2. => #<Enumerator...
  3. > buckets.each { |b| puts b.name }
  4. => bucket-1
  5. => bucket-2
  6. => ...

If the bucket list is empty, run the following command to create a bucket:

  1. > client.create_bucket('my-bucket')
  2. => true

Note:

  • For bucket naming conventions, see Basic OSS Concepts.

  • The bucket name cannot be the same with existing bucket names of other users in OSS. To create a bucket successfully, select a unique bucket name.

View the object list

You can run the following command to view the objects in a bucket:

  1. > bucket = client.get_bucket('my-bucket')
  2. => #<Aliyun::OSS::Bucket...
  3. > objects = bucket.list_objects
  4. => #<Enumerator...
  5. > objects.each { |obj| puts obj.key }
  6. => object-1
  7. => object-2
  8. => ...

Upload an object

You can run the following command to upload a file to a bucket:

  1. > bucket.put_object('my-object', :file => 'local-file')
  2. => true

Specifically, local-file indicates the path to the local file to be uploaded. When the file is uploaded, you can view it using list_objects:

  1. > objects = bucket.list_objects
  2. => #<Enumerator...
  3. > objects.each { |obj| puts obj.key }
  4. => my-object
  5. => ...

Download an object

You can run the following command to download an object from a bucket:

  1. > bucket.get_object('my-object', :file => 'local-file')
  2. => #<Aliyun::OSS::Object...

Specifically, local-file is the path to the downloaded object. After the object is downloaded, you can open the object to view its content.

Delete an object

You can run the following command to delete an object from a bucket:

  1. > bucket.delete_object('my-object')
  2. => true

After deletion, you can run list_objects to check whether the object has been successfully deleted from the bucket.

  1. > objects = bucket.list_objects
  2. => #<Enumerator...
  3. > objects.each { |obj| puts obj.key }
  4. => object-1
  5. => ...

Learn more

Thank you! We've received your feedback.