任务

更新时间:

本文介绍如何使用SDK调用任务相关的方法。

创建任务

以下示例代码用于创建任务。

package sample;

import com.aliyun.hcs_mgw20240626.Client;
import com.aliyun.hcs_mgw20240626.models.*;
import java.util.List;

public class Demo {
   /** 强烈建议不要把AccessKey ID和AccessKey Secret保存到工程代码里,否则可能导致AccessKey泄露,威胁您账号下所有资源的安全。*/
   static String accessKeyId = System.getenv("ALIBABA_CLOUD_ACCESS_KEY_ID");
   static String accessKeySecret = System.getenv("ALIBABA_CLOUD_ACCESS_KEY_SECRET");
   /** 填写主账号ID。*/
   static String userId = "11470***876***55";

   public static void main(String[] args) {
      // 填写任务名称。
      String jobName = "examplejob";
      // 填写源端数据地址名称。
      String srcAddress = "examplesrcaddress";
      // 填写目的端数据地址名称。
      String destAddress = "exampledestaddress";

      try {
         com.aliyun.teaopenapi.models.Config config = new com.aliyun.teaopenapi.models.Config();
         // 这里以北京区域为例。
         config.setEndpoint("cn-beijing.mgw.aliyuncs.com");
         config.setRegionId(accessKeyId);
         config.setAccessKeyId(accessKeyId);
         config.setAccessKeySecret(accessKeySecret);
         Client client = new Client(config);
         CreateJobInfo info = new CreateJobInfo();
         info.name = jobName;
         info.srcAddress = srcAddress;
         info.destAddress = destAddress;
         /**
          overwriteMode和transferMode需要组合使用,具体组合含义如下
          always,all 全覆盖
          always,lastmodified 根据文件最后修改时间覆盖
          never,all 不覆盖
        */
         /* 文件覆盖方式, 可能得值为 1.never  2.always */
         info.overwriteMode = "always";
         /* 文件传输的方式, 可能得值为 1.changed 2.all 3.lastmodified */
         info.transferMode = "lastmodified";

         // 如果maxImportTaskQps值为0或者不设置,会被设置为默认值;MaxBandWidth值为0或者不设置,会被设置为默认值,maxBandWidth值单位为bits。
         ImportQos importQos = new ImportQos();
         // maxBandWidth,maxImportTaskQps 根据实际需求值填写。
         importQos.maxBandWidth = 1073741824L;
         importQos.maxImportTaskQps = 1000L;
         info.setImportQos(importQos);

         // 配置调度规则,具体参数含义请参看API文档。
         ScheduleRule scheduleRule = new ScheduleRule();
         // maxScheduleCount,startCronExpression,suspendCronExpression 根据实际需求值填写。
         scheduleRule.maxScheduleCount = 2L;
         scheduleRule.startCronExpression = "0 0 10 * * ?";
         scheduleRule.suspendCronExpression = "0 0 14 * * ?";
         info.setScheduleRule(scheduleRule);

         // 配置过滤规则,具体参数含义请参看API文档。
         FilterRule filterRule = new FilterRule();
         // 文件过滤器,根据实际需求值填写。
         KeyFilters keyFilters = new KeyFilters();
         KeyFilterItem includekeyFilterItem = new KeyFilterItem();
         List<String> includeRegex = new LinkedList<>();
         includeRegex.add(".*.jpg");
         includeRegex.add(".*.jpeg");
         includekeyFilterItem.setRegex(includeRegex);
         KeyFilterItem excludekeyFilterItem = new KeyFilterItem();
         List<String> excludeRegex = new LinkedList<>();
         excludeRegex.add(".*.txt");
         excludeRegex.add(".*.js");
         excludekeyFilterItem.setRegex(excludeRegex);
         keyFilters.setIncludes(includekeyFilterItem);
         keyFilters.setExcludes(excludekeyFilterItem);
         filterRule.setKeyFilters(keyFilters);

         // 时间过滤器, 时间格式遵循UTC时间格式,根据实际需求值填写。
         LastModifiedFilters lastModifiedFilters = new LastModifiedFilters();
         LastModifyFilterItem includeLastModifyFilterItem = new LastModifyFilterItem();
         List<TimeFilter> includeRegexs = new LinkedList<>();
         TimeFilter includeTimeFilter = new TimeFilter();
         includeTimeFilter.startTime = "2006-01-01T00:00:00Z";
         includeTimeFilter.endTime = "2006-12-31T23:59:59Z";
         includeRegexs.add(includeTimeFilter);
         includeLastModifyFilterItem.setTimeFilter(includeRegexs);
         LastModifyFilterItem excludeLastModifyFilterItem = new LastModifyFilterItem();
         List<TimeFilter> excludeRegexs = new LinkedList<>();
         TimeFilter excludeTimeFilter = new TimeFilter();
         excludeTimeFilter.startTime = "2008-01-01T00:00:00Z";
         excludeTimeFilter.endTime = "2008-12-31T23:59:59Z";
         excludeRegexs.add(excludeTimeFilter);
         excludeLastModifyFilterItem.setTimeFilter(excludeRegexs);
         lastModifiedFilters.setIncludes(includeLastModifyFilterItem);
         lastModifiedFilters.setExcludes(excludeLastModifyFilterItem);
         filterRule.setLastModifiedFilters(lastModifiedFilters);
         info.setFilterRule(filterRule);

         CreateJobRequest request = new CreateJobRequest();
         request.setImportJob(info);
         client.createJob(userId, request);
      } catch (Exception e) {
         e.printStackTrace();
      }
   }
}

