镜像回源主要用于数据无缝迁移到 TOS 的场景。例如 Web 服务已经在您自建的源站上运行,您将静态网页迁移到 TOS 中存储,可配置镜像回源规则获取未迁移至 TOS 中的部分数据。
注意
以下代码用于设置指定存储桶的镜像回源规则。
package main import ( "context" "fmt" "net/http" "github.com/volcengine/ve-tos-golang-sdk/v2/tos" "github.com/volcengine/ve-tos-golang-sdk/v2/tos/enum" ) 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 if clientErr, ok := err.(*tos.TosClientError); ok { fmt.Println("Error:", clientErr.Error()) fmt.Println("Client Cause Err:", clientErr.Cause.Error()) } 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) mirrorBucketRule1 := tos.MirrorBackRule{ // 设置规则 ID ID: "rule1", Condition: tos.Condition{ // 触发回源规则的错误码,目前只支持 404 HttpCode: http.StatusNotFound, }, Redirect: tos.Redirect{ // 回源规则跳转类型 RedirectType: enum.RedirectTypeMirror, // 重定向后是否去配置源站拉取数据 FetchSourceOnRedirect: true, // 执行跳转时是否携带请求参数 PassQuery: true, // 镜像回源结果是 3XX 时, 是否跳转到 Location 获取数据 FollowRedirect: true, // MirrorHeader: tos.MirrorHeader{ // 是否透传全部 HTTP HEADER 到源端 PassAll: false, // 透传指定的 HEADER 到源站 Pass: []string{"header-1", "header-2"}, // 禁止透传指定的 HEADER 到源站 Remove: []string{"header-3", "header-4"}, }, // 公共可访问的源端地址 PublicSource: tos.PublicSource{SourceEndpoint: tos.SourceEndpoint{ Primary: []string{"https://www.example.com"}, Follower: nil, }}, }, } // 设置镜像回源规则 putOutput, err := client.PutBucketMirrorBack(ctx, &tos.PutBucketMirrorBackInput{Bucket: bucketName, Rules: []tos.MirrorBackRule{mirrorBucketRule1}}) checkErr(err) fmt.Println("PutBucketMirrorBack Request ID:", putOutput.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 if clientErr, ok := err.(*tos.TosClientError); ok { fmt.Println("Error:", clientErr.Error()) fmt.Println("Client Cause Err:", clientErr.Cause.Error()) } 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) // 获取镜像回源规则 getOutput, err := client.GetBucketMirrorBack(ctx, &tos.GetBucketMirrorBackInput{Bucket: bucketName}) checkErr(err) fmt.Println("GetBucketMirrorBack Request ID:", getOutput.RequestID) fmt.Println("GetBucketMirrorBack Rule length:", len(getOutput.Rules)) for _, rule := range getOutput.Rules { // 查看规则 ID fmt.Println("rule ID:", rule.ID) // 查看触发回源规则的错误码,目前只支持 404 fmt.Println("rule Condition HttpCode:", rule.Condition.HttpCode) // 查看回源规则跳转类型 fmt.Println("rule Redirect RedirectType:", rule.Redirect.RedirectType) // 重定向后是否去配置源站拉取数据 fmt.Println("rule Redirect FetchSourceOnRedirect:", rule.Redirect.FetchSourceOnRedirect) // 执行跳转时是否携带请求参数 fmt.Println("rule Redirect PassQuery:", rule.Redirect.PassQuery) // 镜像回源结果是 3XX 时, 是否跳转到 Location 获取数据 fmt.Println("rule Redirect FollowRedirect:", rule.Redirect.FollowRedirect) // 是否透传全部 HTTP HEADER 到源端 fmt.Println("rule Redirect MirrorHeader PassAll:", rule.Redirect.MirrorHeader.PassAll) // 透传指定的 HEADER 到源站 fmt.Println("rule Redirect MirrorHeader Pass:", rule.Redirect.MirrorHeader.Pass) // 禁止透传指定的 HEADER 到源站 fmt.Println("rule Redirect MirrorHeader Remove:", rule.Redirect.MirrorHeader.Remove) // 公共可访问的源端地址 fmt.Println("rule Redirect PublicSource SourceEndpoint:", rule.Redirect.PublicSource.SourceEndpoint) } }
注意
您必须为桶的所有者,才能删除桶的镜像回源规则。
以下代码用于删除指定存储桶的镜像回源规则:
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 if clientErr, ok := err.(*tos.TosClientError); ok { fmt.Println("Error:", clientErr.Error()) fmt.Println("Client Cause Err:", clientErr.Cause.Error()) } 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) // 删除镜像回源规则 deleteOutput, err := client.DeleteBucketMirrorBack(ctx, &tos.DeleteBucketMirrorBackInput{Bucket: bucketName}) fmt.Println("DeleteBucketMirrorBack Request ID:", deleteOutput.RequestID) }
关于镜像回源的更多信息,请参见设置回源规则。