有奖:语音产品征文挑战赛等你来> HOT

简介

本文档提供关于跨地域复制的 API 概览以及 SDK 示例代码。
API
操作名
操作描述
设置跨地域复制
设置存储桶的跨地域复制规则
查询跨地域复制
查询存储桶的跨地域复制规则
删除跨地域复制
删除存储桶的跨地域复制规则

SDK API 参考

SDK 所有接口的具体参数与方法说明,请参考 SDK API 参考

设置跨地域复制

功能说明

设置指定存储桶的跨地域复制规则。

示例代码

// 存储桶名称,由bucketname-appid 组成,appid必须填入,可以在COS控制台查看存储桶名称。 https://console.cloud.tencent.com/cos5/bucket
String bucket = "examplebucket-1250000000";
PutBucketReplicationRequest putBucketReplicationRequest =
new PutBucketReplicationRequest(bucket);

String ownerUin = "100000000001"; //发起者身份标示:OwnerUin
String subUin = "100000000001"; //发起者身份标示:SubUin
putBucketReplicationRequest.setReplicationConfigurationWithRole(ownerUin,
subUin);

PutBucketReplicationRequest.RuleStruct ruleStruct =
new PutBucketReplicationRequest.RuleStruct();
//用来标注具体 Rule 的名称
ruleStruct.id = "replication_01";
//标识 Rule 是否生效。true:生效;false:不生效
ruleStruct.isEnable = true;
//目标存储桶地域信息
ruleStruct.region = "ap-beijing";
// 目标存储桶
ruleStruct.bucket = "destinationbucket-1250000000";
//前缀匹配策略
ruleStruct.prefix = "dir/";
putBucketReplicationRequest.setReplicationConfigurationWithRule(ruleStruct);

cosXmlService.putBucketReplicationAsync(putBucketReplicationRequest,
new CosXmlResultListener() {
@Override
public void onSuccess(CosXmlRequest request, CosXmlResult result) {
PutBucketReplicationResult putBucketReplicationResult =
(PutBucketReplicationResult) result;
}

// 如果您使用 kotlin 语言来调用,请注意回调方法中的异常是可空的,否则不会回调 onFail 方法,即:
// clientException 的类型为 CosXmlClientException?,serviceException 的类型为 CosXmlServiceException?
@Override
public void onFail(CosXmlRequest cosXmlRequest,
@Nullable CosXmlClientException clientException,
@Nullable CosXmlServiceException serviceException) {
if (clientException != null) {
clientException.printStackTrace();
} else {
serviceException.printStackTrace();
}
}
});
说明
更多完整示例,请前往 GitHub 查看。

查询跨地域复制

功能说明

查询指定存储桶的跨地域复制规则。

示例代码

// 存储桶名称,由bucketname-appid 组成,appid必须填入,可以在COS控制台查看存储桶名称。 https://console.cloud.tencent.com/cos5/bucket
String bucket = "examplebucket-1250000000";
GetBucketReplicationRequest getBucketReplicationRequest =
new GetBucketReplicationRequest(bucket);

cosXmlService.getBucketReplicationAsync(getBucketReplicationRequest,
new CosXmlResultListener() {
@Override
public void onSuccess(CosXmlRequest request, CosXmlResult result) {
GetBucketReplicationResult getBucketReplicationResult =
(GetBucketReplicationResult) result;
}

// 如果您使用 kotlin 语言来调用,请注意回调方法中的异常是可空的,否则不会回调 onFail 方法,即:
// clientException 的类型为 CosXmlClientException?,serviceException 的类型为 CosXmlServiceException?
@Override
public void onFail(CosXmlRequest cosXmlRequest,
@Nullable CosXmlClientException clientException,
@Nullable CosXmlServiceException serviceException) {
if (clientException != null) {
clientException.printStackTrace();
} else {
serviceException.printStackTrace();
}
}
});
说明
更多完整示例,请前往 GitHub 查看。

删除跨地域复制

功能说明

删除指定存储桶的跨地域复制规则。

示例代码

// 存储桶名称,由bucketname-appid 组成,appid必须填入,可以在COS控制台查看存储桶名称。 https://console.cloud.tencent.com/cos5/bucket
String bucket = "examplebucket-1250000000";
DeleteBucketReplicationRequest deleteBucketReplicationRequest =
new DeleteBucketReplicationRequest(bucket);

cosXmlService.deleteBucketReplicationAsync(deleteBucketReplicationRequest,
new CosXmlResultListener() {
@Override
public void onSuccess(CosXmlRequest request, CosXmlResult result) {
DeleteBucketReplicationResult deleteBucketReplicationResult =
(DeleteBucketReplicationResult) result;
}

// 如果您使用 kotlin 语言来调用,请注意回调方法中的异常是可空的,否则不会回调 onFail 方法,即:
// clientException 的类型为 CosXmlClientException?,serviceException 的类型为 CosXmlServiceException?
@Override
public void onFail(CosXmlRequest cosXmlRequest,
@Nullable CosXmlClientException clientException,
@Nullable CosXmlServiceException serviceException) {
if (clientException != null) {
clientException.printStackTrace();
} else {
serviceException.printStackTrace();
}
}
});
说明
更多完整示例,请前往 GitHub 查看。