同じアカウント内のCRRは、OSS SDK for Java、OSS SDK for Python、およびOSS SDK for Goを使用してのみサポートされます。
Java
import com.aliyun.oss.ClientException;
import com.aliyun.oss.OSS;
import com.aliyun.oss.common.auth.*;
import com.aliyun.oss.OSSClientBuilder;
import com.aliyun.oss.OSSException;
import com.aliyun.oss.model.AddBucketReplicationRequest;
public class Demo {
public static void main(String[] args) throws Exception {
// In this example, the endpoint of the China (Hangzhou) region is used. Specify your actual endpoint.
String endpoint = "https://oss-cn-hangzhou.aliyuncs.com";
// We recommend that you do not save access credentials in the project code. Otherwise, access credentials may be leaked. As a result, the security of all resources in your account is compromised. In this example, access credentials are obtained from environment variables. Before you run the sample code, make sure that the environment variables are configured.
EnvironmentVariableCredentialsProvider credentialsProvider = CredentialsProviderFactory.newEnvironmentVariableCredentialsProvider();
// Specify the name of the source bucket.
String bucketName = "src-bucket";
// Specify the name of the destination bucket. The source and destination buckets must belong to the same Alibaba Cloud account.
String targetBucketName = "dest-bucket";
// Specify the region in which the destination bucket is located. The source and destination buckets must be located in different regions.
String targetBucketLocation = "oss-cn-shanghai";
// Create an OSSClient instance.
OSS ossClient = new OSSClientBuilder().build(endpoint, credentialsProvider);
try {
AddBucketReplicationRequest request = new AddBucketReplicationRequest(bucketName);
request.setTargetBucketName(targetBucketName);
request.setTargetBucketLocation(targetBucketLocation);
// Specify whether to replicate historical data. By default, historical data is replicated. In this example, this parameter is set to false, which indicates that historical data is not replicated.
request.setEnableHistoricalObjectReplication(false);
// Specify the name of the RAM role that you want OSS to use to replicate data. The role must have the permissions to perform CRR on the source bucket and receive replicated objects in the destination bucket.
request.setSyncRole("yourRole");
// Specify whether to replicate the objects that are encrypted by using SSE-KMS.
//request.setSseKmsEncryptedObjectsStatus("Enabled");
// Specify the CMK ID used in SSE-KMS. If Status is set to Enabled, you must specify this parameter.
//request.setReplicaKmsKeyID("3542abdd-5821-4fb5-a425-90adca***");
//List prefixes = new ArrayList();
//prefixes.add("image/");
//prefixes.add("video");
//prefixes.add("a");
//prefixes.add("A");
// Specify the prefixes that are contained in the names of the objects that you want to replicate. After you specify the prefixes, only objects whose names contain one of the prefixes are replicated to the destination bucket.
//request.setObjectPrefixList(prefixes);
//List actions = new ArrayList();
//actions.add(AddBucketReplicationRequest.ReplicationAction.ALL);
// Specify the operations that you want to replicate to the destination bucket. The default value is ALL, which indicates that all operations performed on objects in the source bucket are replicated to the destination bucket.
//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();
}
}
}
}
Python
# -*- coding: utf-8 -*-
import oss2
from oss2.credentials import EnvironmentVariableCredentialsProvider
from oss2.models import ReplicationRule
# Obtain access credentials from environment variables. Before you run the sample code, make sure that the OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET environment variables are configured.
auth = oss2.ProviderAuth(EnvironmentVariableCredentialsProvider())
# Specify the endpoint of the region in which the bucket is located. For example, if the bucket is located in the China (Hangzhou) region, set the endpoint to https://oss-cn-hangzhou.aliyuncs.com.
# Specify the name of the source bucket. Example: src-bucket.
bucket = oss2.Bucket(auth, 'https://oss-cn-hangzhou.aliyuncs.com', 'src-bucket')
replica_config = ReplicationRule(
# Specify the name of the destination bucket. The source and destination buckets must belong to the same Alibaba Cloud account.
target_bucket_name='dest-bucket',
# Specify the region in which the destination bucket is located. The source and destination buckets must be located in different regions.
target_bucket_location='oss-cn-shanghai',
# Specify the name of the RAM role that you want OSS to use to replicate data. The role must have the permissions to perform CRR on the source bucket and receive replicated objects in the destination bucket.
sync_role_name='roleNameTest',
)
# Specify the prefixes that are contained in the names of the objects that you want to replicate. After you specify the prefixes, only objects whose names contain one of the prefixes are replicated to the destination bucket.
# prefix_list = ['prefix1', 'prefix2']
# Specify the data replication rule.
# replica_config = ReplicationRule(
# prefix_list=prefix_list,
# Specify the operations that you want to replicate to the destination bucket. The default value is ALL, which indicates that all operations performed on objects in the source bucket are synchronized to the destination bucket.
# action_list=[ReplicationRule.ALL],
# Specify the name of the destination bucket. The source and destination buckets must belong to the same Alibaba Cloud account.
# target_bucket_name='dest-bucket',
# Specify the region in which the destination bucket is located. The source and destination buckets must be located in different regions.
# target_bucket_location='yourTargetBucketLocation',
# Specify whether to replicate historical data. By default, historical data is replicated. In this example, this parameter is set to False, which indicates that historical data is not replicated.
# is_enable_historical_object_replication=False,
# Specify the link that you want to use to transfer data during data replication.
# target_transfer_type='oss_acc',
#)
# Enable data replication.
bucket.put_bucket_replication(replica_config)
Go
package main
import (
"encoding/xml"
"fmt"
"github.com/aliyun/aliyun-oss-go-sdk/oss"
"os"
)
func HandleError(err error) {
fmt.Println("Error:", err)
os.Exit(-1)
}
// Enable data replication.
func main() {
// Obtain access credentials from environment variables. Before you run the sample code, make sure that the OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET environment variables are configured.
provider, err := oss.NewEnvironmentVariableCredentialsProvider()
if err != nil {
fmt.Println("Error:", err)
os.Exit(-1)
}
// Create an OSSClient instance.
// Specify the endpoint of the region in which the bucket is located. For example, if the bucket is located in the China (Hangzhou) region, set the endpoint to https://oss-cn-hangzhou.aliyuncs.com. Specify your actual endpoint.
client, err := oss.New("yourEndpoint", "", "", oss.SetCredentialsProvider(&provider))
if err != nil {
fmt.Println("Error:", err)
os.Exit(-1)
}
// Specify the name of the source bucket.
srcbucketName := "yourSrcBucket"
// Specify the name of the destination bucket.
destBucketName := "yourDestBucket"
// Specify that objects whose names contain one of the following prefixes are replicated to the destination bucket: prefix_1 and prefix_2.
// To replicate all objects from the source bucket to the destination bucket, do not configure prefixes.
prefix1 := "prefix_1"
prefix2 := "prefix_2"
// Specify the CMK ID used in SSE-KMS. If Status is set to Enabled, you must specify this parameter.
keyId := "c4d49f85-ee30-426b-a5ed-95e9xxxx"
// Specify whether to replicate the objects that are encrypted by using SSE-KMS.
source := "Enabled"
prefixSet := oss.ReplicationRulePrefix{Prefix: []*string{&prefix1, &prefix2}}
// Enable the RTC feature.
enabled := "enabled"
reqReplication := oss.PutBucketReplication{
Rule: []oss.ReplicationRule{
{
PrefixSet: &prefixSet,
// Specify the operations that you want to replicate to the destination bucket. The default value is ALL, which indicates that all operations performed on objects in the source bucket are synchronized to the destination bucket.
Action: "ALL",
RTC: &enabled,
Destination: &oss.ReplicationRuleDestination{
Bucket: destBucketName,
// Specify the region in which the destination bucket is located. The source bucket and destination bucket must be located in different regions.
Location: "oss-cn-shanghai",
// Specify the link that you want to use to transfer data during data replication. In this example, this parameter is set to oss_acc, which indicates that the link used for transfer acceleration is used.
TransferType: "oss_acc",
},
// Specify whether to replicate historical data. By default, historical data is replicated. In this example, this parameter is set to disabled, which indicates that historical data is not replicated.
HistoricalObjectReplication: "disabled",
// Specify the name of the RAM role that you want OSS to use to replicate data. The role must have the permissions to perform CRR on the source bucket and receive replicated objects in the destination bucket.
SyncRole: "yourRole",
EncryptionConfiguration: &keyId,
SourceSelectionCriteria: &source,
},
},
}
xmlBody, err := xml.Marshal(reqReplication)
if err != nil {
HandleError(err)
}
err = client.PutBucketReplication(srcbucketName, string(xmlBody))
if err != nil {
HandleError(err)
}
fmt.Println("Put Bucket Replication Success!")
}