All Products
Search
Document Center

Object Storage Service:Initialization (Ruby SDK)

Last Updated:Nov 29, 2025

OSSClient serves as the client of Object Storage Service (OSS) SDK for Ruby to manage OSS resources such as buckets and objects. Before you can use OSS SDK for Ruby to initiate an API request, you must create an OSSClient instance and modify the default settings of OSSClient as needed.

Create an OSSClient instance

You can use one of the following methods to create an OSSClient instance:

Create an OSSClient instance using an OSS endpoint

The following code provides an example on how to create an OSSClient instance using an OSS endpoint:

require 'aliyun/oss'

client = Aliyun::OSS::Client.new(
  # The endpoint is set to China (Hangzhou) in this example. Specify the actual endpoint for your region.
  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']
)

Create an OSSClient instance using STS

The following code provides an example on how to create an OSSClient instance using Security Token Service (STS):

require 'aliyun/oss'

client = Aliyun::OSS::Client.new(
  # The endpoint is set to China (Hangzhou) in this example. Specify the actual endpoint for your region.
  endpoint: 'https://oss-cn-hangzhou.aliyuncs.com',
  # The temporary AccessKey ID and AccessKey secret obtained from STS.
  access_key_id: 'AccessKeyId',
  access_key_secret: 'AccessKeySecret',
  # The security token obtained from STS.
  sts_token: 'SecurityToken')

Create an OSSClient instance using a custom domain name

The following code provides an example on how to create an OSSClient instance using a custom domain name.

require 'aliyun/oss'

client = Aliyun::OSS::Client.new(
  # Use a custom domain name as the endpoint.
  endpoint: 'http://example.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'],
  cname: true
)

Configure an OSSClient instance

An OSSClient instance supports the following parameters.

Parameter

Description

cname

Specifies whether CNAME can be used as an endpoint.

  • true: A CNAME can be used as an endpoint.

  • false (default value): A canonical name (CNAME) cannot be used as an Endpoint.

download_crc_enable

Specifies whether to enable CRC-64 when you download objects.

  • true: Enables cyclic redundancy check (CRC) during file downloads.

  • false (default): Disables the cyclic redundancy check when files are downloaded.

upload_crc_enable

Specifies whether to enable CRC-64 when you upload objects.

  • true (default): Enables a cyclic redundancy check for file uploads.

  • false: Disables the cyclic redundancy check for file uploads.

open_timeout

The timeout period for establishing a connection. Unit: seconds. Default value: 10.

read_timeout

The timeout period for waiting for a response. Unit: seconds. Default value: 120.