任务

更新时间:

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

创建任务

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

package main

import (
   "fmt"
   openapipackage "github.com/alibabacloud-go/darabonba-openapi/v2/client"
   mgwpackage "github.com/alibabacloud-go/hcs-mgw-20240626/client"
   "os"
)

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

func main()  {
   // 这里以北京区域为例。
   endpoint := "cn-beijing.mgw.aliyuncs.com"
   // 填写任务名称。
   jobName := "examplejob";
   // 填写源端数据地址名称。
   srcAddress := "examplesrcaddress"
   // 填写目的端数据地址名称。
   destAddress := "exampledestaddress"
   config := openapipackage.Config{
      AccessKeyId:     &accessKeyId,
      AccessKeySecret: &accessKeySecret,
      Endpoint:        &endpoint,
   }
   client, err := mgwpackage.NewClient(&config)
   if err != nil{
      fmt.Errorf("create client failed, %v", err)
      return
   }
    /**
      overwriteMode和transferMode需要组合使用,具体组合含义如下
      always,all 全覆盖
      always,lastmodified 根据文件最后修改时间覆盖
      never,all 不覆盖
    */
   /* 文件覆盖方式, 可能的值 1.never  2.always */
   overwriteMode := "always"
   /* 文件传输的方式, 可能的值为 1.changed 2.all 3.lastmodified */
   transferMode := "lastmodified"

   // maxBandWidth,maxImportTaskQps 根据实际需求值填写。
   maxImportTaskQps := int64(1000)
   maxBandWidth := int64(2147483648)
   // 如果maxImportTaskQps值为0或者不设置,会被设置为默认值;MaxBandWidth值为0或者不设置,会被设置为默认值,maxBandWidth值单位为bits。
   importQos := mgwpackage.ImportQos{
      MaxImportTaskQps: &maxImportTaskQps,
      MaxBandWidth:     &maxBandWidth,
   }


   // 配置过滤规则,包含文件类型过滤器、文件过滤器、时间过滤器,具体参数含义请参看API文档。
   // 文件类型过滤器,适用于localfs。
   excludeSymlink := false
   excludeDir := false
   fileTypeFilters := &mgwpackage.FileTypeFilters{
      &excludeDir, &excludeSymlink,
   }

   // 文件过滤器,根据实际需求值填写。
   var includeRegex []*string
   var excludeRegex []*string
   jpgFile := ".*.jpg"
   gifFile := ".*.gif"
   txtFile := ".*.txt"
   jsFile := ".*.js"
   includeRegex = append(includeRegex, &jpgFile, &gifFile)
   excludeRegex = append(excludeRegex, &txtFile, &jsFile)
   KeyFilters := &mgwpackage.KeyFilters{
      Excludes: &mgwpackage.KeyFilterItem{
         excludeRegex,
      }, Includes: &mgwpackage.KeyFilterItem{
         includeRegex,
      },
   }
   // 时间过滤器, 时间格式遵循UTC时间格式,根据实际需求值填写。
   includeStartTime := "2006-01-01T00:00:00Z"
   includeEndTime := "2007-01-01T00:00:00Z"
   excludeStartTime := "2009-01-01T00:00:00Z"
   excludeEndTime := "2010-01-01T00:00:00Z"
   includeTimeFilter := []*mgwpackage.TimeFilter{{
      &includeStartTime, &includeEndTime,
   }}
   includeLastModifyFilters := &mgwpackage.LastModifyFilterItem{
      includeTimeFilter,
   }
   excludeTimeFilter := []*mgwpackage.TimeFilter{{
      EndTime: &excludeStartTime, StartTime: &excludeEndTime,
   }}
   excludeLastModifyFilters := &mgwpackage.LastModifyFilterItem{
      TimeFilter: excludeTimeFilter,
   }
   lastModifiedFilters := &mgwpackage.LastModifiedFilters{
      Excludes: excludeLastModifyFilters, Includes: includeLastModifyFilters,
   }
   filterRule := mgwpackage.FilterRule{
      LastModifiedFilters: lastModifiedFilters,
      KeyFilters:          KeyFilters,
      FileTypeFilters:     fileTypeFilters,
   }

   // 配置调度规则,具体参数含义请参看API文档。
   maxScheduleCount := int64(5)
   startCronExpression := "0 0 10 * * ?"
   suspendCronExpression := "0 0 14 * * ?"
   scheduleRule := mgwpackage.ScheduleRule{
      MaxScheduleCount:      &maxScheduleCount,
      StartCronExpression:   &startCronExpression,
      SuspendCronExpression: &suspendCronExpression,
   }
   _, err = client.CreateJob(&userId, &mgwpackage.CreateJobRequest{ImportJob: &mgwpackage.CreateJobInfo{
      Name:          &jobName,
      TransferMode:  &transferMode,
      OverwriteMode: &overwriteMode,
      SrcAddress:    &srcAddress,
      DestAddress:   &destAddress,
      ImportQos:     &importQos,
      FilterRule:    &filterRule,
      ScheduleRule:  &scheduleRule,
   }})
   if err != nil {
      fmt.Errorf("create job failed, %v", err)
   }
}

