全部產品
Search
文件中心

Data Online Migration:任務

更新時間:Feb 07, 2026

本文介紹如何使用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.setAccessKeyId(accessKeyId);
         config.setAccessKeySecret(accessKeySecret);
         // 支援 HTTPS、HTTP, 未指定時預設採用 HTTPs。
         config.setProtocol("http");
         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,"" 不覆蓋
        */
         /* 檔案覆蓋方式, 可能得值為 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();
      }
   }
}

更新任務

說明

建立任務後,您可以通過該方法來啟動任務、暫停任務以及關閉任務,也可以通過該方法來調整任務限流資訊。

以下範例程式碼用於更新任務狀態。

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.setAccessKeyId(accessKeyId);
			config.setAccessKeySecret(accessKeySecret);
			// 支援 HTTPS、HTTP, 未指定時預設採用 HTTPs。
                        config.setProtocol("http");
			Client client = new Client(config);
			UpdateJobRequest updateJobRequest = new UpdateJobRequest();
			UpdateJobInfo updateJobInfo = new UpdateJobInfo();
			//允許的狀態值有IMPORT_JOB_LAUNCHING(啟動任務), IMPORT_JOB_SUSPEND(暫停任務),IMPORT_JOB_CLOSING(關閉任務)。
			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.setAccessKeyId(accessKeyId);
			config.setAccessKeySecret(accessKeySecret);
			// 支援 HTTPS、HTTP, 未指定時預設採用 HTTPs。
                        config.setProtocol("http");
			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.setAccessKeyId(accessKeyId);
         config.setAccessKeySecret(accessKeySecret);
         // 支援 HTTPS、HTTP, 未指定時預設採用 HTTPs。
         config.setProtocol("http");
         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.setAccessKeyId(accessKeyId);
			config.setAccessKeySecret(accessKeySecret);
			// 支援 HTTPS、HTTP, 未指定時預設採用 HTTPs。
                        config.setProtocol("http");
			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.setAccessKeyId(accessKeyId);
         config.setAccessKeySecret(accessKeySecret);
         // 支援 HTTPS、HTTP, 未指定時預設採用 HTTPs。
         config.setProtocol("http");
         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
        }
      }
    ]
  }
}

擷取任務重試資訊

說明
  • 任務運行完成後,可能存在失敗檔案,線上遷移服務會對這些失敗檔案構造一份清單。通過以下範例程式碼擷取指定輪次失敗檔案的清單資訊,如返回結果為Ready,可用該清單重試失敗檔案。

  • 該方法的結果主要用於重試任務的參數配置中。

  • 該方法主要用於當遷移任務有失敗檔案時,擷取失敗檔案是否能重試的資訊。

以下範例程式碼用於擷取指定輪次的任務重試資訊。

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.setAccessKeyId(accessKeyId);
         config.setAccessKeySecret(accessKeySecret);
         // 支援 HTTPS、HTTP, 未指定時預設採用 HTTPs。
         config.setProtocol("http");
         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,
    "SkippedObjectCount": 200,
    "TotalObjectSize": 1000,
    "CopiedObjectSize": 800,
    "SkippedObjectSize": 800,
    "Version": "test_job_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) {
      // 填寫任務名稱。
      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.setAccessKeyId(accessKeyId);
         config.setAccessKeySecret(accessKeySecret);
         // 支援 HTTPS、HTTP, 未指定時預設採用 HTTPs。
         config.setProtocol("http");
         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": 5368709120,
        "CopiedSize": 4294967296,
        "SkippedCount": 0,
        "SkippedSize": 0,
        "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.setAccessKeyId(accessKeyId);
         config.setAccessKeySecret(accessKeySecret);
         // 支援 HTTPS、HTTP, 未指定時預設採用 HTTPs。
         config.setProtocol("http");
         Client client = new Client(config);
         DeleteJobRequest request = new DeleteJobRequest();
         client.deleteJob(userId, jobName, request);
      } 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";
      // 填寫任務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.setAccessKeyId(accessKeyId);
         config.setAccessKeySecret(accessKeySecret);
         // 支援 HTTPS、HTTP, 未指定時預設採用 HTTPs。
         config.setProtocol("http");
         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.setAccessKeyId(accessKeyId);
         config.setAccessKeySecret(accessKeySecret);
         // 支援 HTTPS、HTTP, 未指定時預設採用 HTTPs。
         config.setProtocol("http");
         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."
  }
}