更新任务

说明

更新任务操作可分为更新任务状态、更新任务限流等,具体可参见 API 任务相关文档。

以下示例代码用于更新任务状态。

package sample;

import com.aliyun.hcs_mgw20240626.Client;
import com.aliyun.hcs_mgw20240626.models.*;

public class Demo {
	/** 强烈建议不要把AccessKey ID和AccessKey Secret保存到工程代码里,否则可能导致AccessKey泄露,威胁您账号下所有资源的安全。*/
	static String accessKeyId = System.getenv("ALIBABA_CLOUD_ACCESS_KEY_ID");
	static String accessKeySecret = System.getenv("ALIBABA_CLOUD_ACCESS_KEY_SECRET");
	/** 填写主账号ID。*/
	static String userId = "11470***876***55";

	public static void main(String[] args) {
		// 填写任务名称。
		String jobName = "examplejob";
		try {
			com.aliyun.teaopenapi.models.Config config = new com.aliyun.teaopenapi.models.Config();
			// 这里以北京区域为例。
			config.setEndpoint("cn-beijing.mgw.aliyuncs.com");
			config.setRegionId(accessKeyId);
			config.setAccessKeyId(accessKeyId);
			config.setAccessKeySecret(accessKeySecret);
			Client client = new Client(config);
			UpdateJobRequest updateJobRequest = new UpdateJobRequest();
			UpdateJobInfo updateJobInfo = new UpdateJobInfo();
			updateJobInfo.setStatus("IMPORT_JOB_LAUNCHING");
			updateJobRequest.setImportJob(updateJobInfo);
			client.updateJob(userId, jobName, updateJobRequest);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}

以下示例代码用于更新任务限流。

package sample;

import com.aliyun.hcs_mgw20240626.Client;
import com.aliyun.hcs_mgw20240626.models.*;

public class Demo {
	/** 强烈建议不要把AccessKey ID和AccessKey Secret保存到工程代码里,否则可能导致AccessKey泄露,威胁您账号下所有资源的安全。*/
	static String accessKeyId = System.getenv("ALIBABA_CLOUD_ACCESS_KEY_ID");
	static String accessKeySecret = System.getenv("ALIBABA_CLOUD_ACCESS_KEY_SECRET");
	/** 填写主账号ID。*/
	static String userId = "11470***876***55";

	public static void main(String[] args) {
		// 填写任务名称。
		String jobName = "examplejob";
		try {
			com.aliyun.teaopenapi.models.Config config = new com.aliyun.teaopenapi.models.Config();
			// 这里以北京区域为例。
			config.setEndpoint("cn-beijing.mgw.aliyuncs.com");
			config.setRegionId(accessKeyId);
			config.setAccessKeyId(accessKeyId);
			config.setAccessKeySecret(accessKeySecret);
			Client client = new Client(config);
			UpdateJobRequest updateJobRequest = new UpdateJobRequest();
			UpdateJobInfo updateJobInfo = new UpdateJobInfo();
			// maxBandWidth、maxImportTaskQps请根据需求填写。
			long maxBandWidth = 1610612736L;
			long maxImportTaskQps = 1500L;
			ImportQos qos = new ImportQos();
			qos.setMaxBandWidth(maxBandWidth);
			updateJobInfo.setImportQos(qos);
			qos.setMaxImportTaskQps(maxImportTaskQps);
			updateJobRequest.setImportJob(updateJobInfo);
			client.updateJob(userId, jobName, updateJobRequest);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}

获取任务详情

以下示例代码用于获取指定任务的详情(通过任务名称)。

package sample;

import com.aliyun.hcs_mgw20240626.Client;
import com.aliyun.hcs_mgw20240626.models.*;
import com.google.gson.Gson;

public class Demo {
   /** 强烈建议不要把AccessKey ID和AccessKey Secret保存到工程代码里,否则可能导致AccessKey泄露,威胁您账号下所有资源的安全。*/
   static String accessKeyId = System.getenv("ALIBABA_CLOUD_ACCESS_KEY_ID");
   static String accessKeySecret = System.getenv("ALIBABA_CLOUD_ACCESS_KEY_SECRET");
   /** 填写主账号ID。*/
   static String userId = "11470***876***55";

   public static void main(String[] args) {
      // 填写任务名称。
      String jobName = "examplejob";
      try {
         com.aliyun.teaopenapi.models.Config config = new com.aliyun.teaopenapi.models.Config();
         // 这里以北京区域为例。
         config.setEndpoint("cn-beijing.mgw.aliyuncs.com");
         config.setRegionId(accessKeyId);
         config.setAccessKeyId(accessKeyId);
         config.setAccessKeySecret(accessKeySecret);
         Client client = new Client(config);
         GetJobRequest request = new GetJobRequest();
         GetJobResponse resp = client.getJob(userId, jobName, request);
         System.out.println(new Gson().toJson(resp.getBody().getImportJob()));
      } catch (Exception e) {
         e.printStackTrace();
      }
   }
}

以下示例代码用于获取指定任务的详情(通过任务ID)。

package sample;

import com.aliyun.hcs_mgw20240626.Client;
import com.aliyun.hcs_mgw20240626.models.*;
import com.google.gson.Gson;

public class Demo {
	/** 强烈建议不要把AccessKey ID和AccessKey Secret保存到工程代码里,否则可能导致AccessKey泄露,威胁您账号下所有资源的安全。*/
	static String accessKeyId = System.getenv("ALIBABA_CLOUD_ACCESS_KEY_ID");
	static String accessKeySecret = System.getenv("ALIBABA_CLOUD_ACCESS_KEY_SECRET");
	/** 填写主账号ID。*/
	static String userId = "11470***876***55";

	public static void main(String[] args) {
		// 填写任务ID。
		String version = "b4155550-****-4371-****-9c73373480211";
		try {
			com.aliyun.teaopenapi.models.Config config = new com.aliyun.teaopenapi.models.Config();
			// 这里以北京区域为例。
			config.setEndpoint("cn-beijing.mgw.aliyuncs.com");
			config.setRegionId(accessKeyId);
			config.setAccessKeyId(accessKeyId);
			config.setAccessKeySecret(accessKeySecret);
			Client client = new Client(config);
			GetJobRequest request = new GetJobRequest();
			GetJobResponse resp = client.getJob(userId, version, request);
			System.out.println(new Gson().toJson(resp.body.getImportJob()));
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}

正常返回示例

{
  "ImportJob": {
    "Name": "test_name",
    "SrcAddress": "test_src_address",
    "DestAddress": "test_dest_address",
    "Status": "IMPORT_JOB_DOING",
    "EnableMultiVersioning": false,
    "CreateTime": "2024-05-01T12:00:00.000Z",
    "ModifyTime": "2024-05-01T12:00:00.000Z",
    "Version": "test_id",
    "Audit": {
      "LogMode": "off"
    },
    "OverwriteMode": "always",
    "TransferMode": "all",
    "Tags": "K1:V1,K2:V2",
    "ParentName": "test_parent_name",
    "ParentVersion": "7db93837-a5ee-4e3a-b3c8-800e7947dabc",
    "ConvertSymlinkTarget": false,
    "CreateReport": false,
    "Owner": "test_owner",
    "FilterRule": {
      "KeyFilters": {
        "Includes": {
          "Regex": [
            ".*\\.jpg$"
          ]
        },
        "Excludes": {
          "Regex": [
            ".*\\.jpg$"
          ]
        }
      },
      "LastModifiedFilters": {
        "Includes": {
          "TimeFilter": [
            {
              "StartTime": "2006-01-01T00:00:00Z",
              "EndTime": "2006-12-31T59:59:59Z"
            }
          ]
        },
        "Excludes": {
          "TimeFilter": [
            {
              "StartTime": "2006-01-01T00:00:00Z",
              "EndTime": "2006-12-31T59:59:59Z"
            }
          ]
        }
      },
      "FileTypeFilters": {
        "ExcludeSymlink": true,
        "ExcludeDir": true
      }
    },
    "ImportQos": {
      "MaxBandWidth": 1073741824,
      "MaxImportTaskQps": 1000
    },
    "ScheduleRule": {
      "StartCronExpression": "0 0 * * * ?",
      "SuspendCronExpression": "0 0 * * * ?",
      "MaxScheduleCount": 1
    }
  }
}

列举任务

以下示例代码用于列举账号下所有任务。

package sample;

import com.aliyun.hcs_mgw20240626.Client;
import com.aliyun.hcs_mgw20240626.models.*;
import com.google.gson.Gson;

public class Demo {
   /** 强烈建议不要把AccessKey ID和AccessKey Secret保存到工程代码里,否则可能导致AccessKey泄露,威胁您账号下所有资源的安全。*/
   static String accessKeyId = System.getenv("ALIBABA_CLOUD_ACCESS_KEY_ID");
   static String accessKeySecret = System.getenv("ALIBABA_CLOUD_ACCESS_KEY_SECRET");
   /** 填写主账号ID。*/
   static String userId = "11470***876***55";

   public static void main(String[] args) {
      try {
         com.aliyun.teaopenapi.models.Config config = new com.aliyun.teaopenapi.models.Config();
         // 这里以北京区域为例。
         config.setEndpoint("cn-beijing.mgw.aliyuncs.com");
         config.setRegionId(accessKeyId);
         config.setAccessKeyId(accessKeyId);
         config.setAccessKeySecret(accessKeySecret);
         Client client = new Client(config);
         ListJobRequest request = new ListJobRequest();
         // 根据实际填写marker,count。
         request.setCount(1);
         String marker = "";
         request.setMarker(marker);
         ListJobResponse resp = client.listJob(userId, request);
         System.out.println(new Gson().toJson(resp.getBody().getImportJobList()));
      } catch (Exception e) {
         e.printStackTrace();
      }
   }
}

正常返回示例

{
  "ImportJobList": {
    "NextMarker": "test_nex_marker",
    "Truncated": true,
    "ImportJob": [
      {
        "Name": "test_name",
        "SrcAddress": "test_src_address",
        "DestAddress": "test_dest_address",
        "Status": "IMPORT_JOB_DOING",
        "EnableMultiVersioning": false,
        "CreateTime": "2024-05-01T12:00:00.000Z",
        "ModifyTime": "2024-05-01T12:00:00.000Z",
        "Version": "test_id",
        "Audit": {
          "LogMode": "off"
        },
        "OverwriteMode": "always",
        "TransferMode": "all",
        "Tags": "K1:V1,K2:V2",
        "ParentName": "test_parent_name",
        "ParentVersion": "7db93837-a5ee-4e3a-b3c8-800e7947dabc",
        "ConvertSymlinkTarget": false,
        "CreateReport": false,
        "Owner": "test_owner",
        "FilterRule": {
          "KeyFilters": {
            "Includes": {
              "Regex": [
                ".*\\.jpg$"
              ]
            },
            "Excludes": {
              "Regex": [
                ".*\\.jpg$"
              ]
            }
          },
          "LastModifiedFilters": {
            "Includes": {
              "TimeFilter": [
                {
                  "StartTime": "2006-01-01T00:00:00Z",
                  "EndTime": "2006-12-31T59:59:59Z"
                }
              ]
            },
            "Excludes": {
              "TimeFilter": [
                {
                  "StartTime": "2006-01-01T00:00:00Z",
                  "EndTime": "2006-12-31T59:59:59Z"
                }
              ]
            }
          },
          "FileTypeFilters": {
            "ExcludeSymlink": true,
            "ExcludeDir": true
          }
        },
        "ImportQos": {
          "MaxBandWidth": 1073741824,
          "MaxImportTaskQps": 1000
        },
        "ScheduleRule": {
          "StartCronExpression": "0 0 * * * ?",
          "SuspendCronExpression": "0 0 * * * ?",
          "MaxScheduleCount": 1
        }
      }
    ]
  }
}

获取任务重试信息

说明

该方法的结果主要用于重试任务的参数配置中。

以下示例代码用于获取指定轮次的任务重试信息。

package sample;

import com.aliyun.hcs_mgw20240626.Client;
import com.aliyun.hcs_mgw20240626.models.*;
import com.google.gson.Gson;

public class Demo {
   /** 强烈建议不要把AccessKey ID和AccessKey Secret保存到工程代码里,否则可能导致AccessKey泄露,威胁您账号下所有资源的安全。*/
   static String accessKeyId = System.getenv("ALIBABA_CLOUD_ACCESS_KEY_ID");
   static String accessKeySecret = System.getenv("ALIBABA_CLOUD_ACCESS_KEY_SECRET");
   /** 填写主账号ID。*/
   static String userId = "11470***876***55";

   public static void main(String[] args) {
      // 填写任务名称。
      String jobName = "examplejob";
      // 填写任务轮次。
      int runtimeId = 1;
      try {
         com.aliyun.teaopenapi.models.Config config = new com.aliyun.teaopenapi.models.Config();
         // 这里以北京区域为例。
         config.setEndpoint("cn-beijing.mgw.aliyuncs.com");
         config.setRegionId(accessKeyId);
         config.setAccessKeyId(accessKeyId);
         config.setAccessKeySecret(accessKeySecret);
         Client client = new Client(config);
         GetJobResultRequest request = new GetJobResultRequest();
         request.runtimeId = runtimeId;
         GetJobResultResponse resp = client.getJobResult(userId, jobName, request);
         System.out.println(new Gson().toJson(resp.body.importJobResult));
      } catch (Exception e) {
         e.printStackTrace();
      }
   }
}

正常返回示例

{
  "ImportJobResult": {
    "ReadyRetry": "Ready",
    "InvPath": "mainfest.json",
    "InvBucket": "test_sys_bucket",
    "InvDomain": "test_domain",
    "InvLocation": "oss",
    "InvAccessId": "test_access_id",
    "InvAccessSecret": "test_secret_key",
    "InvRegionId": "test_region_id",
    "AddressType": "ossinv",
    "TotalObjectCount": 1000,
    "CopiedObjectCount": 800,
    "FailedObjectCount": 200,
    "TotalObjectSize": 1000,
    "CopiedObjectSize": 800,
    "Version": "test_job_id"
  }
}

列举任务历史记录

说明

该方法用于查询指定任务历史记录,详情请参看任务相关 API 文档。

以下示例代码用于列举指定任务的历史记录。

package sample;

import com.aliyun.hcs_mgw20240626.Client;
import com.aliyun.hcs_mgw20240626.models.*;
import com.google.gson.Gson;

public class Demo {
   /** 强烈建议不要把AccessKey ID和AccessKey Secret保存到工程代码里,否则可能导致AccessKey泄露,威胁您账号下所有资源的安全。*/
   static String accessKeyId = System.getenv("ALIBABA_CLOUD_ACCESS_KEY_ID");
   static String accessKeySecret = System.getenv("ALIBABA_CLOUD_ACCESS_KEY_SECRET");
   /** 填写主账号ID。*/
   static String userId = "11470***876***55";

   public static void main(String[] args) {
      // 填写任务名称。
      String jobName = "examplejob";
      // 填写任务轮次。runtimeId可以不指定,不指定表示列举指定任务所有轮历史记录。
      int runtimeId = 1;
      try {
         com.aliyun.teaopenapi.models.Config config = new com.aliyun.teaopenapi.models.Config();
         // 这里以北京区域为例。
         config.setEndpoint("cn-beijing.mgw.aliyuncs.com");
         config.setRegionId(accessKeyId);
         config.setAccessKeyId(accessKeyId);
         config.setAccessKeySecret(accessKeySecret);
         Client client = new Client(config);
         ListJobHistoryRequest request = new ListJobHistoryRequest();
         // 下列参数值请根据实际需求填写。
         request.runtimeId = runtimeId;
         request.count = 1;
         request.marker = "";
         ListJobHistoryResponse resp = client.listJobHistory(userId, jobName, request);
         request.marker = resp.body.jobHistoryList.nextMarker;
         resp = client.listJobHistory(userId, jobName, request);
         System.out.println(new Gson().toJson(resp.body.jobHistoryList));
      } catch (Exception e) {
         e.printStackTrace();
      }
   }
}

正常返回示例

{
  "JobHistoryList": {
    "Truncated": "true",
    "NextMarker": "test_next_marker",
    "JobHistory": [
      {
        "Name": "test_name",
        "JobVersion": "test_id",
        "RuntimeId": "1",
        "CommitId": "2",
        "StartTime": "2024-05-01T12:00:00.000Z",
        "EndTime": "2024-05-01T12:00:00.000Z",
        "Status": "IMPORT_JOB_DOING",
        "TotalCount": 1000,
        "CopiedCount": 900,
        "FailedCount": 100,
        "TotalSize": 1000,
        "CopiedSize": 1000,
        "RuntimeState": "Normal",
        "Message": "test error msg.",
        "Operator": "user",
        "ListStatus": "Listing"
      }
    ]
  }
}

删除任务

以下示例代码用于删除指定任务。

package sample;

import com.aliyun.hcs_mgw20240626.Client;
import com.aliyun.hcs_mgw20240626.models.*;

public class Demo {
   /** 强烈建议不要把AccessKey ID和AccessKey Secret保存到工程代码里,否则可能导致AccessKey泄露,威胁您账号下所有资源的安全。*/
   static String accessKeyId = System.getenv("ALIBABA_CLOUD_ACCESS_KEY_ID");
   static String accessKeySecret = System.getenv("ALIBABA_CLOUD_ACCESS_KEY_SECRET");
   /** 填写主账号ID。*/
   static String userId = "11470***876***55";

   public static void main(String[] args) {
      // 填写任务名称。
      String jobName = "examplejob";
      try {
         com.aliyun.teaopenapi.models.Config config = new com.aliyun.teaopenapi.models.Config();
         // 这里以北京区域为例。
         config.setEndpoint("cn-beijing.mgw.aliyuncs.com");
         config.setRegionId(accessKeyId);
         config.setAccessKeyId(accessKeyId);
         config.setAccessKeySecret(accessKeySecret);
         Client client = new Client(config);
         DeleteJobRequest request = new DeleteJobRequest();
         client.deleteJob(userId, jobName, request);
      } catch (Exception e) {
         e.printStackTrace();
      }
   }
}

创建任务迁移报告

说明

该方法为异步方法,调用该方法后后端开始准备生成迁移报告,是否生成完毕需要通过获取任务迁移报告方法来完成,具体详情请参见任务相关API文档。

以下示例代码用于创建指定任务迁移报告。

package sample;

import com.aliyun.hcs_mgw20240626.Client;
import com.aliyun.hcs_mgw20240626.models.*;

public class Demo {
   /** 强烈建议不要把AccessKey ID和AccessKey Secret保存到工程代码里,否则可能导致AccessKey泄露,威胁您账号下所有资源的安全。*/
   static String accessKeyId = System.getenv("ALIBABA_CLOUD_ACCESS_KEY_ID");
   static String accessKeySecret = System.getenv("ALIBABA_CLOUD_ACCESS_KEY_SECRET");
   /** 填写主账号ID。*/
   static String userId = "11470***876***55";

   public static void main(String[] args) {
      // 填写任务名称,必填。
      String jobName = "examplejob";
      // 填写任务ID,必填。
      String version = "b4155550-****-4371-****-9c7337348021";
      // 填写任务轮次,必填。
      int runtimeId = 1;
      try {
         com.aliyun.teaopenapi.models.Config config = new com.aliyun.teaopenapi.models.Config();
         // 这里以北京区域为例。
         config.setEndpoint("cn-beijing.mgw.aliyuncs.com");
         config.setRegionId(accessKeyId);
         config.setAccessKeyId(accessKeyId);
         config.setAccessKeySecret(accessKeySecret);
         Client client = new Client(config);
         CreateReportRequest request = new CreateReportRequest();
         CreateReportInfo info = new CreateReportInfo();
         info.jobName = jobName;
         info.version = version;
         info.runtimeId = runtimeId;
         request.setCreateReport(info);
         client.createReport(userId, request);
      } catch (Exception e) {
         e.printStackTrace();
      }
   }
}

获取任务迁移报告

说明

调用该方法可以查询到指定任务的迁移报告,可根据该方法判断迁移报告是否生成完毕以及获取迁移报告。

以下示例代码用于创建指定任务迁移报告。

package sample;

import com.aliyun.hcs_mgw20240626.Client;
import com.aliyun.hcs_mgw20240626.models.*;
import com.google.gson.Gson;

public class Demo {
   /** 强烈建议不要把AccessKey ID和AccessKey Secret保存到工程代码里,否则可能导致AccessKey泄露,威胁您账号下所有资源的安全。*/
   static String accessKeyId = System.getenv("ALIBABA_CLOUD_ACCESS_KEY_ID");
   static String accessKeySecret = System.getenv("ALIBABA_CLOUD_ACCESS_KEY_SECRET");
   /** 填写主账号ID。*/
   static String userId = "11470***876***55";

   public static void main(String[] args) {
      // 填写任务ID,必填。
      String version = "b4155550-****-4371-****-9c7337348021";
      // 填写任务轮次,必填。
      int runtimeId = 1;
      try {
         com.aliyun.teaopenapi.models.Config config = new com.aliyun.teaopenapi.models.Config();
         // 这里以北京区域为例。
         config.setEndpoint("cn-beijing.mgw.aliyuncs.com");
         config.setRegionId(accessKeyId);
         config.setAccessKeyId(accessKeyId);
         config.setAccessKeySecret(accessKeySecret);
         Client client = new Client(config);
         GetReportRequest request = new GetReportRequest();
         request.version = version;
         request.runtimeId = runtimeId;
         GetReportResponse resp = client.getReport(userId, request);
         System.out.println(new Gson().toJson(resp.body.getReportResponse));
      } catch (Exception e) {
         e.printStackTrace();
      }
   }
}

正常返回示例

{
  "GetReportResponse": {
    "Status": "Running",
    "ReportCreateTime": "2024-05-01T12:00:00.000Z",
    "ReportEndTime": "2024-05-01T12:00:00.000Z",
    "TotalCount": 1000,
    "CopiedCount": 800,
    "SkippedCount": 100,
    "FailedCount": 100,
    "JobCreateTime": "2024-05-01T12:00:00.000Z",
    "JobEndTime": "2024-05-01T12:00:00.000Z",
    "JobExecuteTime": "1000",
    "TotalListPrefix": "test_total_prefix/",
    "SkippedListPrefix": "test_skipped_prefix/",
    "FailedListPrefix": "test_failed_prefix/",
    "ErrorMessage": "test error msg."
  }
}