更新任务

说明

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

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

package main

import (
   "fmt"
   openapipackage "github.com/alibabacloud-go/darabonba-openapi/v2/client"
   mgwpackage "github.com/alibabacloud-go/hcs-mgw-20240626/client"
   "os"
)

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

func main()  {
   // 这里以北京区域为例。
   endpoint := "cn-beijing.mgw.aliyuncs.com"
   // 填写任务名称。
   jobName := "examplejob";
   config := openapipackage.Config{
      AccessKeyId:     &accessKeyId,
      AccessKeySecret: &accessKeySecret,
      Endpoint:        &endpoint,
   }
   client, err := mgwpackage.NewClient(&config)
   if err != nil{
      fmt.Errorf("create client failed, %v", err)
      return
   }
   // 允许的状态值有IMPORT_JOB_LAUNCHING(启动任务), IMPORT_JOB_SUSPEND(暂停任务),IMPORT_JOB_CLOSING(关闭任务)。
   status := "IMPORT_JOB_LAUNCHING"
   _, err = client.UpdateJob(&userId, &jobName, &mgwpackage.UpdateJobRequest{
      &mgwpackage.UpdateJobInfo{Status: &status},
   })
   if err != nil {
      fmt.Errorf("update job failed, %v", err)
   }
}

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

package main

import (
	"fmt"
	openapipackage "github.com/alibabacloud-go/darabonba-openapi/v2/client"
	mgwpackage "github.com/alibabacloud-go/hcs-mgw-20240626/client"
	"os"
)

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

func main()  {
	// 这里以北京区域为例。
       endpoint := "cn-beijing.mgw.aliyuncs.com"
	// 填写任务ID,必填。
	jobName := "examplejob"
	config := openapipackage.Config{
		AccessKeyId:     &accessKeyId,
		AccessKeySecret: &accessKeySecret,
		Endpoint:        &endpoint,
	}
	client, err := mgwpackage.NewClient(&config)
	if err != nil{
		fmt.Errorf("create client failed, %v", err)
		return
	}

	// maxBandWidth、maxImportTaskQps请根据需求填写。
	maxBandWidth := int64(1610612736)
	maxImportTaskQps := int64(1500)
	_, err = client.UpdateJob(&userId, &jobName, &mgwpackage.UpdateJobRequest{
		&mgwpackage.UpdateJobInfo{
				ImportQos: &mgwpackage.ImportQos{
					MaxBandWidth: &maxBandWidth,
					MaxImportTaskQps: &maxImportTaskQps,
				},
			},
	})
	if err != nil {
		fmt.Errorf("update job failed, %v", err)
	}
}

获取任务详情

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

package main

import (
   "encoding/json"
   "fmt"
   openapipackage "github.com/alibabacloud-go/darabonba-openapi/v2/client"
   mgwpackage "github.com/alibabacloud-go/hcs-mgw-20240626/client"
   "os"
)

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

func main()  {
   // 这里以北京区域为例。
   endpoint := "cn-beijing.mgw.aliyuncs.com"
   // 填写任务名称。
   jobName := "examplejob";
   config := openapipackage.Config{
      AccessKeyId:     &accessKeyId,
      AccessKeySecret: &accessKeySecret,
      Endpoint:        &endpoint,
   }
   client, err := mgwpackage.NewClient(&config)
   if err != nil{
      fmt.Errorf("create client failed, %v", err)
      return
   }
   resp, err := client.GetJob(&userId, &jobName, &mgwpackage.GetJobRequest{})
   if err != nil {
      fmt.Errorf("get job failed, %v", err)
   }
   jsonBytes, err := json.Marshal(resp)
   if err != nil {
      fmt.Println("covert failed,", err)
      return
   }
   fmt.Println(string(jsonBytes))
}

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

