You can enable static website hosting for a bucket and configure redirection rules (RoutingRule) for mirroring-based back-to-origin. After static website hosting is configured, you can access the website hosted on the bucket, and you are automatically redirected to the specified index page or error page. After the redirection rules for mirroring-based back-to-origin take effect, you can use this feature to seamlessly migrate data to OSS.
Usage notes
In this topic, the public endpoint of the China (Hangzhou) region is used. If you want to access OSS from other Alibaba Cloud services in the same region as OSS, use an internal endpoint. For more information about OSS regions and endpoints, see Regions and endpoints.
In this topic, access credentials are obtained from environment variables. For more information about how to configure access credentials, see Configure access credentials.
In this topic, an OSSClient instance is created by using an OSS endpoint. If you want to create an OSSClient instance by using custom domain names or Security Token Service (STS), see Configure OSSClient instances.
To configure static website hosting or mirroring-based back-to-origin, you must have the
oss:PutBucketWebsitepermission. To query static website hosting configurations or mirroring-based back-to-origin rules, you must have theoss:GetBucketWebsitepermission. To delete static website hosting configurations or mirroring-based back-to-origin rules, you must have theoss:DeleteBucketWebsitepermission. For more information, see Grant custom permissions to a RAM user.
Static website hosting
A static website consists of webpages with static content, including client-side scripts such as JavaScript. You can use the static website hosting feature to host your static website on a bucket and access the website using the endpoint of the bucket.
Configure static website hosting
The following code shows how to configure static website hosting:
package main import ( "fmt" "os" "github.com/aliyun/aliyun-oss-go-sdk/oss" ) func main() { // Obtain access credentials from environment variables. Before you run this sample code, make sure that the OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET environment variables are set. provider, err := oss.NewEnvironmentVariableCredentialsProvider() if err != nil { fmt.Println("Error:", err) os.Exit(-1) } // Create an OSSClient instance. // Set yourEndpoint to the endpoint of the bucket. For example, for a bucket in the China (Hangzhou) region, set the endpoint to https://oss-cn-hangzhou.aliyuncs.com. For other regions, set the endpoint as needed. // Set yourRegion to the region where the bucket is located. For example, for a bucket in the China (Hangzhou) region, set the region to cn-hangzhou. For other regions, set the region as needed. clientOptions := []oss.ClientOption{oss.SetCredentialsProvider(&provider)} clientOptions = append(clientOptions, oss.Region("yourRegion")) // Set the signature version. clientOptions = append(clientOptions, oss.AuthVersion(oss.AuthV4)) client, err := oss.New("yourEndpoint", "", "", clientOptions...) if err != nil { fmt.Println("Error:", err) os.Exit(-1) } // Specify the bucket name. For example, examplebucket. bucketName := "examplebucket" // Configure static website hosting to use index.html as the default homepage and error.html as the default 404 page. err = client.SetBucketWebsite(bucketName, "index.html", "error.html") if err != nil { fmt.Println("Error:", err) os.Exit(-1) } }View the static website hosting configuration
The following code shows how to view the static website hosting configuration:
package main import ( "fmt" "os" "github.com/aliyun/aliyun-oss-go-sdk/oss" ) func main() { // Obtain access credentials from environment variables. Before you run this sample code, make sure that the OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET environment variables are set. provider, err := oss.NewEnvironmentVariableCredentialsProvider() if err != nil { fmt.Println("Error:", err) os.Exit(-1) } // Create an OSSClient instance. // Set yourEndpoint to the endpoint of the bucket. For example, for a bucket in the China (Hangzhou) region, set the endpoint to https://oss-cn-hangzhou.aliyuncs.com. For other regions, set the endpoint as needed. // Set yourRegion to the region where the bucket is located. For example, for a bucket in the China (Hangzhou) region, set the region to cn-hangzhou. For other regions, set the region as needed. clientOptions := []oss.ClientOption{oss.SetCredentialsProvider(&provider)} clientOptions = append(clientOptions, oss.Region("yourRegion")) // Set the signature version. clientOptions = append(clientOptions, oss.AuthVersion(oss.AuthV4)) client, err := oss.New("yourEndpoint", "", "", clientOptions...) if err != nil { fmt.Println("Error:", err) os.Exit(-1) } // Specify the bucket name. For example, examplebucket. bucketName := "examplebucket" // View the static website hosting configuration. 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 the static website hosting configuration
The following code shows how to delete the static website hosting configuration:
package main import ( "fmt" "os" "github.com/aliyun/aliyun-oss-go-sdk/oss" ) func main() { // Obtain access credentials from environment variables. Before you run this sample code, make sure that the OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET environment variables are set. provider, err := oss.NewEnvironmentVariableCredentialsProvider() if err != nil { fmt.Println("Error:", err) os.Exit(-1) } // Create an OSSClient instance. // Set yourEndpoint to the endpoint of the bucket. For example, for a bucket in the China (Hangzhou) region, set the endpoint to https://oss-cn-hangzhou.aliyuncs.com. For other regions, set the endpoint as needed. // Set yourRegion to the region where the bucket is located. For example, for a bucket in the China (Hangzhou) region, set the region to cn-hangzhou. For other regions, set the region as needed. clientOptions := []oss.ClientOption{oss.SetCredentialsProvider(&provider)} clientOptions = append(clientOptions, oss.Region("yourRegion")) // Set the signature version. clientOptions = append(clientOptions, oss.AuthVersion(oss.AuthV4)) client, err := oss.New("yourEndpoint", "", "", clientOptions...) if err != nil { fmt.Println("Error:", err) os.Exit(-1) } // Specify the bucket name. For example, examplebucket. bucketName := "examplebucket" // Delete the static website hosting configuration. err = client.DeleteBucketWebsite(bucketName) if err != nil { fmt.Println("Error:", err) os.Exit(-1) } }
Mirroring-based back-to-origin
Mirroring-based back-to-origin is primarily used for seamless data migration to OSS. For example, a service is running on a self-managed origin server or on other cloud products. Because of business growth, you may need to migrate the service to OSS without service interruptions. During the migration, you can use mirroring-based back-to-origin rules to retrieve data that is not yet migrated to OSS to ensure business continuity.
Configure mirroring-based back-to-origin
For example, if a requester tries to access an object that does not exist in the destination bucket, you can specify back-to-origin conditions and an origin URL to fetch the object from the origin. Assume that you have a bucket named examplebucket in the China (Hangzhou) region. If a requester tries to access a non-existent file in the `examplefolder/` directory in the bucket's root directory, you can configure OSS to fetch the file from the `examplefolder/` directory of the www.example.com site.
The following code shows how to configure mirroring-based back-to-origin rules for the preceding scenario:
package main import ( "fmt" "os" "github.com/aliyun/aliyun-oss-go-sdk/oss" ) func main() { // Obtain access credentials from environment variables. Before you run this sample code, make sure that the OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET environment variables are set. provider, err := oss.NewEnvironmentVariableCredentialsProvider() if err != nil { fmt.Println("Error:", err) os.Exit(-1) } // Create an OSSClient instance. // Set yourEndpoint to the endpoint of the bucket. For example, for a bucket in the China (Hangzhou) region, set the endpoint to https://oss-cn-hangzhou.aliyuncs.com. For other regions, set the endpoint as needed. // Set yourRegion to the region where the bucket is located. For example, for a bucket in the China (Hangzhou) region, set the region to cn-hangzhou. For other regions, set the region as needed. clientOptions := []oss.ClientOption{oss.SetCredentialsProvider(&provider)} clientOptions = append(clientOptions, oss.Region("yourRegion")) // Set the signature version. clientOptions = append(clientOptions, oss.AuthVersion(oss.AuthV4)) client, err := oss.New("yourEndpoint", "", "", clientOptions...) if err != nil { fmt.Println("Error:", err) os.Exit(-1) } // Specify the bucket name. For example, examplebucket. bucketName := "examplebucket" var indexWebsite = "myindex.html" var errorWebsite = "myerror.html" btrue := true bfalse := false // Set the back-to-origin type to Mirror. ruleOk := oss.RoutingRule{ RuleNumber: 1, Condition: oss.Condition{ KeyPrefixEquals: "", // Set the back-to-origin condition to an HTTP status code of 404. HTTPErrorCodeReturnedEquals: 404, }, Redirect: oss.Redirect{ RedirectType: "Mirror", // PassQueryString: &btrue, // Specify the origin URL. MirrorURL: "http://www.test.com/", // MirrorPassQueryString:&btrue, // MirrorFollowRedirect:&bfalse, // MirrorCheckMd5:&bfalse, MirrorHeaders: oss.MirrorHeaders{ // PassAll:&bfalse, // Allow specified HTTP header parameters to be passed. Pass: []string{"myheader-key1", "myheader-key2"}, // Prohibit specified HTTP header parameters from being passed. Remove: []string{"myheader-key3", "myheader-key4"}, Set: []oss.MirrorHeaderSet{ { Key: "myheader-key5", Value: "myheader-value5", }, }, }, }, } // Set the back-to-origin type to Redirection. ruleArrOk := []oss.RoutingRule{ { RuleNumber: 2, Condition: oss.Condition{ // Set the back-to-origin conditions to an HTTP status code of 404 and the object prefix to abc/. KeyPrefixEquals: "abc/", HTTPErrorCodeReturnedEquals: 404, IncludeHeader: []oss.IncludeHeader{ { Key: "host", Equals: "test.oss-cn-beijing-internal.aliyuncs.com", }, }, }, Redirect: oss.Redirect{ RedirectType: "AliCDN", Protocol: "http", HostName: "www.test.com", PassQueryString: &bfalse, ReplaceKeyWith: "prefix/${key}.suffix", HttpRedirectCode: 301, }, }, // Set the back-to-origin type to Mirror. { RuleNumber: 3, Condition: oss.Condition{ KeyPrefixEquals: "", HTTPErrorCodeReturnedEquals: 404, }, Redirect: oss.Redirect{ RedirectType: "Mirror", PassQueryString: &btrue, MirrorURL: "http://www.test.com/", MirrorPassQueryString: &btrue, MirrorFollowRedirect: &bfalse, MirrorCheckMd5: &bfalse, MirrorHeaders: oss.MirrorHeaders{ PassAll: &btrue, Pass: []string{"myheader-key1", "myheader-key2"}, Remove: []string{"myheader-key3", "myheader-key4"}, Set: []oss.MirrorHeaderSet{ { Key: "myheader-key5", Value: "myheader-value5", }, }, }, }, }, } wxmlOne := oss.WebsiteXML{ IndexDocument: oss.IndexDocument{ Suffix: indexWebsite, }, ErrorDocument: oss.ErrorDocument{ Key: errorWebsite, }, } wxmlOne.RoutingRules = append(wxmlOne.RoutingRules, ruleOk) wxmlOne.RoutingRules = append(wxmlOne.RoutingRules, ruleArrOk...) err = client.SetBucketWebsiteDetail(bucketName, wxmlOne) if err != nil { fmt.Println("Error:", err) os.Exit(-1) } }Retrieve the mirroring-based back-to-origin configuration
The following code shows how to retrieve the mirroring-based back-to-origin configuration:
package main import ( "fmt" "os" "github.com/aliyun/aliyun-oss-go-sdk/oss" ) func main() { // Obtain access credentials from environment variables. Before you run this sample code, make sure that the OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET environment variables are set. provider, err := oss.NewEnvironmentVariableCredentialsProvider() if err != nil { fmt.Println("Error:", err) os.Exit(-1) } // Create an OSSClient instance. // Set yourEndpoint to the endpoint of the bucket. For example, for a bucket in the China (Hangzhou) region, set the endpoint to https://oss-cn-hangzhou.aliyuncs.com. For other regions, set the endpoint as needed. // Set yourRegion to the region where the bucket is located. For example, for a bucket in the China (Hangzhou) region, set the region to cn-hangzhou. For other regions, set the region as needed. clientOptions := []oss.ClientOption{oss.SetCredentialsProvider(&provider)} clientOptions = append(clientOptions, oss.Region("yourRegion")) // Set the signature version. clientOptions = append(clientOptions, oss.AuthVersion(oss.AuthV4)) client, err := oss.New("yourEndpoint", "", "", clientOptions...) if err != nil { fmt.Println("Error:", err) os.Exit(-1) } // Specify the bucket name. For example, examplebucket. bucketName := "examplebucket" // Get the mirroring-based back-to-origin configuration. data, err := client.GetBucketWebsiteXml(bucketName) if err != nil { fmt.Println("Error:", err) os.Exit(-1) } fmt.Println(data) }Delete the mirroring-based back-to-origin configuration
The following code shows how to delete the mirroring-based back-to-origin configuration:
package main import ( "fmt" "os" "github.com/aliyun/aliyun-oss-go-sdk/oss" ) func main() { // Obtain access credentials from environment variables. Before you run this sample code, make sure that the OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET environment variables are set. provider, err := oss.NewEnvironmentVariableCredentialsProvider() if err != nil { fmt.Println("Error:", err) os.Exit(-1) } // Create an OSSClient instance. // Set yourEndpoint to the endpoint of the bucket. For example, for a bucket in the China (Hangzhou) region, set the endpoint to https://oss-cn-hangzhou.aliyuncs.com. For other regions, set the endpoint as needed. // Set yourRegion to the region where the bucket is located. For example, for a bucket in the China (Hangzhou) region, set the region to cn-hangzhou. For other regions, set the region as needed. clientOptions := []oss.ClientOption{oss.SetCredentialsProvider(&provider)} clientOptions = append(clientOptions, oss.Region("yourRegion")) // Set the signature version. clientOptions = append(clientOptions, oss.AuthVersion(oss.AuthV4)) client, err := oss.New("yourEndpoint", "", "", clientOptions...) if err != nil { fmt.Println("Error:", err) os.Exit(-1) } // Specify the bucket name. For example, examplebucket. bucketName := "examplebucket" // Delete the mirroring-based back-to-origin configuration. err = client.DeleteBucketWebsite(bucketName) if err != nil { fmt.Println("Error:", err) os.Exit(-1) } }
References
For the complete sample code for static website hosting and mirroring-based back-to-origin, see GitHub sample.
For more information about the API operation to configure static website hosting or mirroring-based back-to-origin, see PutBucketWebsite.
For more information about the API operation to query static website hosting or mirroring-based back-to-origin configurations, see GetBucketWebsite.
For more information about the API operation to delete static website hosting or mirroring-based back-to-origin configurations, see DeleteBucketWebsite.