edit-icon download-icon

Static website hosting

Last Updated: Nov 07, 2017

In CNAME, we mentioned that the OSS allows you to direct your domain name to the OSS service address. In this way, the OSS bucket is accessed when you access your website. You must specify the names of the objects in the bucket corresponding to the homepage (index) and error page (error) of the website respectively.

For more information about static website hosting, see Static website hosting.

Set website hosting

You can use Client.SetBucketWebsite to set website hosting:

  1. import "github.com/aliyun/aliyun-oss-go-sdk/oss"
  2. client, err := oss.New("Endpoint", "AccessKeyId", "AccessKeySecret")
  3. if err != nil {
  4. // HandleError(err)
  5. }
  6. // bucketName:"my-bucket", indexWebsite:"index.html", errorWebsite:"error.html"
  7. err = client.SetBucketWebsite("my-bucket", "index.html", "error.html")
  8. if err != nil {
  9. // HandleError(err)
  10. }

View website hosting

You can use Client.GetBucketWebsite to view website hosting:

  1. import "fmt"
  2. import "github.com/aliyun/aliyun-oss-go-sdk/oss"
  3. client, err := oss.New("Endpoint", "AccessKeyId", "AccessKeySecret")
  4. if err != nil {
  5. // HandleError(err)
  6. }
  7. wsRes, err := client.GetBucketWebsite("my-bucket")
  8. if err != nil {
  9. // HandleError(err)
  10. }
  11. fmt.Println("indexWebsite:", wsRes.IndexDocument.Suffix,
  12. "errorWebsite:", wsRes.ErrorDocument.Key)

Clear website hosting

You can use Client.DeleteBucketWebsite to clear website hosting:

  1. import "github.com/aliyun/aliyun-oss-go-sdk/oss"
  2. client, err := oss.New("Endpoint", "AccessKeyId", "AccessKeySecret")
  3. if err != nil {
  4. // HandleError(err)
  5. }
  6. err = client.DeleteBucketWebsite("my-bucket")
  7. if err != nil {
  8. // HandleError(err)
  9. }
Thank you! We've received your feedback.