查询异步任务结果
本文档为您介绍查询异步任务结果常用语言和常见情况的示例代码。
说明
您可以进入在线咨询获取在线人工帮助。
阿里云视觉智能开放平台视觉AI能力API接入、接口使用或问题咨询等,请通过钉钉群(23109592)加入阿里云视觉智能开放平台咨询群联系我们。
能力介绍
关于查询异步任务结果的功能介绍以及具体调用参数说明,请参见查询异步任务结果。
SDK包安装
常见语言的SDK依赖包信息,请参见下文示例代码中的引入依赖包注释部分。
示例代码
/*
引入依赖包
最低SDK版本要求:viapi20230117的SDK版本需大于等于2.0.1。
可以在此仓库地址中引用最新版本SDK:https://mvnrepository.com/artifact/com.aliyun/viapi20230117
<!-- https://mvnrepository.com/artifact/com.aliyun/viapi20230117 -->
<dependency>
<groupId>com.aliyun</groupId>
<artifactId>viapi20230117</artifactId>
<version>${aliyun.viapi.version}</version>
</dependency>
*/
import com.aliyun.tea.TeaModel;
import com.aliyun.viapi20230117.models.GetAsyncJobResultResponse;
public class GetAsyncJobResult {
public static com.aliyun.viapi20230117.Client createClient(String accessKeyId, String accessKeySecret) throws Exception {
com.aliyun.teaopenapi.models.Config config = new com.aliyun.teaopenapi.models.Config()
// 必填,您的 AccessKey ID
.setAccessKeyId(accessKeyId)
// 必填,您的 AccessKey Secret
.setAccessKeySecret(accessKeySecret);
// 访问的域名
config.endpoint = "viapi.cn-shanghai.aliyuncs.com";
return new com.aliyun.viapi20230117.Client(config);
}
public static void main(String[] args_) throws Exception {
// "YOUR_ACCESS_KEY_ID", "YOUR_ACCESS_KEY_SECRET" 的生成请参考https://help.aliyun.com/document_detail/175144.html
// 如果您是用的子账号AccessKey,还需要为子账号授予权限AliyunVIAPIFullAccess,请参考https://help.aliyun.com/document_detail/145025.html
com.aliyun.viapi20230117.Client client = GetAsyncJobResult.createClient("YOUR_ACCESS_KEY_ID", "YOUR_ACCESS_KEY_SECRET");
com.aliyun.viapi20230117.models.GetAsyncJobResultRequest getAsyncJobResultRequest = new com.aliyun.viapi20230117.models.GetAsyncJobResultRequest()
.setJobId("1299348D-DFF2-5FDA-8C9C-C2D14EBF63F2");
com.aliyun.teautil.models.RuntimeOptions runtime = new com.aliyun.teautil.models.RuntimeOptions();
try {
GetAsyncJobResultResponse getAsyncJobResultResponse = client.getAsyncJobResultWithOptions(getAsyncJobResultRequest, runtime);
// 获取整体结果。
System.out.println(com.aliyun.teautil.Common.toJSONString(TeaModel.buildMap(getAsyncJobResultResponse)));
// 获取单个字段。
System.out.println(getAsyncJobResultResponse.getBody());
} catch (com.aliyun.tea.TeaException teaException) {
// 获取整体报错信息。
System.out.println(com.aliyun.teautil.Common.toJSONString(teaException));
// 获取单个字段。
System.out.println(teaException.getCode());
}
}
}
# -*- coding: utf-8 -*-
# 引入依赖包
# 最低SDK版本要求:viapi20230117的SDK版本需大于等于2.0.1。
# 可以在此仓库地址中引用最新版本SDK:https://pypi.org/project/alibabacloud-viapi20230117/
# pip install alibabacloud_viapi20230117
from alibabacloud_tea_openapi.models import Config
from alibabacloud_tea_util.models import RuntimeOptions
from alibabacloud_viapi20230117.client import Client
from alibabacloud_viapi20230117.models import GetAsyncJobResultRequest
config = Config(
# "YOUR_ACCESS_KEY_ID", "YOUR_ACCESS_KEY_SECRET" 的生成请参考https://help.aliyun.com/document_detail/175144.html
# 如果您是用的子账号AccessKey,还需要为子账号授予权限AliyunVIAPIFullAccess,请参考https://help.aliyun.com/document_detail/145025.html
# 您的 AccessKey ID
access_key_id='YOUR_ACCESS_KEY_ID',
# 您的 AccessKey Secret
access_key_secret='YOUR_ACCESS_KEY_SECRET',
# 访问的域名
endpoint='viapi.cn-shanghai.aliyuncs.com',
# 访问的域名对应的region
region_id='cn-shanghai'
)
get_async_job_result_request = GetAsyncJobResultRequest(
job_id='1299348D-DFF2-5FDA-8C9C-C2D14EBF63F2'
)
runtime = RuntimeOptions()
try:
# 初始化Client
client = Client(config)
response = client.get_async_job_result_with_options(get_async_job_result_request, runtime)
# 获取整体结果
print(response.body)
except Exception as error:
# 获取整体报错信息
print(error)
# 获取单个字段
print(error.code)
<?php
//安装依赖包
//最低SDK版本要求:viapi20230117的SDK版本需大于等于1.0.0。
//可以在此仓库地址中引用最新版本SDK:https://packagist.org/packages/alibabacloud/viapi-20230117
//composer require alibabacloud/viapi-20230117
use AlibabaCloud\SDK\Viapi\V20230117\Viapi;
use \Exception;
use AlibabaCloud\Tea\Utils\Utils;
use Darabonba\OpenApi\Models\Config;
use AlibabaCloud\SDK\Viapi\V20230117\Models\GetAsyncJobResultRequest;
use AlibabaCloud\Tea\Utils\Utils\RuntimeOptions;
class GetAsyncJobResult {
/**
* 使用AK&SK初始化账号Client
* @param string $accessKeyId
* @param string $accessKeySecret
* @return Viapi Client
*/
public static function createClient($accessKeyId, $accessKeySecret){
$config = new Config([
// 必填,您的 AccessKey ID
"accessKeyId" => $accessKeyId,
// 必填,您的 AccessKey Secret
"accessKeySecret" => $accessKeySecret
]);
// 访问的域名
$config->endpoint = "viapi.cn-shanghai.aliyuncs.com";
return new Viapi($config);
}
/**
* @param string[] $args
* @return void
*/
public static function main($args){
// "YOUR_ACCESS_KEY_ID", "YOUR_ACCESS_KEY_SECRET" 的生成请参考https://help.aliyun.com/document_detail/175144.html
// 如果您是用的子账号AccessKey,还需要为子账号授予权限AliyunVIAPIFullAccess,请参考https://help.aliyun.com/document_detail/145025.html
$client = self::createClient("YOUR_ACCESS_KEY_ID", "YOUR_ACCESS_KEY_SECRET");
$getAsyncJobResultRequest = new GetAsyncJobResultRequest([
"jobId" => "C9DF3928-0D19-58E1-9C82-C77F1BCFB269"
]);
$runtime = new RuntimeOptions([]);
try {
$resp = $client->getAsyncJobResultWithOptions($getAsyncJobResultRequest, $runtime);
# 获取整体结果
echo Utils::toJSONString($resp->body);
} catch (Exception $exception) {
# 获取整体报错信息
echo Utils::toJSONString($exception);
# 获取单个字段
echo $exception->getCode();
}
}
}
$path = __DIR__ . \DIRECTORY_SEPARATOR . '..' . \DIRECTORY_SEPARATOR . 'vendor' . \DIRECTORY_SEPARATOR . 'autoload.php';
if (file_exists($path)) {
require_once $path;
}
//$argv是预留的数组入参参数,无实际意义,无需进行修改
GetAsyncJobResult::main(array_slice($argv, 1));
// 安装依赖包
// 最低SDK版本要求:viapi20230117的SDK版本需大于等于1.0.0。
// 可以在此仓库地址中引用最新版本SDK:https://www.npmjs.com/package/@alicloud/viapi20230117
// npm install @alicloud/viapi20230117
const ViapiClient = require('@alicloud/viapi20230117');
const OpenapiClient = require('@alicloud/openapi-client');
const TeaUtil = require('@alicloud/tea-util');
let config = new OpenapiClient.Config({
// "YOUR_ACCESS_KEY_ID", "YOUR_ACCESS_KEY_SECRET" 的生成请参考https://help.aliyun.com/document_detail/175144.html
// 如果您是用的子账号AccessKey,还需要为子账号授予权限AliyunVIAPIFullAccess,请参考https://help.aliyun.com/document_detail/145025.html
// 您的 AccessKey ID
accessKeyId: 'YOUR_ACCESS_KEY_ID',
// 您的 AccessKey Secret
accessKeySecret: 'YOUR_ACCESS_KEY_SECRET'
});
// 访问的域名
config.endpoint = `viapi.cn-shanghai.aliyuncs.com`;
const client = new ViapiClient.default(config);
let getAsyncJobResultRequest = new ViapiClient.GetAsyncJobResultRequest({
jobId: "1299348D-DFF2-5FDA-8C9C-C2D14EBF63F2",
});
let runtime = new TeaUtil.RuntimeOptions({ });
client.getAsyncJobResultWithOptions(getAsyncJobResultRequest, runtime)
.then(function(getAsyncJobResultResponse) {
// 获取整体结果
console.log(getAsyncJobResultResponse);
// 获取单个字段
console.log(getAsyncJobResultResponse.body.data);
}, function(error) {
// 获取整体报错信息
console.log(error);
// 获取单个字段
console.log(error.data.Code);
})
/**
最低SDK版本要求:viapi20230117的SDK版本需大于等于v2.0.1。
依赖github.com/alibabacloud-go/viapi-20230117/v2
建议使用go mod tidy安装依赖
*/
package main
import (
"fmt"
openapi "github.com/alibabacloud-go/darabonba-openapi/v2/client"
util "github.com/alibabacloud-go/tea-utils/v2/service"
"github.com/alibabacloud-go/tea/tea"
viapi20230117 "github.com/alibabacloud-go/viapi-20230117/v2/client"
)
func main() {
config := &openapi.Config{
// "YOUR_ACCESS_KEY_ID", "YOUR_ACCESS_KEY_SECRET" 的生成请参考https://help.aliyun.com/document_detail/175144.html
// 如果您是用的子账号AccessKey,还需要为子账号授予权限AliyunVIAPIFullAccess,请参考https://help.aliyun.com/document_detail/145025.html
// 您的 AccessKey ID
AccessKeyId: tea.String("YOUR_ACCESS_KEY_ID"),
// 您的 AccessKey Secret
AccessKeySecret: tea.String("YOUR_ACCESS_KEY_SECRET"),
}
// 访问的域名
config.Endpoint = tea.String("viapi.cn-shanghai.aliyuncs.com")
client, err := viapi20230117.NewClient(config)
if err != nil {
panic(err)
}
getAsyncJobResultRequest := &viapi20230117.GetAsyncJobResultRequest{
JobId: tea.String("1299348D-DFF2-5FDA-8C9C-C2D14EBF63F2"),
}
runtime := &util.RuntimeOptions{}
getAsyncJobResultResponse, err := client.GetAsyncJobResultWithOptions(getAsyncJobResultRequest, runtime)
if err != nil {
// 获取整体报错信息
fmt.Println(err.Error())
} else {
// 获取整体结果
fmt.Println(getAsyncJobResultResponse)
}
}
// 安装依赖包
// 最低SDK版本要求:viapi20230117的SDK版本需大于等于2.0.1。
// 可以在此仓库地址中引用最新版本SDK:https://www.nuget.org/packages/AlibabaCloud.SDK.Viapi20230117
// dotnet add package AlibabaCloud.SDK.Viapi20230117
using System;
using System.Collections;
using System.Collections.Generic;
using System.Drawing;
using System.IO;
using System.Net;
using System.Threading.Tasks;
using AlibabaCloud.SDK.Viapi20230117.Models;
using Tea;
using Tea.Utils;
namespace AlibabaCloud.SDK.Sample
{
public class Sample
{
/**
* 使用AK&SK初始化账号Client
* @param accessKeyId
* @param accessKeySecret
* @return Client
* @throws Exception
*/
public static AlibabaCloud.SDK.Viapi20230117.Client CreateClient(string accessKeyId, string accessKeySecret)
{
AlibabaCloud.OpenApiClient.Models.Config config = new AlibabaCloud.OpenApiClient.Models.Config
{
// 您的 AccessKey ID
AccessKeyId = accessKeyId,
// 您的 AccessKey Secret
AccessKeySecret = accessKeySecret,
};
// 访问的域名
config.Endpoint = "viapi.cn-shanghai.aliyuncs.com";
return new AlibabaCloud.SDK.Viapi20230117.Client(config);
}
public static void Main(string[] args)
{
// "YOUR_ACCESS_KEY_ID", "YOUR_ACCESS_KEY_SECRET" 的生成请参考https://help.aliyun.com/document_detail/175144.html
// 如果您是用的子账号AccessKey,还需要为子账号授予权限AliyunVIAPIFullAccess,请参考https://help.aliyun.com/document_detail/145025.html
AlibabaCloud.SDK.Viapi20230117.Client client = CreateClient("YOUR_ACCESS_KEY_ID", "YOUR_ACCESS_KEY_SECRET");
AlibabaCloud.SDK.Viapi20230117.Models.GetAsyncJobResultRequest getAsyncJobResultRequest = new AlibabaCloud.SDK.Viapi20230117.Models.GetAsyncJobResultRequest
{
JobId = "1299348D-DFF2-5FDA-8C9C-C2D14EBF63F2",
};
AlibabaCloud.TeaUtil.Models.RuntimeOptions runtime = new AlibabaCloud.TeaUtil.Models.RuntimeOptions();
try
{
AlibabaCloud.SDK.Viapi20230117.Models.GetAsyncJobResultResponse getAsyncJobResultResponse = client.GetAsyncJobResultWithOptions(getAsyncJobResultRequest, runtime);
// 获取整体结果
Console.WriteLine(AlibabaCloud.TeaUtil.Common.ToJSONString(getAsyncJobResultResponse.Body));
// 获取单个字段
Console.WriteLine(AlibabaCloud.TeaUtil.Common.ToJSONString(getAsyncJobResultResponse.Body.Data));
}
catch (TeaException error)
{
// 如有需要,请打印 error
AlibabaCloud.TeaUtil.Common.AssertAsString(error.Message);
}
catch (Exception _error)
{
TeaException error = new TeaException(new Dictionary<string, object>
{
{ "message", _error.Message }
});
// 如有需要,请打印 error
Console.WriteLine(error.Message);
}
}
}
}