You can enable static website hosting for buckets and configure mirroring-based back-to-origin rules. After you host a static website on a bucket, you can visit the website by directly accessing the bucket. You are automatically redirected to the specified index page or error page. After mirroring-based back-to-origin rules are configured and take effect, you can use mirroring-based back-to-origin to seamlessly migrate data to Object Storage Service (OSS).

Static website hosting

Static websites are websites in which all web pages consist only of static content, including scripts such as JavaScript code that can be run on the client. You can use the static website hosting feature to host your static website in an OSS bucket and use the domain name of the bucket to access the website.

  • Configure static website hosting

    The following code provides an example on how to configure static website hosting:
    package main
    
    import (
        "fmt"
        "os"
        "github.com/aliyun/aliyun-oss-go-sdk/oss"
    )
    
    func main() {
        // Create an OSSClient instance. 
        // Set yourEndpoint to the endpoint of the region in which the bucket is located. For example, if the bucket is located in the China (Hangzhou) region, set yourEndpoint to https://oss-cn-hangzhou.aliyuncs.com. Specify the endpoint based on your business requirements. 
        // Security risks may arise if you use the AccessKey pair of an Alibaba Cloud account to access OSS because the account has permissions on all API operations. We recommend that you use a RAM user to call API operations or perform routine operations and maintenance. To create a RAM user, log on to the RAM console. 
        client, err := oss.New("yourEndpoint", "yourAccessKeyId", "yourAccessKeySecret")
        if err != nil {
            fmt.Println("Error:", err)
            os.Exit(-1)
        }
        // Specify the name of the bucket. Example: examplebucket. 
        bucketName := "examplebucket"
    
        // Set the default homepage to index.html and the default 404 page to error.html for the static website that is hosted in the bucket. 
        err = client.SetBucketWebsite(bucketName, "index.html", "error.html")
        if err != nil {
            fmt.Println("Error:", err)
            os.Exit(-1)
        }
    }            
  • Query static website hosting configurations

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

    package main
    
    import (
        "fmt"
        "os"
        "github.com/aliyun/aliyun-oss-go-sdk/oss"
    )
    
    func main() {
        // Create an OSSClient instance. 
        // Set yourEndpoint to the endpoint of the region in which the bucket is located. For example, if the bucket is located in the China (Hangzhou) region, set yourEndpoint to https://oss-cn-hangzhou.aliyuncs.com. Specify the endpoint based on your business requirements. 
        // Security risks may arise if you use the AccessKey pair of an Alibaba Cloud account to access OSS because the account has permissions on all API operations. We recommend that you use a RAM user to call API operations or perform routine operations and maintenance. To create a RAM user, log on to the RAM console. 
        client, err := oss.New("yourEndpoint", "yourAccessKeyId", "yourAccessKeySecret")
        if err != nil {
            fmt.Println("Error:", err)
            os.Exit(-1)
        }
    
        // Specify the name of the bucket. Example: examplebucket. 
        bucketName := "examplebucket"
    
        // Query the static website 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

    The following code provides an example on how 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. 
        // Set yourEndpoint to the endpoint of the region in which the bucket is located. For example, if the bucket is located in the China (Hangzhou) region, set yourEndpoint to https://oss-cn-hangzhou.aliyuncs.com. Specify the endpoint based on your business requirements. 
        // Security risks may arise if you use the AccessKey pair of an Alibaba Cloud account to access OSS because the account has permissions on all API operations. We recommend that you use a RAM user to call API operations or perform routine operations and maintenance. To create a RAM user, log on to the RAM console. 
        client, err := oss.New("yourEndpoint", "yourAccessKeyId", "yourAccessKeySecret")
        if err != nil {
            fmt.Println("Error:", err)
            os.Exit(-1)
        }
    
        // Specify the name of the bucket. Example: examplebucket. 
        bucketName := "examplebucket"
    
        // Delete the static website hosting configurations. 
        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 used to seamlessly migrate data to OSS. For example, you can migrate services from a self-managed origin or from another cloud service to OSS without service interruption. You can use mirroring-based back-to-origin rules during migration to obtain the data that is not migrated to OSS. This ensures service continuity.

  • Configure mirroring-based back-to-origin

    If a requester attempts to access an object in the specified bucket and the object does not exist, you can specify the URL of the object in the origin and back-to-origin conditions to allow the requester to obtain the object. For example, a bucket named examplebucket is located in the China (Hangzhou) region. When a requester attempts to access an object in the examplefolder directory of the root directory of the bucket and the object does not exist, the requester is redirected to www.example.com to access the required object that is stored in the examplefolder directory of the origin.

    The following code provides an example on how to configure mirroring-based back-to-origin rules in the preceding scenario:

    package main
    
    import (
        "fmt"
        "os"
        "github.com/aliyun/aliyun-oss-go-sdk/oss"
    )
    
    func main() {
        // Create an OSSClient instance. 
        // Set yourEndpoint to the endpoint of the region in which the bucket is located. For example, if the bucket is located in the China (Hangzhou) region, set yourEndpoint to https://oss-cn-hangzhou.aliyuncs.com. Specify the endpoint based on your business requirements. 
        // Security risks may arise if you use the AccessKey pair of an Alibaba Cloud account to access OSS because the account has permissions on all API operations. We recommend that you use a RAM user to call API operations or perform routine operations and maintenance. To create a RAM user, log on to the RAM console. 
        client, err := oss.New("yourEndpoint", "yourAccessKeyId", "yourAccessKeySecret")
        if err != nil {
            fmt.Println("Error:", err)
            os.Exit(-1)
        }
    
        // Specify the name of the bucket. Example: examplebucket. 
        bucketName := "examplebucket"
        putXml := `<WebsiteConfiguration>
          <IndexDocument>
            <Suffix>index.html</Suffix>
            <SupportSubDir>true</SupportSubDir>
            <Type>0</Type>
          </IndexDocument>
          <ErrorDocument>
            <Key>error.html</Key>
            <HttpStatus>404</HttpStatus>
          </ErrorDocument>
          <RoutingRules>
            <RoutingRule>
              <RuleNumber>1</RuleNumber>
              <Condition>
                <! -- Specify the prefix of object names. Only the objects whose names contain the specified prefix match the rule. -->
                <KeyPrefixEquals>examplefolder/</KeyPrefixEquals>
                <! -- Specify the HTTP status code. The rule is matched only when the specified object is accessed and HTTP status code 404 is returned. -->
                <HttpErrorCodeReturnedEquals>404</HttpErrorCodeReturnedEquals>
              </Condition>
              <Redirect>
                <! -- Specify the redirect type. -->
                <RedirectType>Mirror</RedirectType>
                <PassQueryString>true</PassQueryString>
                <! -- Specify the origin URL for the mirroring-based back-to-origin rule. In this example, the origin URL is https://www.example.com/. -->
                <MirrorURL>http://example.com/</MirrorURL>-->
                <! -- Specify the status codes such as 4xx and 5xx that OSS must pass through with the response body to the client after OSS receives the status codes from the origin. -->
                <TransparentMirrorResponseCodes>400,404</TransparentMirrorResponseCodes>
                <! -- Specify whether to include parameters of the original request in the redirect request when the system runs the redirection rule or the mirroring-based back-to-origin rule. -->
                <PassQueryString>true</PassQueryString>
                <! -- This parameter plays the same role as PassQueryString and has a higher priority than PassQueryString. This parameter takes effect only when RedirectType is set to Mirror. -->
                <MirrorPassQueryString>true</MirrorPassQueryString>
                <! -- Specify the HTTP status code in the response. This parameter takes effect only when RedirectType is set to External or AliCDN. -->
                <HttpRedirectCode>302</HttpRedirectCode>
                <! -- Specify the domain name that is used for redirection, which must comply with the naming conventions for domains. -->
                <HostName>oss.aliyuncs.com</HostName>
                <! -- Specify the protocol that is used for redirection. This parameter takes effect only when RedirectType is set to External or AliCDN. -->
                <Protocol>https</Protocol>
                <! -- Specify the string that is used to replace the object name when the request is redirected. This parameter can be set to a variable. -->
                <ReplaceKeyWith>key.jpg</ReplaceKeyWith>
                <! -- If this parameter is set to true, the prefix of the object name is replaced with the value specified by ReplaceKeyPrefixWith. -->
                <EnableReplacePrefix>true</EnableReplacePrefix>
                <! -- Specify the string that is used to replace the prefix of the object name when the request is redirected. -->
                <ReplaceKeyPrefixWith>examplebucket</ReplaceKeyPrefixWith>
                <! -- Specify whether to check the MD5 hash of the body of the response that is returned by the origin. This parameter takes effect only when RedirectType is set to Mirror. -->
                <MirrorCheckMd5>false</MirrorCheckMd5>
                <! -- Specify whether to redirect the access to the address that is specified by Location if the origin returns HTTP status code 3xx.  This parameter takes effect only when RedirectType is set to Mirror. -->
                <MirrorFollowRedirect>true</MirrorFollowRedirect>
                <MirrorHeaders>
                  <! -- Specify whether to pass through all request headers to the origin. This parameter takes effect only when RedirectType is set to Mirror. -->
                  <PassAll>true</PassAll>
                  <! -- Specify the headers to pass through to the origin. This parameter takes effect only when RedirectType is set to Mirror. -->
                  <Pass>myheader-key1</Pass>
                  <Pass>myheader-key2</Pass>
                  <! -- Specify the headers that are not allowed to pass through to the origin. This parameter takes effect only when RedirectType is set to Mirror. -->
                  <Remove>myheader-key3</Remove>
                  <Remove>myheader-key4</Remove>
                   <! -- Specify the headers that are passed to the origin. The specified headers are passed to the origin regardless of whether they are included in the request. -->
                  <Set>
                    <Key>myheader-key5</Key>
                    <Value>myheader-value5</Value>
                  </Set>
                </MirrorHeaders>
              </Redirect>
            </RoutingRule>
          </RoutingRules>
        </WebsiteConfiguration>    `
    
        err = client.SetBucketWebsiteXml(bucketName,putXml)
        if err != nil {
            fmt.Println("Error:", err)
            os.Exit(-1)
        }
    }                    
  • Query mirroring-based back-to-origin configurations

    The following code provides an example on how to query mirroring-based back-to-origin configurations:

    package main
    
    import (
        "fmt"
        "os"
        "github.com/aliyun/aliyun-oss-go-sdk/oss"
    )
    
    func main() {
        // Create an OSSClient instance. 
        // Set yourEndpoint to the endpoint of the region in which the bucket is located. For example, if the bucket is located in the China (Hangzhou) region, set yourEndpoint to https://oss-cn-hangzhou.aliyuncs.com. Specify the endpoint based on your business requirements. 
        // Security risks may arise if you use the AccessKey pair of an Alibaba Cloud account to access OSS because the account has permissions on all API operations. We recommend that you use a RAM user to call API operations or perform routine operations and maintenance. To create a RAM user, log on to the RAM console. 
        client, err := oss.New("yourEndpoint", "yourAccessKeyId", "yourAccessKeySecret")
        if err != nil {
            fmt.Println("Error:", err)
            os.Exit(-1)
        }
    
        // Specify the name of the bucket. Example: examplebucket. 
        bucketName := "examplebucket"
    
        data,err := client.GetBucketWebsiteXml(bucketName)
        if err != nil {
          fmt.Println("Error:", err)
          os.Exit(-1)
        }
        fmt.Println(data)
    }           
  • Delete mirroring-based back-to-origin configurations

    The following code provides an example on how to delete mirroring-based back-to-origin configurations:

    package main
    
    import (
        "fmt"
        "os"
        "github.com/aliyun/aliyun-oss-go-sdk/oss"
    )
    
    func main() {
        // Create an OSSClient instance. 
        // Set yourEndpoint to the endpoint of the region in which the bucket is located. For example, if the bucket is located in the China (Hangzhou) region, set yourEndpoint to https://oss-cn-hangzhou.aliyuncs.com. Specify the endpoint based on your business requirements. 
        // Security risks may arise if you use the AccessKey pair of an Alibaba Cloud account to access OSS because the account has permissions on all API operations. We recommend that you use a RAM user to call API operations or perform routine operations and maintenance. To create a RAM user, log on to the RAM console. 
        client, err := oss.New("yourEndpoint", "yourAccessKeyId", "yourAccessKeySecret")
        if err != nil {
            fmt.Println("Error:", err)
            os.Exit(-1)
        }
    
        // Specify the name of the bucket. Example: examplebucket. 
        bucketName := "examplebucket"
    
        // Delete mirroring-based back-to-origin configurations. 
        err = client.DeleteBucketWebsite(bucketName)
        if err != nil {
            fmt.Println("Error:", err)
            os.Exit(-1)
        }
    }            

References

  • For more information about the complete sample code of static website hosting and mirroring-based back-to-origin, visit GitHub.
  • For more information about the API operation that you can call to configure static website hosting or mirroring-based back-to-origin, see PutBucketWebsite.
  • For more information about the API operation that you can call to query static website hosting or mirroring-based back-to-origin configurations, see GetBucketWebsite.
  • For more information about the API operation that you can call to delete static website hosting or mirroring-based back-to-origin configurations, see DeleteBucketWebsite.