TOS 支持跨区域复制,您可以将一个地域的对象复制到不同地域的存储桶中。配置跨区域复制规则后,当您在源桶中上传新文件时,TOS 会自动将文件同步至目的桶内。该功能用于满足异地容灾和数据复制的需求。
您可以通过 TOS Java SDK 的 putBucketReplication
接口设置指定桶的跨区域复制规则。
以下代码展示如何设置指定桶的跨区域复制规则。
import com.volcengine.tos.TOSV2; import com.volcengine.tos.TOSV2ClientBuilder; import com.volcengine.tos.TosClientException; import com.volcengine.tos.TosServerException; import com.volcengine.tos.comm.common.StatusType; import com.volcengine.tos.comm.common.StorageClassInheritDirectiveType; import com.volcengine.tos.model.bucket.Destination; import com.volcengine.tos.model.bucket.PutBucketReplicationInput; import com.volcengine.tos.model.bucket.PutBucketReplicationOutput; import com.volcengine.tos.model.bucket.ReplicationRule; import java.util.ArrayList; import java.util.Arrays; import java.util.List; public class PutBucketReplicationExample { public static void main(String[] args) { String endpoint = "your endpoint"; String region = "your region"; String accessKey = System.getenv("TOS_ACCESS_KEY"); String secretKey = System.getenv("TOS_SECRET_KEY"); String bucketName = "bucket-example"; // 目标region String destinationRegion = "your destination region"; // 目标bucket String destinationBucket = "your destination bucket"; String role = "your role"; TOSV2 tos = new TOSV2ClientBuilder().build(region, endpoint, accessKey, secretKey); try{ ReplicationRule rule = new ReplicationRule() // 设置规则id,必选 .setId("1") // 指定是否开启跨区域复制规则 .setStatus(StatusType.STATUS_ENABLED) // 指定跨区域复制匹配对象的前缀,不设置则默认复制全部对象 .setPrefixSet(Arrays.asList("prefix1", "prefix2")) // 指定复制到的目标信息 .setDestination(new Destination() // 目的桶和目的region,必选 .setBucket(destinationBucket).setLocation(destinationRegion) // 指定复制对象的存储类型规则 // STORAGE_CLASS_ID_SOURCE_OBJECT: 与源对象保持一致 // STORAGE_CLASS_ID_DESTINATION_BUCKET: 与目标桶保持一致 .setStorageClassInheritDirectiveType(StorageClassInheritDirectiveType.STORAGE_CLASS_ID_SOURCE_OBJECT)) // 是否需要复制历史对象 .setHistoricalObjectReplication(StatusType.STATUS_DISABLED); List<ReplicationRule> rules = new ArrayList<>(); rules.add(rule); PutBucketReplicationInput input = new PutBucketReplicationInput().setBucket(bucketName).setRules(rules).setRole(role); PutBucketReplicationOutput output = tos.putBucketReplication(input); System.out.println("putBucketReplication succeed"); } catch (TosClientException e) { // 操作失败,捕获客户端异常,一般情况是请求参数错误,此时请求并未发送 System.out.println("putBucketReplication failed"); System.out.println("Message: " + e.getMessage()); if (e.getCause() != null) { e.getCause().printStackTrace(); } } catch (TosServerException e) { // 操作失败,捕获服务端异常,可以获取到从服务端返回的详细错误信息 System.out.println("putBucketReplication failed"); System.out.println("StatusCode: " + e.getStatusCode()); System.out.println("Code: " + e.getCode()); System.out.println("Message: " + e.getMessage()); System.out.println("RequestID: " + e.getRequestID()); } catch (Throwable t) { // 作为兜底捕获其他异常,一般不会执行到这里 System.out.println("putBucketReplication failed"); System.out.println("unexpected exception, message: " + t.getMessage()); } } }
您可以通过 TOS Java SDK 的 getBucketReplication
接口获取指定桶配置的跨区域复制规则。
注意
要获取桶的跨区域复制规则,默认您必须为桶所有者。
以下代码展示如何获取桶配置的跨区域复制规则。
import com.volcengine.tos.TOSV2; import com.volcengine.tos.TOSV2ClientBuilder; import com.volcengine.tos.TosClientException; import com.volcengine.tos.TosServerException; import com.volcengine.tos.model.bucket.GetBucketReplicationInput; import com.volcengine.tos.model.bucket.GetBucketReplicationOutput; import com.volcengine.tos.model.bucket.ReplicationRule; public class GetBucketReplicationExample { public static void main(String[] args) { String endpoint = "your endpoint"; String region = "your region"; String accessKey = System.getenv("TOS_ACCESS_KEY"); String secretKey = System.getenv("TOS_SECRET_KEY"); String bucketName = "bucket-example"; TOSV2 tos = new TOSV2ClientBuilder().build(region, endpoint, accessKey, secretKey); try{ GetBucketReplicationInput input = new GetBucketReplicationInput().setBucket(bucketName); GetBucketReplicationOutput output = tos.getBucketReplication(input); if (output.getRules() != null && output.getRules().size() > 0) { for (int i = 0; i < output.getRules().size(); i++){ ReplicationRule rule = output.getRules().get(i); // 规则 ID System.out.println("rule id:" + rule.getId()); // 规则状态 System.out.println("rule status:" + rule.getStatus()); for (String prefix : rule.getPrefixSet()) { System.out.println("rule prefix:" + prefix); } // 目标桶 System.out.println("rule dst bucket:" + rule.getDestination().getBucket()); // 目标地域 System.out.println("rule dst location:" + rule.getDestination().getLocation()); // 存储类型 System.out.println("rule dst storage class:" + rule.getDestination().getStorageClass()); // 存储类型规则 System.out.println("rule dst storage class inherit directive:" + rule.getDestination().getStorageClassInheritDirectiveType()); } } } catch (TosClientException e) { // 操作失败,捕获客户端异常,一般情况是请求参数错误,此时请求并未发送 System.out.println("getBucketLocation failed"); System.out.println("Message: " + e.getMessage()); if (e.getCause() != null) { e.getCause().printStackTrace(); } } catch (TosServerException e) { // 操作失败,捕获服务端异常,可以获取到从服务端返回的详细错误信息 System.out.println("getBucketLocation failed"); System.out.println("StatusCode: " + e.getStatusCode()); System.out.println("Code: " + e.getCode()); System.out.println("Message: " + e.getMessage()); System.out.println("RequestID: " + e.getRequestID()); } catch (Throwable t) { // 作为兜底捕获其他异常,一般不会执行到这里 System.out.println("getBucketLocation failed"); System.out.println("unexpected exception, message: " + t.getMessage()); } } }
注意
以下代码展示如何获取桶的跨区域复制进度。
import com.volcengine.tos.TOSV2; import com.volcengine.tos.TOSV2ClientBuilder; import com.volcengine.tos.TosClientException; import com.volcengine.tos.TosServerException; import com.volcengine.tos.model.bucket.GetBucketReplicationInput; import com.volcengine.tos.model.bucket.GetBucketReplicationOutput; import com.volcengine.tos.model.bucket.ReplicationRule; public class GetBucketReplicationByRuleIdExample { public static void main(String[] args) { String endpoint = "your endpoint"; String region = "your region"; String accessKey = System.getenv("TOS_ACCESS_KEY"); String secretKey = System.getenv("TOS_SECRET_KEY"); String bucketName = "bucket-example"; // 不指定 ruleId 查询当前桶所有跨区域复制规则 // 指定 ruleId 时,返回指定规则 String ruleId = "the specific rule id"; TOSV2 tos = new TOSV2ClientBuilder().build(region, endpoint, accessKey, secretKey); try{ GetBucketReplicationInput input = new GetBucketReplicationInput().setBucket(bucketName).setRuleID(ruleId); GetBucketReplicationOutput output = tos.getBucketReplication(input); if (output.getRules() != null && output.getRules().size() > 0) { for (int i = 0; i < output.getRules().size(); i++){ ReplicationRule rule = output.getRules().get(i); // 规则 ID System.out.println("rule id:" + rule.getId()); // 历史复制进度 System.out.println("rule historical object:" + rule.getProgress().getHistoricalObject()); // 增量进度时间点 System.out.println("rule new object:" + rule.getProgress().getNewObject()); } } } catch (TosClientException e) { // 操作失败,捕获客户端异常,一般情况是请求参数错误,此时请求并未发送 System.out.println("getBucketLocation failed"); System.out.println("Message: " + e.getMessage()); if (e.getCause() != null) { e.getCause().printStackTrace(); } } catch (TosServerException e) { // 操作失败,捕获服务端异常,可以获取到从服务端返回的详细错误信息 System.out.println("getBucketLocation failed"); System.out.println("StatusCode: " + e.getStatusCode()); System.out.println("Code: " + e.getCode()); System.out.println("Message: " + e.getMessage()); System.out.println("RequestID: " + e.getRequestID()); } catch (Throwable t) { // 作为兜底捕获其他异常,一般不会执行到这里 System.out.println("getBucketLocation failed"); System.out.println("unexpected exception, message: " + t.getMessage()); } } }
您可以通过 TOS Java SDK 的 deleteBucketReplication
接口删除指定桶配置的跨域资源共享规则。
注意
要删除桶的跨区域复制规则,默认您必须为桶所有者。
以下代码展示如何删除桶的跨区域复制规则。
import com.volcengine.tos.TOSV2; import com.volcengine.tos.TOSV2ClientBuilder; import com.volcengine.tos.TosClientException; import com.volcengine.tos.TosServerException; import com.volcengine.tos.model.bucket.DeleteBucketReplicationInput; import com.volcengine.tos.model.bucket.DeleteBucketReplicationOutput; public class DeleteBucketReplicationExample { public static void main(String[] args) { String endpoint = "your endpoint"; String region = "your region"; String accessKey = System.getenv("TOS_ACCESS_KEY"); String secretKey = System.getenv("TOS_SECRET_KEY"); String bucketName = "bucket-example"; TOSV2 tos = new TOSV2ClientBuilder().build(region, endpoint, accessKey, secretKey); try{ DeleteBucketReplicationInput input = new DeleteBucketReplicationInput().setBucket(bucketName); DeleteBucketReplicationOutput output = tos.deleteBucketReplication(input); System.out.println("deleteBucketReplication succeed, " + output); } catch (TosClientException e) { // 操作失败,捕获客户端异常,一般情况是请求参数错误,此时请求并未发送 System.out.println("deleteBucketReplication failed"); System.out.println("Message: " + e.getMessage()); if (e.getCause() != null) { e.getCause().printStackTrace(); } } catch (TosServerException e) { // 操作失败,捕获服务端异常,可以获取到从服务端返回的详细错误信息 System.out.println("deleteBucketReplication failed"); System.out.println("StatusCode: " + e.getStatusCode()); System.out.println("Code: " + e.getCode()); System.out.println("Message: " + e.getMessage()); System.out.println("RequestID: " + e.getRequestID()); } catch (Throwable t) { // 作为兜底捕获其他异常,一般不会执行到这里 System.out.println("deleteBucketReplication failed"); System.out.println("unexpected exception, message: " + t.getMessage()); } } }
关于设置跨区域复制规则的更多介绍,请参见跨区域复制规则。