All Products
Search
Document Center

Object Storage Service:Manage static website hosting by using OSS SDK for Ruby

Last Updated:Aug 28, 2024

You can configure static website hosting for an Object Storage Service (OSS) bucket. After the configurations take effect, you can visit the website by directly using the domain name of the bucket. In this case, you are redirected to the index page or error page that is specified for the hosted website.

Configure static website hosting

The following sample 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 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 = Aliyun::OSS::BucketWebsite.new(index: 'index.html', error: 'error.html')

Query static website hosting configurations

The following sample code provides an example on how to query the configurations of 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 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 sample code provides an example on how to delete the configurations of 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 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 = Aliyun::OSS::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.