package main

import (
	"encoding/json"
	"fmt"
	openapipackage "github.com/alibabacloud-go/darabonba-openapi/v2/client"
	mgwpackage "github.com/alibabacloud-go/hcs-mgw-20240626/client"
	"os"
)

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

func main()  {
	// 这里以北京区域为例。
        endpoint := "cn-beijing.mgw.aliyuncs.com"
	// 填写任务ID,必填。
	jobId := "b4155550-****-4371-****-9c7337348021"
	config := openapipackage.Config{
		AccessKeyId:     &accessKeyId,
		AccessKeySecret: &accessKeySecret,
		Endpoint:        &endpoint,
	}
	client, err := mgwpackage.NewClient(&config)
	if err != nil{
		fmt.Errorf("create client failed, %v", err)
		return
	}
	byVersion := ""
	resp, err := client.GetJob(&userId, &jobId, &mgwpackage.GetJobRequest{
		ByVersion: &byVersion,
	})
	if err != nil {
		fmt.Errorf("get job failed, %v", err)
	}
	jsonBytes, err := json.Marshal(resp)
	if err != nil {
		fmt.Println("covert failed,", err)
		return
	}
	fmt.Println(string(jsonBytes))
}

正常返回示例

{
  "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 main

import (
   "encoding/json"
   "fmt"
   openapipackage "github.com/alibabacloud-go/darabonba-openapi/v2/client"
   mgwpackage "github.com/alibabacloud-go/hcs-mgw-20240626/client"
   "os"
)

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

func main()  {
   // 这里以北京区域为例。
   endpoint := "cn-beijing.mgw.aliyuncs.com"
   // 填写任务名称。
   jobName := "examplejob";
   config := openapipackage.Config{
      AccessKeyId:     &accessKeyId,
      AccessKeySecret: &accessKeySecret,
      Endpoint:        &endpoint,
   }
   client, err := mgwpackage.NewClient(&config)
   if err != nil{
      fmt.Errorf("create client failed, %v", err)
      return
   }
   // 根据实际填写marker,count。
   count := int32(1)
   marker := ""
   resp, err := ctx.Client.ListJob(&userId, &mgwpackage.ListJobRequest{
      Count:&count,
      Marker:&marker,
   })
   if err != nil {
      fmt.Errorf("list job failed, %v", err)
   }
   jsonBytes, err := json.Marshal(resp)
   if err != nil {
      fmt.Println("covert failed,", err)
      return
   }
   fmt.Println(string(jsonBytes))
}

正常返回示例

{
  "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 main

import (
   "encoding/json"
   "fmt"
   openapipackage "github.com/alibabacloud-go/darabonba-openapi/v2/client"
   mgwpackage "github.com/alibabacloud-go/hcs-mgw-20240626/client"
   "os"
)

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

func main()  {
   // 这里以北京区域为例。
   endpoint := "cn-beijing.mgw.aliyuncs.com"
   // 填写任务名称。
   jobName := "examplejob";
   // 填写任务轮次。
   runtimeId := int32(1)
   config := openapipackage.Config{
      AccessKeyId:     &accessKeyId,
      AccessKeySecret: &accessKeySecret,
      Endpoint:        &endpoint,
   }
   client, err := mgwpackage.NewClient(&config)
   if err != nil{
      fmt.Errorf("create client failed, %v", err)
      return
   }

   resp, err := client.GetJobResult(&userId, &jobName, &mgwpackage.GetJobResultRequest{
      &runtimeId,
   })
   if err != nil {
      fmt.Errorf("get job result failed, %v", err)
   }
   jsonBytes, err := json.Marshal(resp)
   if err != nil {
      fmt.Println("covert failed,", err)
      return
   }
   fmt.Println(string(jsonBytes))
}

正常返回示例

{
  "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 main

import (
   "encoding/json"
   "fmt"
   openapipackage "github.com/alibabacloud-go/darabonba-openapi/v2/client"
   mgwpackage "github.com/alibabacloud-go/hcs-mgw-20240626/client"
   "os"
)

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

func main()  {
   // 这里以北京区域为例。
   endpoint := "cn-beijing.mgw.aliyuncs.com"
   // 填写任务名称。
   jobName := "examplejob";
   // 填写任务轮次。runtimeId可以不指定,不指定表示列举指定任务所有轮历史记录。
   runtimeId := int32(1)
   config := openapipackage.Config{
      AccessKeyId:     &accessKeyId,
      AccessKeySecret: &accessKeySecret,
      Endpoint:        &endpoint,
   }
   client, err := mgwpackage.NewClient(&config)
   if err != nil{
      fmt.Errorf("create client failed, %v", err)
      return
   }
   // 下列参数值请根据实际需求填写。
   count := int32(1)
   marker := ""
   resp, err := client.ListJobHistory(&userId, &jobName, &mgwpackage.ListJobHistoryRequest{
      Count: &count,
      Marker: &marker,
      RuntimeId: &runtimeId,
   })
   if err != nil {
      fmt.Errorf("list job history failed, %v", err)
   }
   jsonBytes, err := json.Marshal(resp)
   if err != nil {
      fmt.Println("covert failed,", err)
      return
   }
   fmt.Println(string(jsonBytes))
}

正常返回示例

{
  "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 main

import (
   "fmt"
   openapipackage "github.com/alibabacloud-go/darabonba-openapi/v2/client"
   mgwpackage "github.com/alibabacloud-go/hcs-mgw-20240626/client"
   "os"
)

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

func main()  {
   // 这里以北京区域为例。
   endpoint := "cn-beijing.mgw.aliyuncs.com"
   // 填写任务名称。
   jobName := "examplejob";
   config := openapipackage.Config{
      AccessKeyId:     &accessKeyId,
      AccessKeySecret: &accessKeySecret,
      Endpoint:        &endpoint,
   }
   client, err := mgwpackage.NewClient(&config)
   if err != nil{
      fmt.Errorf("create client failed, %v", err)
      return
   }
   _, err = client.DeleteJob(&userId, &jobName, &mgwpackage.DeleteJobRequest{})
   if err != nil {
      fmt.Errorf("delete job failed, %v", err)
   }
}

创建任务迁移报告

说明

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

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

package main

import (
   "fmt"
   openapipackage "github.com/alibabacloud-go/darabonba-openapi/v2/client"
   mgwpackage "github.com/alibabacloud-go/hcs-mgw-20240626/client"
   "os"
)

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

func main()  {
   // 这里以北京区域为例。
   endpoint := "cn-beijing.mgw.aliyuncs.com"
   // 填写任务名称。
   jobName := "examplejob"
   // 填写任务ID,必填。
   version := "b4155550-****-4371-****-9c7337348021"
   // 填写任务轮次,必填。
   runtimeId := int32(1)
   config := openapipackage.Config{
      AccessKeyId:     &accessKeyId,
      AccessKeySecret: &accessKeySecret,
      Endpoint:        &endpoint,
   }
   client, err := mgwpackage.NewClient(&config)
   if err != nil{
      fmt.Errorf("create client failed, %v", err)
      return
   }
   _, err = client.CreateReport(&userId, &mgwpackage.CreateReportRequest{
      CreateReport: &mgwpackage.CreateReportInfo{
         JobName :  &jobName,
         RuntimeId : &runtimeId,
         Version :  &version,
      },
   })
   if err != nil {
      fmt.Errorf("create report failed, %v", err)
   }
}

查询任务迁移报告

说明

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

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

package main

import (
   "encoding/json"
   "fmt"
   openapipackage "github.com/alibabacloud-go/darabonba-openapi/v2/client"
   mgwpackage "github.com/alibabacloud-go/hcs-mgw-20240626/client"
   "os"
)

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

func main()  {
   // 这里以北京区域为例。
   endpoint := "cn-beijing.mgw.aliyuncs.com"
   // 填写任务ID,必填。
   version := "b4155550-****-4371-****-9c7337348021"
   // 填写任务轮次,必填。
   runtimeId := int32(1)
   config := openapipackage.Config{
      AccessKeyId:     &accessKeyId,
      AccessKeySecret: &accessKeySecret,
      Endpoint:        &endpoint,
   }
   client, err := mgwpackage.NewClient(&config)
   if err != nil{
      fmt.Errorf("create client failed, %v", err)
      return
   }
   resp, err := client.GetReport(&userId, &mgwpackage.GetReportRequest{
      RuntimeId :&runtimeId,
      Version : &version,
   })
   if err != nil {
      fmt.Errorf("get report failed, %v", err)
   }
   jsonBytes, err := json.Marshal(resp)
   if err != nil {
      fmt.Println("covert failed,", err)
      return
   }
   fmt.Println(string(jsonBytes))
}

正常返回示例

{
  "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."
  }
}