You need to enable JavaScript to run this app.
导航

管理跨区域复制(C++ SDK)

最近更新时间2024.02.04 18:31:05

首次发布时间2023.03.15 15:05:53

TOS 支持跨区域复制文件,您可以将一个地域的文件复制到不同地域的存储桶中。开启跨区域复制规则后,当您在源存储桶中上传新文件时,TOS 会自动将文件同步至目的桶内。该功能用于满足异地容灾和数据复制的需求。

设置跨区域复制规则

注意事项

  • 开启跨区域复制规则之前的文件不会同步。
  • 跨区域复制采用异步复制机制,根据您的文件大小,需要的时间为几分钟至几小时不等。
  • 一个存储桶最多可创建 100 条跨区域复制规则。
  • 仅当源桶和目标桶的版本控制状态相同时,才能开启跨区域复制。
  • 要设置桶的跨区域复制规则,默认您必须为桶所有者。
  • CRR 任务执行时,当同时设置了 StorgeClass 和 StorageClassInheritDirective 时,以 StorgeClass 的配置为准;
  • 只设置了 StorageClassInheritDirective 时,该配置值为 SOURCE_OBJECT 时与源对象一致, 该配置项值为 DESTINATION_BUCKET 时与目标桶一致。
  • StorgeClass 和 StorageClassInheritDirective 均未设置时,使用默认策略即与目标桶的存储类型保持一致;

示例代码

以下代码用于设置桶 examplebucket 的跨区域复制规则。

#include "TosClientV2.h"
using namespace VolcengineTos;

int main(void){
    // 初始化 TOS 账号信息
    // Your Region 填写 Bucket 所在 Region
    std::string region = "Your Region";
    std::string accessKey = std::getenv("TOS_ACCESS_KEY");
    std::string secretKey = std::getenv("TOS_SECRET_KEY");
    // 填写 Bucket 名称,例如 examplebucket
    std::string bucketName = "examplebucket";
    // 指定数据要复制到的目标 Bucket
    std::string destBucketName = "yourdestbucket";
    // Your DestBucket Region 填写 Bucket 所在的 Region
    std::string destRegion = "Your DestBucket Region";

    // 初始化网络等资源
    InitializeClient();
    // 创建交互的 client
    TosClientV2 client(region, accessKey, secretKey);

    PutBucketReplicationInput input(bucketName);
    ReplicationRule rule;
    // 设置该跨区域复制的 id
    rule.setId("1");
    // 设置开启该规则
    rule.setStatus(StatusType::StatusEnabled);
    // 设置该跨区域复制规则仅复制指定前缀的对象
    rule.setPrefixSet({"prefix1","prefix2"});
    //  指定数据要复制到的目标,设置跨区域复制目的桶和目的桶所在区域
    Destination destination(destBucketName,destRegion);
    // 设置 StorageClass 和 StorageClassInheritDirective
        destination.setStorageClass(StorageClassType::IA);
        destination.setStorageClassInheritDirective(StorageClassInheritDirectiveType::DestinationBucket);

    rule.setDestination(destination);
    // 设置是否开启历史对象的同步
    // rule.setHistoricalObjectReplication(StatusType::StatusEnabled);

    input.setRules({rule});
    input.setRole("ServiceRoleforReplicationAccessTOS");
    auto output = client.putBucketReplication(input);
    if (!output.isSuccess()) {
        // 异常处理
        std::cout << "PutBucketReplication failed." << output.error().String() << std::endl;
        // 释放网络等资源
        CloseClient();
        return -1;
    }
    std::cout << "PutBucketReplication success." << std::endl;

    // 释放网络等资源
    CloseClient();
    return 0;
}

获取跨区域复制规则

注意

要获取桶的跨区域复制规则,默认您必须为桶所有者。

示例代码

以下代码用于获取桶 examplebucket 的跨区域复制规则。

#include "TosClientV2.h"
using namespace VolcengineTos;

