详情页标题前

阿里云对象存储OSSJava数据复制-云淘科技

详情页1

数据复制是以异步(近实时)方式将源Bucket中的文件(Object)以及对Object的创建、更新和删除等操作自动复制到目标Bucket。OSS支持跨区域复制(Cross-Region Replication)和同区域复制(Same-Region Replication)。

注意事项

  • 本文以华东1(杭州)外网Endpoint为例。如果您希望通过与OSS同地域的其他阿里云产品访问OSS,请使用内网Endpoint。关于OSS支持的Region与Endpoint的对应关系,请参见访问域名和数据中心。
  • 本文以从环境变量读取访问凭证为例。如何配置访问凭证,请参见Java配置访问凭证。

  • 本文以OSS域名新建OSSClient为例。如果您希望通过自定义域名、STS等方式新建OSSClient,请参见新建OSSClient。
  • 要开启数据复制,您必须有oss:PutBucketReplication;要查看数据复制规则,您必须有oss:GetBucketReplication权限;要查看可复制的目标地域,您必须有oss:GetBucketReplicationLocation权限;要查看数据复制进度,您必须有oss:GetBucketReplicationProgress权限;要关闭数据复制,您必须有oss:DeleteBucketReplication权限。具体操作,请参见为RAM用户授权自定义的权限策略。

开启数据复制

重要

开启数据复制前,请确保源存储空间与目标存储空间同时处于非版本化或已启用版本控制状态。

以下代码用于配置数据复制规则。

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 {
        // Endpoint以华东1(杭州)为例,其它Region请按实际情况填写。
        String endpoint = "https://oss-cn-hangzhou.aliyuncs.com";
        // 从环境变量中获取访问凭证。运行本代码示例之前,请确保已设置环境变量OSS_ACCESS_KEY_ID和OSS_ACCESS_KEY_SECRET。
        EnvironmentVariableCredentialsProvider credentialsProvider = CredentialsProviderFactory.newEnvironmentVariableCredentialsProvider();
        // 填写Bucket名称,例如examplebucket。
        String bucketName = "examplebucket";
        // 指定数据要复制到的目标Bucket。
        String targetBucketName = "yourTargetBucketName";
        // 指定目标Bucket所在地域。
        // 如果您希望开启跨区域复制,则源Bucket与目标Bucket必须处于不同的地域。如果您希望开启同地域复制,则源Bucket与目标Bucket必须处于相同的地域。
        String targetBucketLocation = "yourTargetBucketLocation";

        // 创建OSSClient实例。
        OSS ossClient = new OSSClientBuilder().build(endpoint, credentialsProvider);

        try {
            AddBucketReplicationRequest request = new AddBucketReplicationRequest(bucketName);

            request.setTargetBucketName(targetBucketName);
            request.setTargetBucketLocation(targetBucketLocation);
            // 默认复制历史数据。此处设置为false,表示禁止复制历史数据。
            request.setEnableHistoricalObjectReplication(false);
            // 指定授权OSS进行数据复制的角色名称。如果指定使用SSE-KMS加密目标对象,则必须指定该元素。
            //request.setSyncRole("yourRole");
            // 指定OSS是否复制通过SSE-KMS加密创建的对象。
            //request.setSseKmsEncryptedObjectsStatus("Enabled");
            // 指定SSE-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");
            // 指定待复制Object的前缀Prefix。指定Prefix后,只有匹配该Prefix的Object才会复制到目标Bucket。
            //request.setObjectPrefixList(prefixes);
            //List actions = new ArrayList();
            //actions.add(AddBucketReplicationRequest.ReplicationAction.ALL);
            // 指定可以被复制到目标Bucket的操作。默认值为ALL,表示源Bucket的所有操作都会复制到目标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();
            }
        }
    }
}        

查看数据复制规则

以下代码用于查看指定Bucket的数据复制规则。

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.ReplicationRule;
import java.util.List;

public class Demo {

