TOS 支持跨区域复制,您可以将一个地域的对象复制到不同地域的存储桶中。开启跨区域复制规则后,当您在源存储桶中上传新文件时,TOS 会自动将文件同步至目的桶内。该功能用于满足异地容灾和数据复制的需求。
StorageClassIDSourceObject
时与源对象一致, 该配置项值为 StorageClassIDDestinationBucket
时与目标桶一致。以下代码用于设置桶的跨区域复制规则。
package main import ( "context" "fmt" "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 { 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) // 设置跨区域规则 rule := tos.ReplicationRule{ // 规则 ID ID: "rule_id", // 指定是否开启跨区域复制规则 Status: enum.StatusEnabled, // 指定跨区域复制匹配对象的前缀,空则代表复制全部对象。 PrefixSet: nil, // 指定复制到的目标信息 Destination: tos.Destination{ // 目标桶名 Bucket: "dst_bucket", // 目标 Region 信息 Location: "cn-shanghai", // 指定复制对象的存储类型规则 // enum.StorageClassIDSourceObject: 与源对象保持一致 // enum.StorageClassIDDestinationBucket: 与目标桶保持一致 StorageClassInheritDirective: enum.StorageClassIDSourceObject, }, // 是否需要复制历史对象 HistoricalObjectReplication: enum.StatusDisabled, } // 设置 Role role := "your role" output, err := client.PutBucketReplication(ctx, &tos.PutBucketReplicationInput{ Bucket: bucketName, Role: role, Rules: []tos.ReplicationRule{rule}, }) checkErr(err) fmt.Println("PutBucketReplication 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) input := &tos.GetBucketReplicationInput{ Bucket: bucketName, // 不指定 RuleID 查询当前桶所有跨区域复制规则 // 指定 RuleID 时,返回指定规则 RuleID: "ruleId", } output, err := client.GetBucketReplication(ctx, input) checkErr(err) fmt.Println("GetBucketReplication Request ID:", output.RequestID) for _, rule := range output.Rules { // 规则 ID fmt.Println("rule id:", rule.ID) // 规则状态 fmt.Println("rule status:", rule.Status) for _, prefix := range rule.PrefixSet { // 规则前缀 fmt.Println("rule prefix:", prefix) } // 目标桶 fmt.Println("rule dst bucket:", rule.Destination.Bucket) // 目标地域 fmt.Println("rule dst location:", rule.Destination.Location) // 存储类型 fmt.Println("rule dst storage class:", rule.Destination.StorageClass) // 存储类型规则 fmt.Println("rule dst storage class inherit directive:", rule.Destination.StorageClassInheritDirective) } }
注意
以下代码用于获取桶的跨区域复制进度。
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) input := &tos.GetBucketReplicationInput{ Bucket: bucketName, // 不指定 RuleID 查询当前桶所有跨区域复制规则 // 指定 RuleID 时,返回指定规则 RuleID: "ruleId", } output, err := client.GetBucketReplication(ctx, input) checkErr(err) fmt.Println("GetBucketReplication Request ID:", output.RequestID) for _, rule := range output.Rules { // 规则 ID fmt.Println("rule id:", rule.ID) // 历史复制进度 fmt.Println("rule historical object:", rule.Progress.HistoricalObject) // 增量进度时间点 fmt.Println("rule new object:", rule.Progress.NewObject) } }
注意
要删除桶的跨区域复制规则,默认您必须为桶所有者。
以下代码用于删除桶的跨区域复制规则。
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) input := &tos.DeleteBucketReplicationInput{ Bucket: bucketName, } output, err := client.DeleteBucketReplication(ctx, input) checkErr(err) fmt.Println("DeleteBucketReplication Request ID:", output.RequestID) }
关于设置跨区域复制规则的更多介绍,请参见跨区域复制规则。