クロスリージョンレプリケーションは、異なるリージョンのバケット間でオブジェクトを自動的かつ非同期的にコピーするため、使用されます。 ソースバケット内のオブジェクトに対する変更操作 (作成、置換、および削除) は、対象のバケットと同期されます。 この機能は、クロスリージョンディザスターリカバリーとデータレプリケーションの要件を満たすため、使用されます。
クロスリージョンレプリケーションの詳細については、「クロスリージョンレプリケーションの管理」をご参照ください。
クロスリージョンレプリケーションの有効化
クロスリージョンレプリケーションを有効にするには、次のコードを実行します。
// This example uses endpoint China East 1 (Hangzhou). Specify the actual endpoint based on your requirements.
String endpoint = "http://oss-cn-hangzhou.aliyuncs.com";
// It is highly risky to log on with AccessKey of an Alibaba Cloud account because the account has permissions on all the APIs in OSS. We recommend that you log on as a RAM user to access APIs or perform routine operations and maintenance. To create a RAM account, log on to https://ram.console.aliyun.com.
String accessKeyId = "<yourAccessKeyId>";
String accessKeySecret = "<yourAccessKeySecret>";
String bucketName = "<yourBucketName>";
// Create an OSSClient instance.
OSSClient ossClient = new OSSClient(endpoint, accessKeyId, accessKeySecret);
AddBucketReplicationRequest request = new AddBucketReplicationRequest(bucketName);
request.setReplicationRuleID("<yourRuleId>");
request.setTargetBucketName("<yourTargetBucketName>");
// This example uses the target endpoint China (Beijing).
request.setTargetBucketLocation("oss-cn-beijing");
// Disable historical data replication. Historical data replication is enabled by default.
request.setEnableHistoricalObjectReplication(false);
ossClient.addBucketReplication(request);
// Close your OSSClient.
ossClient.shutdown();
クロスリージョンレプリケーション構成の表示
次のコードを使用して、バケットを対象としたクロスリージョンレプリケーション構成を表示します。
// This example uses endpoint China (Hangzhou). Specify the actual endpoint based on your requirements.
String endpoint = "http://oss-cn-hangzhou.aliyuncs.com";
// It is highly risky to log on with AccessKey of an Alibaba Cloud account because the account has permissions on all the APIs in OSS. We recommend that you log on as a RAM user to access APIs or perform routine operations and maintenance. To create a RAM account, log on to https://ram.console.aliyun.com.
String accessKeyId = "<yourAccessKeyId>";
String accessKeySecret = "<yourAccessKeySecret>";
String bucketName = "<yourBucketName>";
// Create an OSSClient instance.
OSSClient ossClient = new OSSClient(endpoint, accessKeyId, accessKeySecret);
List<ReplicationRule> rules = ossClient.getBucketReplication(bucketName);
for (ReplicationRule rule : rules) {
System.out.println(rule.getReplicationRuleID());
System.out.println(rule.getTargetBucketLocation());
System.out.println(rule.getTargetBucketName());
}
// Close your OSSClient.
ossClient.shutdown();
クロスリージョンレプリケーション進行状況の表示
クロスリージョンレプリケーションの進行状況には、履歴データの同期とリアルタイムデータの同期の進行状況が含まれます。
- パーセント記号 (%) は、履歴データの同期進行状況を示します。 この機能は、履歴データの同期が有効になっているバケットに対してのみ機能します。
- "時点" (データが最後に書き込まれた時間) は、リアルタイムデータの同期進行状況を示します。 ある時点が表示されている場合、その時点より前に作成されたデータは同期されています。
クロスリージョンレプリケーションの進行状況を表示するには、次のコードを実行します。
// This example uses endpoint China East 1 (Hangzhou). Specify the actual endpoint based on your requirements.
String endpoint = "http://oss-cn-hangzhou.aliyuncs.com";
// It is highly risky to log on with AccessKey of an Alibaba Cloud account because the account has permissions on all the APIs in OSS. We recommend that you log on as a RAM user to access APIs or perform routine operations and maintenance. To create a RAM account, log on to https://ram.console.aliyun.com.
String accessKeyId = "<yourAccessKeyId>";
String accessKeySecret = "<yourAccessKeySecret>";
String bucketName = "<yourBucketName>";
// Create an OSSClient instance.
OSSClient ossClient = new OSSClient(endpoint, accessKeyId, accessKeySecret);
BucketReplicationProgress process = ossClient.getBucketReplicationProgress(bucketName, "<yourRuleId>");
System.out.println(process.getReplicationRuleID());
// Check whether historical data synchronization is enabled.
System.out.println(process.isEnableHistoricalObjectReplication());
// Print historical data synchronization progress.
System.out.println(process.getHistoricalObjectProgress());
// Print real-time data synchronization progress.
System.out.println(process.getNewObjectProgress());
// Close your OSSClient.
ossClient.shutdown();
同期を行うため、対象のリージョンを表示する
次のコードを実行して、バケットを同期する対象のリージョンを取得します。
// This example uses endpoint China East 1 (Hangzhou). Specify the actual endpoint based on your requirements.
String endpoint = "http://oss-cn-hangzhou.aliyuncs.com";
// It is highly risky to log on with AccessKey of an Alibaba Cloud account because the account has permissions on all the APIs in OSS. We recommend that you log on as a RAM user to access APIs or perform routine operations and maintenance. To create a RAM account, log on to https://ram.console.aliyun.com.
String accessKeyId = "<yourAccessKeyId>";
String accessKeySecret = "<yourAccessKeySecret>";
String bucketName = "<yourBucketName>";
// Create an OSSClient instance.
OSSClient ossClient = new OSSClient(endpoint, accessKeyId, accessKeySecret);
List<String> locations = ossClient.getBucketReplicationLocation(bucketName);
for (String loc : locations) {
System.out.println(loc);
}
// Close your OSSClient.
ossClient.shutdown();
クロスリージョンレプリケーションの無効化
クロスリージョンレプリケーションを無効にするには、次のコードを使用します。
// This example uses endpoint China East 1 (Hangzhou). Specify the actual endpoint based on your requirements.
String endpoint = "http://oss-cn-hangzhou.aliyuncs.com";
// It is highly risky to log on with AccessKey of an Alibaba Cloud account because the account has permissions on all the APIs in OSS. We recommend that you log on as a RAM user to access APIs or perform routine operations and maintenance. To create a RAM account, log on to https://ram.console.aliyun.com.
String accessKeyId = "<yourAccessKeyId>";
String accessKeySecret = "<yourAccessKeySecret>";
String bucketName = "<yourBucketName>";
// Create an OSSClient instance.
OSSClient ossClient = new OSSClient(endpoint, accessKeyId, accessKeySecret);
// Disable cross-region replication. After this function is disabled, objects in the target bucket exist and all changes in the source objects cannot be synchronized.
ossClient.deleteBucketReplication(bucketName, "<yourRuleId>");
// Close your OSSClient.
ossClient.shutdown();