データレプリケーションは、オブジェクト、およびオブジェクトに対する作成、更新、削除などの操作を、ソースバケットからターゲットバケットにほぼリアルタイムで非同期にレプリケーションします。Object Storage Service (OSS) は、クロスリージョンレプリケーション (CRR) と同一リージョンレプリケーション (SRR) をサポートしています。
注意事項
このトピックでは、中国 (杭州) リージョンのパブリックエンドポイントを使用します。同じリージョン内の他の Alibaba Cloud サービスから OSS にアクセスするには、内部エンドポイントを使用します。サポートされているリージョンとエンドポイントの詳細については、「リージョンとエンドポイント」をご参照ください。
このトピックでは、アクセス認証情報は環境変数から取得されます。アクセス認証情報の設定方法の詳細については、「アクセス認証情報の設定」をご参照ください。
このトピックでは、OSSClient インスタンスは OSS エンドポイントを使用して作成されます。カスタムドメイン名または Security Token Service (STS) を使用して OSSClient インスタンスを作成する場合は、「一般的なシナリオの設定例」をご参照ください。
データレプリケーションを有効にするには、
oss:PutBucketReplication権限が必要です。データレプリケーションルールを表示するには、oss:GetBucketReplication権限が必要です。レプリケーション可能なターゲットリージョンを表示するには、oss:GetBucketReplicationLocation権限が必要です。データレプリケーションの進捗を表示するには、oss:GetBucketReplicationProgress権限が必要です。データレプリケーションを無効にするには、oss:DeleteBucketReplication権限が必要です。詳細については、「RAM ユーザーへのカスタムアクセスポリシーの付与」をご参照ください。
データレプリケーションの有効化
データレプリケーションを有効にする前に、ソースバケットとターゲットバケットの両方でバージョン管理が有効になっているか、または両方で無効になっていることを確認してください。
次のコードは、データレプリケーションルールを設定する方法を示しています。
import com.aliyun.oss.*;
import com.aliyun.oss.common.auth.*;
import com.aliyun.oss.common.comm.SignVersion;
import com.aliyun.oss.model.AddBucketReplicationRequest;
public class Demo {
public static void main(String[] args) throws Exception {
// この例では、中国 (杭州) リージョンのエンドポイントを使用します。実際のエンドポイントを指定してください。
String endpoint = "https://oss-cn-hangzhou.aliyuncs.com";
// 環境変数からアクセス認証情報を取得します。このコードを実行する前に、OSS_ACCESS_KEY_ID および OSS_ACCESS_KEY_SECRET 環境変数が設定されていることを確認してください。
EnvironmentVariableCredentialsProvider credentialsProvider = CredentialsProviderFactory.newEnvironmentVariableCredentialsProvider();
// バケット名を指定します。例: examplebucket。
String bucketName = "examplebucket";
// データレプリケーションのターゲットバケットを指定します。
String targetBucketName = "yourTargetBucketName";
// ターゲットバケットが配置されているリージョンを指定します。
// クロスリージョンレプリケーションを有効にするには、ソースバケットとターゲットバケットが異なるリージョンにある必要があります。同一リージョンレプリケーションを有効にするには、ソースバケットとターゲットバケットが同じリージョンにある必要があります。
String targetBucketLocation = "yourTargetBucketLocation";
// バケットが配置されているリージョンを指定します。たとえば、バケットが中国 (杭州) リージョンにある場合は、リージョンを cn-hangzhou に設定します。
String region = "cn-hangzhou";
// OSSClient インスタンスを作成します。
// OSSClient インスタンスが不要になったら、shutdown メソッドを呼び出してリソースを解放します。
ClientBuilderConfiguration clientBuilderConfiguration = new ClientBuilderConfiguration();
clientBuilderConfiguration.setSignatureVersion(SignVersion.V4);
OSS ossClient = OSSClientBuilder.create()
.endpoint(endpoint)
.credentialsProvider(credentialsProvider)
.clientConfiguration(clientBuilderConfiguration)
.region(region)
.build();
try {
AddBucketReplicationRequest request = new AddBucketReplicationRequest(bucketName);
request.setTargetBucketName(targetBucketName);
request.setTargetBucketLocation(targetBucketLocation);
// デフォルトでは、既存データがレプリケーションされます。この例では、値を false に設定して既存データのレプリケーションを無効にします。
request.setEnableHistoricalObjectReplication(false);
// OSS がデータレプリケーションに使用することを承認するロールの名前を指定します。Key Management Service (KMS) によるサーバーサイド暗号化 (SSE) を使用してターゲットオブジェクトを暗号化する場合、この要素を指定する必要があります。
//request.setSyncRole("yourRole");
// SSE-KMS 暗号化で作成されたオブジェクトを OSS がレプリケーションするかどうかを指定します。
//request.setSseKmsEncryptedObjectsStatus("Enabled");
// KMS キーの ID を指定します。Status を Enabled に設定した場合は、この要素を指定する必要があります。
//request.setReplicaKmsKeyID("3542abdd-5821-4fb5-a425-90adca***");
//List prefixes = new ArrayList();
//prefixes.add("image/");
//prefixes.add("video");
//prefixes.add("a");
//prefixes.add("A");
// レプリケーションするオブジェクトのプレフィックスを指定します。プレフィックスを指定すると、プレフィックスに一致するオブジェクトのみがターゲットバケットにレプリケーションされます。
//request.setObjectPrefixList(prefixes);
//List actions = new ArrayList();
//actions.add(AddBucketReplicationRequest.ReplicationAction.PUT);
// ソースバケットでのオブジェクトの作成および更新操作をターゲットバケットにレプリケーションします。
//request.setReplicationActionList(actions);
ossClient.addBucketReplication(request);
} catch (OSSException oe) {
System.out.println("Caught an OSSException, which means your request made it to OSS, "
+ "but was rejected with an error response for some reason.");
System.out.println("Error Message:" + oe.getErrorMessage());
System.out.println("Error Code:" + oe.getErrorCode());
System.out.println("Request ID:" + oe.getRequestId());
System.out.println("Host ID:" + oe.getHostId());
} catch (ClientException ce) {
System.out.println("Caught an ClientException, which means the client encountered "
+ "a serious internal problem while trying to communicate with OSS, "
+ "such as not being able to access the network.");
System.out.println("Error Message:" + ce.getMessage());
} finally {
if (ossClient != null) {
ossClient.shutdown();
}
}
}
} データレプリケーションルールの表示
次のコードは、指定されたバケットのデータレプリケーションルールを表示する方法を示しています。
import com.aliyun.oss.*;
import com.aliyun.oss.common.auth.*;
import com.aliyun.oss.common.comm.SignVersion;
import com.aliyun.oss.model.ReplicationRule;
import java.util.List;
public class Demo {
public static void main(String[] args) throws Exception {
// この例では、中国 (杭州) リージョンのエンドポイントを使用します。実際のエンドポイントを指定してください。
String endpoint = "https://oss-cn-hangzhou.aliyuncs.com";
// 環境変数からアクセス認証情報を取得します。このコードを実行する前に、OSS_ACCESS_KEY_ID および OSS_ACCESS_KEY_SECRET 環境変数が設定されていることを確認してください。
EnvironmentVariableCredentialsProvider credentialsProvider = CredentialsProviderFactory.newEnvironmentVariableCredentialsProvider();
// バケット名を指定します。例: examplebucket。
String bucketName = "examplebucket";
// バケットが配置されているリージョンを指定します。たとえば、バケットが中国 (杭州) リージョンにある場合は、リージョンを cn-hangzhou に設定します。
String region = "cn-hangzhou";
// OSSClient インスタンスを作成します。
// OSSClient インスタンスが不要になったら、shutdown メソッドを呼び出してリソースを解放します。
ClientBuilderConfiguration clientBuilderConfiguration = new ClientBuilderConfiguration();
clientBuilderConfiguration.setSignatureVersion(SignVersion.V4);
OSS ossClient = OSSClientBuilder.create()
.endpoint(endpoint)
.credentialsProvider(credentialsProvider)
.clientConfiguration(clientBuilderConfiguration)
.region(region)
.build();
try {
// データレプリケーションルールを表示します。
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());
}
} catch (OSSException oe) {
System.out.println("Caught an OSSException, which means your request made it to OSS, "
+ "but was rejected with an error response for some reason.");
System.out.println("Error Message:" + oe.getErrorMessage());
System.out.println("Error Code:" + oe.getErrorCode());
System.out.println("Request ID:" + oe.getRequestId());
System.out.println("Host ID:" + oe.getHostId());
} catch (ClientException ce) {
System.out.println("Caught an ClientException, which means the client encountered "
+ "a serious internal problem while trying to communicate with OSS, "
+ "such as not being able to access the network.");
System.out.println("Error Message:" + ce.getMessage());
} finally {
if (ossClient != null) {
ossClient.shutdown();
}
}
}
} レプリケーション可能なターゲットリージョンの表示
次のコードは、データをレプリケーションできるターゲットリージョンのリストを取得する方法を示しています。
import com.aliyun.oss.*;
import com.aliyun.oss.common.auth.*;
import com.aliyun.oss.common.comm.SignVersion;
import java.util.List;
public class Demo {
public static void main(String[] args) throws Exception {
// この例では、中国 (杭州) リージョンのエンドポイントを使用します。実際のエンドポイントを指定してください。
String endpoint = "https://oss-cn-hangzhou.aliyuncs.com";
// 環境変数からアクセス認証情報を取得します。このコードを実行する前に、OSS_ACCESS_KEY_ID および OSS_ACCESS_KEY_SECRET 環境変数が設定されていることを確認してください。
EnvironmentVariableCredentialsProvider credentialsProvider = CredentialsProviderFactory.newEnvironmentVariableCredentialsProvider();
// バケット名を指定します。例: examplebucket。
String bucketName = "examplebucket";
// バケットが配置されているリージョンを指定します。たとえば、バケットが中国 (杭州) リージョンにある場合は、リージョンを cn-hangzhou に設定します。
String region = "cn-hangzhou";
// OSSClient インスタンスを作成します。
// OSSClient インスタンスが不要になったら、shutdown メソッドを呼び出してリソースを解放します。
ClientBuilderConfiguration clientBuilderConfiguration = new ClientBuilderConfiguration();
clientBuilderConfiguration.setSignatureVersion(SignVersion.V4);
OSS ossClient = OSSClientBuilder.create()
.endpoint(endpoint)
.credentialsProvider(credentialsProvider)
.clientConfiguration(clientBuilderConfiguration)
.region(region)
.build();
try {
// データをレプリケーションできるターゲットリージョンを表示します。
List<String> locations = ossClient.getBucketReplicationLocation(bucketName);
for (String loc : locations) {
System.out.println(loc);
}
} catch (OSSException oe) {
System.out.println("Caught an OSSException, which means your request made it to OSS, "
+ "but was rejected with an error response for some reason.");
System.out.println("Error Message:" + oe.getErrorMessage());
System.out.println("Error Code:" + oe.getErrorCode());
System.out.println("Request ID:" + oe.getRequestId());
System.out.println("Host ID:" + oe.getHostId());
} catch (ClientException ce) {
System.out.println("Caught an ClientException, which means the client encountered "
+ "a serious internal problem while trying to communicate with OSS, "
+ "such as not being able to access the network.");
System.out.println("Error Message:" + ce.getMessage());
} finally {
if (ossClient != null) {
ossClient.shutdown();
}
}
}
} データレプリケーションの進捗の表示
データレプリケーションの進捗は、既存データのレプリケーションの進捗と増分データのレプリケーションの進捗に分かれています。
既存データのレプリケーションの進捗はパーセンテージで表されます。これは、既存データのレプリケーションが有効になっているバケットにのみ適用されます。
増分データのレプリケーションの進捗は、特定の時点によって示されます。これは、この時点より前に書き込まれたデータがレプリケーションされたことを意味します。
次のコードは、データレプリケーションの進捗を表示する方法を示しています。
import com.aliyun.oss.*;
import com.aliyun.oss.common.auth.*;
import com.aliyun.oss.common.comm.SignVersion;
import com.aliyun.oss.model.BucketReplicationProgress;
public class Demo {
public static void main(String[] args) throws Exception {
// この例では、中国 (杭州) リージョンのエンドポイントを使用します。実際のエンドポイントを指定してください。
String endpoint = "https://oss-cn-hangzhou.aliyuncs.com";
// 環境変数からアクセス認証情報を取得します。このコードを実行する前に、OSS_ACCESS_KEY_ID および OSS_ACCESS_KEY_SECRET 環境変数が設定されていることを確認してください。
EnvironmentVariableCredentialsProvider credentialsProvider = CredentialsProviderFactory.newEnvironmentVariableCredentialsProvider();
// バケット名を指定します。例: examplebucket。
String bucketName = "examplebucket";
// データレプリケーションルールの ID を指定します。
String ruleId = "yourRuleId";
// バケットが配置されているリージョンを指定します。たとえば、バケットが中国 (杭州) リージョンにある場合は、リージョンを cn-hangzhou に設定します。
String region = "cn-hangzhou";
// OSSClient インスタンスを作成します。
// OSSClient インスタンスが不要になったら、shutdown メソッドを呼び出してリソースを解放します。
ClientBuilderConfiguration clientBuilderConfiguration = new ClientBuilderConfiguration();
clientBuilderConfiguration.setSignatureVersion(SignVersion.V4);
OSS ossClient = OSSClientBuilder.create()
.endpoint(endpoint)
.credentialsProvider(credentialsProvider)
.clientConfiguration(clientBuilderConfiguration)
.region(region)
.build();
try {
BucketReplicationProgress process = ossClient.getBucketReplicationProgress(bucketName, ruleId);
System.out.println(process.getReplicationRuleID());
System.out.println(process.isEnableHistoricalObjectReplication());
// 既存データのレプリケーションの進捗を表示します。
System.out.println(process.getHistoricalObjectProgress());
// 増分データのレプリケーションの進捗を表示します。
System.out.println(process.getNewObjectProgress());
} catch (OSSException oe) {
System.out.println("Caught an OSSException, which means your request made it to OSS, "
+ "but was rejected with an error response for some reason.");
System.out.println("Error Message:" + oe.getErrorMessage());
System.out.println("Error Code:" + oe.getErrorCode());
System.out.println("Request ID:" + oe.getRequestId());
System.out.println("Host ID:" + oe.getHostId());
} catch (ClientException ce) {
System.out.println("Caught an ClientException, which means the client encountered "
+ "a serious internal problem while trying to communicate with OSS, "
+ "such as not being able to access the network.");
System.out.println("Error Message:" + ce.getMessage());
} finally {
if (ossClient != null) {
ossClient.shutdown();
}
}
}
} データレプリケーションの無効化
次のコードは、データレプリケーションを無効にする方法を示しています。
import com.aliyun.oss.*;
import com.aliyun.oss.common.auth.*;
import com.aliyun.oss.common.comm.SignVersion;
public class Demo {
public static void main(String[] args) throws Exception {
// この例では、中国 (杭州) リージョンのエンドポイントを使用します。実際のエンドポイントを指定してください。
String endpoint = "https://oss-cn-hangzhou.aliyuncs.com";
// 環境変数からアクセス認証情報を取得します。このコードを実行する前に、OSS_ACCESS_KEY_ID および OSS_ACCESS_KEY_SECRET 環境変数が設定されていることを確認してください。
EnvironmentVariableCredentialsProvider credentialsProvider = CredentialsProviderFactory.newEnvironmentVariableCredentialsProvider();
// バケット名を指定します。例: examplebucket。
String bucketName = "examplebucket";
String ruleId = "yourRuleId";
// バケットが配置されているリージョンを指定します。たとえば、バケットが中国 (杭州) リージョンにある場合は、リージョンを cn-hangzhou に設定します。
String region = "cn-hangzhou";
// OSSClient インスタンスを作成します。
// OSSClient インスタンスが不要になったら、shutdown メソッドを呼び出してリソースを解放します。
ClientBuilderConfiguration clientBuilderConfiguration = new ClientBuilderConfiguration();
clientBuilderConfiguration.setSignatureVersion(SignVersion.V4);
OSS ossClient = OSSClientBuilder.create()
.endpoint(endpoint)
.credentialsProvider(credentialsProvider)
.clientConfiguration(clientBuilderConfiguration)
.region(region)
.build();
try {
// データレプリケーションを無効にします。データレプリケーションを無効にしても、ターゲットバケットにレプリケーションされたオブジェクトは引き続き存在します。ただし、ソースバケット内のオブジェクトへの変更は、ターゲットバケットにはレプリケーションされなくなります。
ossClient.deleteBucketReplication(bucketName, ruleId);
} catch (OSSException oe) {
System.out.println("Caught an OSSException, which means your request made it to OSS, "
+ "but was rejected with an error response for some reason.");
System.out.println("Error Message:" + oe.getErrorMessage());
System.out.println("Error Code:" + oe.getErrorCode());
System.out.println("Request ID:" + oe.getRequestId());
System.out.println("Host ID:" + oe.getHostId());
} catch (ClientException ce) {
System.out.println("Caught an ClientException, which means the client encountered "
+ "a serious internal problem while trying to communicate with OSS, "
+ "such as not being able to access the network.");
System.out.println("Error Message:" + ce.getMessage());
} finally {
if (ossClient != null) {
ossClient.shutdown();
}
}
}
} 関連ドキュメント
データレプリケーションの完全なサンプルコードについては、GitHub の例をご参照ください。
データレプリケーションを有効にする API 操作の詳細については、「PutBucketReplication」をご参照ください。
データレプリケーション設定を表示する API 操作の詳細については、「GetBucketReplication」をご参照ください。
レプリケーション可能なターゲットリージョンを表示する API 操作の詳細については、「GetBucketReplicationLocation」をご参照ください。
データレプリケーションの進捗を表示する API 操作の詳細については、「GetBucketReplicationProgress」をご参照ください。
データレプリケーションを無効にする API 操作の詳細については、「DeleteBucketReplication」をご参照ください。