You need to enable JavaScript to run this app.
导航
管理静态网站(Go SDK)
最近更新时间:2024.02.04 18:31:05首次发布时间:2023.03.15 14:39:19

TOS 支持托管静态网站,您可以将静态网站的所有内容存储至 TOS 存储桶,配置静态网站规则后,即可通过存储桶域名访问该网站。

设置网站配置规则

注意

  • 一个存储桶中只能创建一条静态网站托管规则。
  • 设置静态网站后,必须绑定自定义域名才能生效,具体操作,请参见绑定自定义域名
  • 出于安全合规考虑,从 2022年10月18日开始,如果您使用存储桶的默认域名访问网页类型文件(mimetype为text/html,扩展名包括 HTM、HTML、JSP、PLG、HTX、STM),Response Header中会自动加上 Content-Disposition:attachment,即从浏览器访问网页类型文件时,将不会直接预览网站,而会将网站的内容下载到本地。

示例代码

以下代码用于设置重定向所有的请求到另外一个站点。

package main

import (
   "context"
   "fmt"

   "github.com/volcengine/ve-tos-golang-sdk/v2/tos"
)

func checkErr(err error) {
   if err != nil {
      if serverErr, ok := err.(*tos.TosServerError); ok {
         fmt.Println("Error:", serverErr.Error())
         fmt.Println("Request ID:", serverErr.RequestID)
         fmt.Println("Response Status Code:", serverErr.StatusCode)
         fmt.Println("Response Header:", serverErr.Header)
         fmt.Println("Response Err Code:", serverErr.Code)
         fmt.Println("Response Err Msg:", serverErr.Message)
      } else {
         fmt.Println("Error:", err)
      }
      panic(err)
   }
}

func main() {
   var (
      accessKey = os.Getenv("TOS_ACCESS_KEY")
      secretKey = os.Getenv("TOS_SECRET_KEY")
      // Bucket 对应的 Endpoint,以华北2(北京)为例:https://tos-cn-beijing.volces.com
      endpoint = "https://tos-cn-beijing.volces.com"
      region   = "cn-beijing"
      // 填写 BucketName
      bucketName = "*** Provide your bucket name ***"
      ctx        = context.Background()
   )
   // 初始化客户端
   client, err := tos.NewClientV2(endpoint, tos.WithRegion(region), tos.WithCredentials(tos.NewStaticCredentials(accessKey, secretKey)))
   checkErr(err)
   // 场景1 重定向所有的请求到另一个站点
   input := &tos.PutBucketWebsiteInput{
      Bucket: bucketName,
      RedirectAllRequestsTo: &tos.RedirectAllRequestsTo{
         HostName: "www.example.com",
         Protocol: "http",
      },
   }
   output, err := client.PutBucketWebsite(ctx, input)
   checkErr(err)
   fmt.Println("PutBucketWebsite Request ID:", output.RequestID)

   // 场景2 根据规则重定向请求
   input = &tos.PutBucketWebsiteInput{
      Bucket: bucketName,
      IndexDocument: &tos.IndexDocument{
         Suffix: "index.html",
      },
      ErrorDocument: &tos.ErrorDocument{
         Key: "error.html",
      },
      // 前缀匹配,获取重定向结果
      RoutingRules: &tos.RoutingRules{Rules: []tos.RoutingRule{{
         // 重定向规则的条件配置
         Condition: tos.RoutingRuleCondition{
            // 指定重定向规则的对象键前缀匹配条件
            KeyPrefixEquals: "prefix",
            // 指定重定向规则的错误码匹配条件
            HttpErrorCodeReturnedEquals: 403,
         },
         // 重定向规则的具体重定向目标配置
         Redirect: tos.RoutingRuleRedirect{
            // 指定重定向规则的目标协议
            Protocol: "https",
            HostName: "www.redirect.com",
         },
      },
      }},
   }
   output, err = client.PutBucketWebsite(ctx, input)
   checkErr(err)
   fmt.Println("PutBucketWebsite Request ID:", output.RequestID)

}

获取网站配置规则

注意

要获取桶的网站配置规则,默认您必须为桶所有者。

示例代码

以下代码用于获取桶的网站配置规则。

package main

import (
   "context"
   "fmt"

   "github.com/volcengine/ve-tos-golang-sdk/v2/tos"
)

func checkErr(err error) {
   if err != nil {
      if serverErr, ok := err.(*tos.TosServerError); ok {
         fmt.Println("Error:", serverErr.Error())
         fmt.Println("Request ID:", serverErr.RequestID)
         fmt.Println("Response Status Code:", serverErr.StatusCode)
         fmt.Println("Response Header:", serverErr.Header)
         fmt.Println("Response Err Code:", serverErr.Code)
         fmt.Println("Response Err Msg:", serverErr.Message)
      } else {
         fmt.Println("Error:", err)
      }
      panic(err)
   }
}

