Use a custom domain name (Ruby SDK)

更新时间:
复制 MD 格式

When you upload objects to a bucket, Object Storage Service (OSS) generates URLs based on the OSS endpoint. To serve objects through your own domain—for example, http://assets.example.com/ instead of a long OSS endpoint URL—map the custom domain name to the bucket, add a CNAME record for the custom domain name, then pass the custom domain as the endpoint and set cname: true when initializing the client.

Prerequisites

Before you begin, ensure that you have:

Initialize the client with a custom domain name

Set endpoint to your custom domain name and cname to true. OSS uses the cname flag to route requests correctly through the domain instead of the standard OSS endpoint.

require 'aliyun/oss'

include Aliyun::OSS

client = Client.new(
  # Use a custom domain name as the endpoint.
  endpoint: 'http://example.com',
  # Load access credentials from environment variables.
  # Set OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET before running this example.
  access_key_id: ENV['OSS_ACCESS_KEY_ID'],
  access_key_secret: ENV['OSS_ACCESS_KEY_SECRET'],
  cname: true
)

# Get a reference to the bucket.
bucket = client.get_bucket('examplebucket')

Limitations

Custom domain names cannot be used to call the list_buckets operation.