All Products
Search
Document Center

Object Storage Service:Initialization

Last Updated:Oct 19, 2023

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 based on your business requirements.

Create an OSSClient instance

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

Create an OSSClient instance by using an OSS endpoint

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

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

Create an OSSClient instance by using STS

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

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',
  # Specify the temporary AccessKey pair obtained from STS. The AccessKey pair consists of an AccessKey ID and an AccessKey secret. 
  access_key_id: 'AccessKeyId',
  access_key_secret: 'AccessKeySecret',
  # Specify the security token obtained from STS. 
  sts_token: 'SecurityToken')

Create an OSSClient instance by using a custom domain name

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

require 'aliyun/oss'

client = Aliyun::OSS::Client.new(
  # Specify the custom domain name that you want to map to the bucket. 
  endpoint: 'http://example.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']
  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

  • false (default)

download_crc_enable

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

  • true

  • false (default)

upload_crc_enable

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

  • true (default)

  • false

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.