    public static void main(String[] args) throws Exception {
        // Endpoint以华东1(杭州)为例,其它Region请按实际情况填写。
        String endpoint = "https://oss-cn-hangzhou.aliyuncs.com";
        // 从环境变量中获取访问凭证。运行本代码示例之前,请确保已设置环境变量OSS_ACCESS_KEY_ID和OSS_ACCESS_KEY_SECRET。
        EnvironmentVariableCredentialsProvider credentialsProvider = CredentialsProviderFactory.newEnvironmentVariableCredentialsProvider();
        // 填写Bucket名称,例如examplebucket。
        String bucketName = "examplebucket";

        // 创建OSSClient实例。
        OSS ossClient = new OSSClientBuilder().build(endpoint, credentialsProvider);

        try {
            // 查看数据复制规则。
            List 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.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.ReplicationRule;
import java.util.List;

public class Demo {

    public static void main(String[] args) throws Exception {
        // Endpoint以华东1(杭州)为例,其它Region请按实际情况填写。
        String endpoint = "https://oss-cn-hangzhou.aliyuncs.com";
        // 从环境变量中获取访问凭证。运行本代码示例之前,请确保已设置环境变量OSS_ACCESS_KEY_ID和OSS_ACCESS_KEY_SECRET。
        EnvironmentVariableCredentialsProvider credentialsProvider = CredentialsProviderFactory.newEnvironmentVariableCredentialsProvider();
        // 填写Bucket名称,例如examplebucket。
        String bucketName = "examplebucket";

        // 创建OSSClient实例。
        OSS ossClient = new OSSClientBuilder().build(endpoint, credentialsProvider);

        try {
            // 查看可复制的目标地域。
            List 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.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.BucketReplicationProgress;

public class Demo {

    public static void main(String[] args) throws Exception {
        // Endpoint以华东1(杭州)为例,其它Region请按实际情况填写。
        String endpoint = "https://oss-cn-hangzhou.aliyuncs.com";
        // 从环境变量中获取访问凭证。运行本代码示例之前,请确保已设置环境变量OSS_ACCESS_KEY_ID和OSS_ACCESS_KEY_SECRET。
        EnvironmentVariableCredentialsProvider credentialsProvider = CredentialsProviderFactory.newEnvironmentVariableCredentialsProvider();
        // 填写Bucket名称,例如examplebucket。
        String bucketName = "examplebucket";
        // 填写数据复制规则ID。
        String ruleId = "yourRuleId";

        // 创建OSSClient实例。
        OSS ossClient = new OSSClientBuilder().build(endpoint, credentialsProvider);

        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.ClientException;
import com.aliyun.oss.OSS;
import com.aliyun.oss.common.auth.*;
import com.aliyun.oss.OSSClientBuilder;
import com.aliyun.oss.OSSException;

public class Demo {

    public static void main(String[] args) throws Exception {
        // Endpoint以华东1(杭州)为例,其它Region请按实际情况填写。
        String endpoint = "https://oss-cn-hangzhou.aliyuncs.com";
        // 从环境变量中获取访问凭证。运行本代码示例之前,请确保已设置环境变量OSS_ACCESS_KEY_ID和OSS_ACCESS_KEY_SECRET。
        EnvironmentVariableCredentialsProvider credentialsProvider = CredentialsProviderFactory.newEnvironmentVariableCredentialsProvider();
        // 填写Bucket名称,例如examplebucket。
        String bucketName = "examplebucket";
        String ruleId = "yourRuleId";

        // 创建OSSClient实例。
        OSS ossClient = new OSSClientBuilder().build(endpoint, credentialsProvider);

        try {
            // 关闭数据复制。关闭后,已复制到目标Bucket内的文件依然存在,但是不再将源Bucket内文件的所有改动复制到目标Bucket。
            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。

内容没看懂? 不太想学习?想快速解决? 有偿解决: 联系专家

阿里云企业补贴进行中: 马上申请

腾讯云限时活动1折起,即将结束: 马上收藏

同尘科技为腾讯云授权服务中心。

购买腾讯云产品享受折上折,更有现金返利:同意关联,立享优惠

转转请注明出处:https://www.yunxiaoer.com/158025.html

(0)
上一篇 2023年12月10日
下一篇 2023年12月10日
详情页2

相关推荐

  • 阿里云RDS数据库数据迁移方案概览-云淘科技

    RDS提供了多种数据迁移方案,可满足不同上云或迁云的业务需求,使您可以在不影响业务的情况下将数据在其他数据库与阿里云云数据库RDS之间平滑迁移。 使用场景 相关操作 将自建数据库迁移到云数据库 使用DTS将自建PostgreSQL迁移到RDS PostgreSQL 使用pg_dump和pg_restore将自建PostgreSQL迁移到RDS Postgre…

    阿里云数据库 2023年12月9日
  • 阿里云对象存储OSSOSS访问域名使用规则-云淘科技

    OSS会为每一个存储空间(Bucket)分配默认的访问域名,本文介绍OSS访问域名的构成规则及使用方式。 OSS域名构成规则 针对OSS的网络请求,除了GetService(ListBuckets)以及DescribeRegions API以外,其他所有请求的域名都是由带有指定Bucket信息的三级域名组成的。 访问域名结构为BucketName.Endpo…

    阿里云对象存储 2023年12月10日
  • 阿里云大数据开发治理平台 DataWorksPolarDB数据源-云淘科技

    PolarDB数据源为您提供读取和写入PolarDB双向通道的功能,您可以通过向导模式和脚本模式配置同步任务。 使用限制 离线读写 支持读取视图表。 实时读 来源数据源为阿里云PolarDB MySQL时,您需要开启Binlog。阿里云PolarDB MySQL是一款完全兼容MySQL的云原生数据库,默认使用了更高级别的物理日志代替Binlog,但为了更好地…

  • 阿里云对象存储OSS管理目录-云淘科技

    与传统文件系统中的层级结构不同,OSS内部使用扁平结构存储数据。即所有数据均以对象(Object)的形式保存在存储空间(Bucket)中。为方便管理,OSS控制台将所有以正斜线(/)结尾的对象显示为目录,实现类似于目录的基本功能。您可以通过目录的层次来组织文件,实现分组并简化权限管理。 实现原理 OSS通过创建大小为0字节的对象来模拟目录的概念,以便实现类似…

    阿里云对象存储 2023年12月10日
  • 信息流广告,信息流部分建议宽度830px,只针对默认列表样式,顺序随机
  • 阿里云日志服务SLS对云监控指标进行智能巡检-云淘科技

    云监控(CloudMonitor)是一项针对阿里云资源和互联网应用进行监控的服务,提供丰富的监控指标。日志服务支持接入云监控数据,并支持您使用智能巡检功能对云监控数据进行智能的异常巡检。 前提条件 已导入云监控数据到目标Metricstore中(例如Project:monitor,Metricstore:cloud-monitor-metrics)。具体操作…

    2023年12月10日

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信
本站为广大会员提供阿里云、腾讯云、华为云、百度云等一线大厂的购买,续费优惠,保证底价,买贵退差。