int main(void){
    // 初始化 TOS 账号信息
    // Your Region 填写 Bucket 所在 Region
    std::string region = "Your Region";
    std::string accessKey = std::getenv("TOS_ACCESS_KEY");
    std::string secretKey = std::getenv("TOS_SECRET_KEY");
    // 填写 Bucket 名称,例如 examplebucket
    std::string bucketName = "examplebucket";
    // 填写获取的 ruleId
    std::string ruleId = "yourCrrRuleId";

    // 初始化网络等资源
    InitializeClient();
    // 创建交互的 client
    TosClientV2 client(region, accessKey, secretKey);
    GetBucketReplicationInput input(bucketName);
    // 如果需要基于特定的 ruleId 进行 crr 规则查询
    // input.setRuleId(ruleId);
    auto output = client.getBucketReplication(input);
    if (!output.isSuccess()) {
        // 异常处理
        std::cout << "GetBucketReplication failed." << output.error().String() << std::endl;
        // 释放网络等资源
        CloseClient();
        return -1;
    }
    std::cout << "GetBucketReplication success." << std::endl;
    std::cout << "Role:" << output.result().getRole() << std::endl;
    std::cout << "Get Bucket Crr Rule List:" << std::endl;
    for (const auto& rule : output.result().getRules()) {
        std::cout << "id:" << rule.getId() << std::endl;
        std::cout << "status:" << rule.getStringFormatStatus() << std::endl;
        std::cout << "historical object replication:" << rule.getStringFormatHistoricalObjectReplication() << std::endl;
        if (rule.getProgress() != nullptr) {
            // 新写入数据复制进度用新写入数据的时间点表示,代表这个时间点之前的数据已复制完成。
            std::cout << "new object speed of progress:" << rule.getProgress()->getHistoricalObject() << std::endl;
            // 历史数据复制用百分比进度表示
            std::cout << "historical object speed of progress:" << rule.getProgress()->getHistoricalObject()
                      << std::endl;
        }
        for (auto const& prefix : rule.getPrefixSet()) {
            std::cout << "prefix is:" << prefix << std::endl;
        }
        auto destination = rule.getDestination();
        std::cout << "dest bucket is:" << destination.getBucket() << std::endl;
        std::cout << "storage class is:" << destination.getStringFormatStorageClass() << std::endl;
        std::cout << "storage class inherit directive is:" << destination.getStringFormatStorageClassInheritDirective()
                  << std::endl;
        std::cout << "dest location is:" << destination.getLocation() << std::endl;
    }

    // 释放网络等资源
    CloseClient();
    return 0;
}

删除跨区域复制规则

注意

要删除桶的跨区域复制规则,默认您必须为桶所有者。

示例代码

以下代码用于删除桶 examplebucket 的跨区域复制规则。

#include "TosClientV2.h"
using namespace VolcengineTos;

int main(void){
    // 初始化 TOS 账号信息
    // Your Region 填写 Bucket 所在 Region
    std::string region = "Your Region";
    std::string accessKey = std::getenv("TOS_ACCESS_KEY");
    std::string secretKey = std::getenv("TOS_SECRET_KEY");
    // 填写 Bucket 名称,例如 examplebucket
    std::string bucketName = "examplebucket";

    // 初始化网络等资源
    InitializeClient();
    // 创建交互的 client
    TosClientV2 client(region, accessKey, secretKey);
    DeleteBucketReplicationInput input(bucketName);
    auto output = client.deleteBucketReplication(input);
    if (!output.isSuccess()) {
        // 异常处理
        std::cout << "DeleteBucketReplication failed." << output.error().String() << std::endl;
        // 释放网络等资源
        CloseClient();
        return -1;
    }
    std::cout << "DeleteBucketReplication success." << std::endl;
    // 释放网络等资源
    CloseClient();
    return 0;
}

相关文档

关于设置跨区域复制规则的更多介绍,请参见跨区域复制规则