You can configure and manage static website hosting for your bucket. After the configurations take effect, you can use the bucket domain to access this static website and be redirected to a specified index page or error page.

Configure static website hosting

The following code provides an example on how to configure static website hosting:

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',
  # 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')

# Specify the name of the bucket. Example: examplebucket. 
bucket = client.get_bucket('examplebucket')
# Replace index.html with your default homepage for statistic website hosting. 
# Replace error.html with the default 404 page for static website hosting. 
bucket.website = BucketWebsite.new(index: 'index.html', error: 'error.html')

Query static website hosting configurations

The following code provides an example on how to query static website hosting configurations:

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',
  # 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')

# Specify the name of the bucket. Example: examplebucket. 
bucket = client.get_bucket('examplebucket')
# Query the static website hosting configurations. 
web = bucket.website
puts web.to_s

Delete static website hosting configurations

The following code provides an example on how to delete static website hosting configurations:

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',
  # 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')

# Specify the name of the bucket. Example: examplebucket. 
bucket = client.get_bucket('examplebucket')
# Delete the static website hosting configurations. 
bucket.website = BucketWebsite.new(enable: false)

References

  • For more information about the API operation that you can call to configure static website hosting, see PutBucketWebsite.
  • For more information about the API operation that you can call to query static website hosting configurations, see GetBucketWebsite.
  • For more information about the API operation that you can call to delete static website hosting configurations, see DeleteBucketWebsite.