Use Object Storage Service (OSS) SDK for Ruby to create a bucket, upload an object, download an object, list objects, and delete an object.
Prerequisites
Before you begin, make sure you have:
Ruby installed on your machine
The
aliyun-oss-sdkgem installed (gem install aliyun-oss-sdk)An Alibaba Cloud account with OSS activated
An AccessKey ID and AccessKey secret stored in environment variables:
export OSS_ACCESS_KEY_ID=<your-access-key-id> export OSS_ACCESS_KEY_SECRET=<your-access-key-secret>
Run your first example
Copy the following code into a file named hello-oss.rb. It connects to OSS and creates a bucket.
require 'aliyun/oss'
client = Aliyun::OSS::Client.new(
# Replace with the endpoint for your region.
# Example: https://oss-cn-hangzhou.aliyuncs.com (China (Hangzhou))
endpoint: 'https://oss-cn-hangzhou.aliyuncs.com',
access_key_id: ENV['OSS_ACCESS_KEY_ID'],
access_key_secret: ENV['OSS_ACCESS_KEY_SECRET']
)
client.create_bucket('examplebucket')
puts 'Bucket created successfully.'Run it:
ruby hello-oss.rbExpected output:
Bucket created successfully.Create a bucket
A bucket is a container for objects in OSS. Each bucket name must be globally unique.
require 'aliyun/oss'
client = Aliyun::OSS::Client.new(
endpoint: 'https://oss-cn-hangzhou.aliyuncs.com',
access_key_id: ENV['OSS_ACCESS_KEY_ID'],
access_key_secret: ENV['OSS_ACCESS_KEY_SECRET']
)
client.create_bucket('examplebucket')Upload an object
Upload a local file to an OSS bucket. The following example uploads examplefile.txt from D:\localpath and stores it as exampleobject.txt in examplebucket.
require 'aliyun/oss'
client = Aliyun::OSS::Client.new(
endpoint: 'https://oss-cn-hangzhou.aliyuncs.com',
access_key_id: ENV['OSS_ACCESS_KEY_ID'],
access_key_secret: ENV['OSS_ACCESS_KEY_SECRET']
)
bucket = client.get_bucket('examplebucket')
bucket.put_object('exampleobject.txt', :file => 'D:\\localpath\\examplefile.txt')Download an object
Download an object from OSS to a local file. The following example downloads exampleobject.txt from examplebucket and saves it to D:\localpath\examplefile.txt.
require 'aliyun/oss'
client = Aliyun::OSS::Client.new(
endpoint: 'https://oss-cn-hangzhou.aliyuncs.com',
access_key_id: ENV['OSS_ACCESS_KEY_ID'],
access_key_secret: ENV['OSS_ACCESS_KEY_SECRET']
)
bucket = client.get_bucket('examplebucket')
bucket.get_object('exampleobject.txt', :file => 'D:\\localpath\\examplefile.txt')List objects
List all objects in a bucket. By default, up to 100 objects are returned.
require 'aliyun/oss'
client = Aliyun::OSS::Client.new(
endpoint: 'https://oss-cn-hangzhou.aliyuncs.com',
access_key_id: ENV['OSS_ACCESS_KEY_ID'],
access_key_secret: ENV['OSS_ACCESS_KEY_SECRET']
)
bucket = client.get_bucket('examplebucket')
objects = bucket.list_objects
objects.each { |o| puts o.key }Expected output:
exampleobject.txtDelete an object
Delete an object by specifying its full path within the bucket. Do not include the bucket name in the path.
require 'aliyun/oss'
client = Aliyun::OSS::Client.new(
endpoint: 'https://oss-cn-hangzhou.aliyuncs.com',
access_key_id: ENV['OSS_ACCESS_KEY_ID'],
access_key_secret: ENV['OSS_ACCESS_KEY_SECRET']
)
bucket = client.get_bucket('examplebucket')
# Full path of the object. Example: exampledir/exampleobject.txt
bucket.delete_object('exampledir/exampleobject.txt')What's next
| Operation | API reference |
|---|---|
| Create a bucket | PutBucket |
| Upload an object | PutObject |
| Download an object | GetObject |
| List objects | GetBucket (ListObjects) |
| Delete an object | DeleteObject |