You can set your bucket configuration to the static website hosting mode. After the configuration takes effect, you can access this static website with the bucket domain and be redirected to a specified index page or error page.

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

Configure static website hosting

Run the following code to configure static website hosting:

package main

import (
	"fmt"
	"os"
	"github.com/aliyun/aliyun-oss-go-sdk/oss"
)

func main() {
	// Create an OSSClient instance.
	client, err := oss.New("<yourEndpoint>", "<yourAccessKeyId>", "<yourAccessKeySecret>")
	if err ! = nil {
		fmt.Println("Error:", err)
		os.Exit(-1)
	}

	bucketName := "<yourBucketName>"

	// Configure static web hosting: The index page is index.html and the error page is error.html.
	err = client.SetBucketWebsite(bucketName, "index.html", "error.html")
	if err ! = nil {
		fmt.Println("Error:", err)
		os.Exit(-1)
	}
}

View static website hosting configurations

Run the following code to view static website hosting configurations:

package main

import (
	"fmt"
	"os"
	"github.com/aliyun/aliyun-oss-go-sdk/oss"
)

func main() {
	// Create an OSSClient instance.
	client, err := oss.New("<yourEndpoint>", "<yourAccessKeyId>", "<yourAccessKeySecret>")
	if err ! = nil {
		fmt.Println("Error:", err)
		os.Exit(-1)
	}

	bucketName := "<yourBucketName>"

	// View static web hosting configurations.
	wsRes, err := client.GetBucketWebsite(bucketName)
	if err ! = nil {
		fmt.Println("Error:", err)
		os.Exit(-1)
	}
	fmt.Println("indexWebsite: ", wsRes.IndexDocument.Suffix)
	fmt.Println("errorWebsite: ", wsRes.ErrorDocument.Key)
}

Delete static website hosting configurations

Run the following code to delete static website hosting configurations:

package main

import (
	"fmt"
	"os"
	"github.com/aliyun/aliyun-oss-go-sdk/oss"
)

func main() {
	// Create an OSSClient instance.
	client, err := oss.New("<yourEndpoint>", "<yourAccessKeyId>", "<yourAccessKeySecret>")
	if err ! = nil {
		fmt.Println("Error:", err)
		os.Exit(-1)
	}

	bucketName := "<yourBucketName>"

	// Delete static website hosting configurations.
	err = client.DeleteBucketWebsite(bucketName)
	if err ! = nil {
		fmt.Println("Error:", err)
		os.Exit(-1)
	}
}