func main() {
   var (
      accessKey = os.Getenv("TOS_ACCESS_KEY")
      secretKey = os.Getenv("TOS_SECRET_KEY")
      // Bucket 对应的 Endpoint,以华北2(北京)为例:https://tos-cn-beijing.volces.com
      endpoint = "https://tos-cn-beijing.volces.com"
      region   = "cn-beijing"
      // 填写 BucketName
      bucketName = "*** Provide your bucket name ***"
      ctx        = context.Background()
   )
   // 初始化客户端
   client, err := tos.NewClientV2(endpoint, tos.WithRegion(region), tos.WithCredentials(tos.NewStaticCredentials(accessKey, secretKey)))
   checkErr(err)
   output, err := client.GetBucketWebsite(ctx, &tos.GetBucketWebsiteInput{Bucket: bucketName})
   checkErr(err)
   fmt.Println("GetBucketWebsite Request ID:", output.RequestID)
   
   if output.RedirectAllRequestsTo != nil {
      fmt.Println("GetBucketWebsite RedirectAllRequestsTo HostName:", output.RedirectAllRequestsTo.HostName)
      fmt.Println("GetBucketWebsite RedirectAllRequestsTo Protocol:", output.RedirectAllRequestsTo.Protocol)
   }

   if output.IndexDocument != nil {
      fmt.Println("GetBucketWebsite IndexDocument ForbiddenSubDir:", output.IndexDocument.ForbiddenSubDir)
      fmt.Println("GetBucketWebsite IndexDocument Suffix:", output.IndexDocument.Suffix)

   }

   if output.ErrorDocument != nil {
      fmt.Println("GetBucketWebsite ErrorDocument Key:", output.ErrorDocument.Key)
   }

   for _, rule := range output.RoutingRules {
      fmt.Println("GetBucketWebsite RoutingRules Rule Condition HttpErrorCodeReturnedEquals:", rule.Condition.HttpErrorCodeReturnedEquals)
      fmt.Println("GetBucketWebsite RoutingRules Rule Condition KeyPrefixEquals:", rule.Condition.KeyPrefixEquals)
      fmt.Println("GetBucketWebsite RoutingRules Rule Redirect HostName:", rule.Redirect.HostName)
      fmt.Println("GetBucketWebsite RoutingRules Rule Redirect Protocol:", rule.Redirect.Protocol)
      fmt.Println("GetBucketWebsite RoutingRules Rule Redirect ReplaceKeyWith:", rule.Redirect.ReplaceKeyWith)
      fmt.Println("GetBucketWebsite RoutingRules Rule Redirect ReplaceKeyPrefixWith:", rule.Redirect.ReplaceKeyPrefixWith)
      fmt.Println("GetBucketWebsite RoutingRules Rule Redirect HttpRedirectCode:", rule.Redirect.HttpRedirectCode)
   }

}

删除网站配置规则

注意

要删除桶的网站配置规则,默认您必须为桶所有者。

示例代码

以下代码用于删除桶的网站配置规则。

package main

import (
   "context"
   "fmt"

   "github.com/volcengine/ve-tos-golang-sdk/v2/tos"
)

func checkErr(err error) {
   if err != nil {
      if serverErr, ok := err.(*tos.TosServerError); ok {
         fmt.Println("Error:", serverErr.Error())
         fmt.Println("Request ID:", serverErr.RequestID)
         fmt.Println("Response Status Code:", serverErr.StatusCode)
         fmt.Println("Response Header:", serverErr.Header)
         fmt.Println("Response Err Code:", serverErr.Code)
         fmt.Println("Response Err Msg:", serverErr.Message)
      } else {
         fmt.Println("Error:", err)
      }
      panic(err)
   }
}

func main() {
   var (
      accessKey = os.Getenv("TOS_ACCESS_KEY")
      secretKey = os.Getenv("TOS_SECRET_KEY")
      // Bucket 对应的 Endpoint,以华北2(北京)为例:https://tos-cn-beijing.volces.com
      endpoint = "https://tos-cn-beijing.volces.com"
      region   = "cn-beijing"
      // 填写 BucketName
      bucketName = "*** Provide your bucket name ***"
      ctx        = context.Background()
   )
   // 初始化客户端
   client, err := tos.NewClientV2(endpoint, tos.WithRegion(region), tos.WithCredentials(tos.NewStaticCredentials(accessKey, secretKey)))
   checkErr(err)
   output, err := client.DeleteBucketWebsite(ctx, &tos.DeleteBucketWebsiteInput{Bucket: bucketName})
   checkErr(err)
   fmt.Println("DeleteBucketWebsite Request ID:", output.RequestID)
}

相关文档

关于设置静态网站配置的更多信息,请参见设置静态网站