After you add a CNAME record to map a custom domain name to a specified bucket, you can use the custom domain name to access Object Storage Service (OSS) resources. This topic describes how to use a custom domain name to access a bucket.

For example, you have a website whose domain name is example.com. Users access the images on your website by using URLs in the following format: http://img.example.com/x.jpg. After you migrate the data of your website to an OSS bucket, you can map the domain name to the bucket to access the images by using the same URLs. For more information, see Map custom domain names in OSS Developer Guide.

Sample code

The following code provides an example on how to map a custom domain name to a bucket:

require 'aliyun/oss'

include Aliyun::OSS

client = Client.new(
  # Specify the desired custom domain that you want to map to the bucket. 
  endpoint: 'http://img.example.com',
  # The AccessKey pair of an Alibaba Cloud account has permissions on all API operations. Using these credentials to perform operations in OSS is a high-risk operation. We recommend that you use a RAM user to call API operations or perform routine O&M. To create a RAM user, log on to the RAM console. 
  access_key_id: 'AccessKeyId', access_key_secret: 'AccessKeySecret')
  # Map the custom domain name to the bucket. 
  cname: true)

# Specify the name of the bucket. Example: examplebucket. 
bucket = client.get_bucket('examplebucket')
Note If a custom domain name is mapped to a bucket, the domain name cannot be used to call the list_buckets operation.