文档审核增强版支持SDK方式调用和原生HTTPS方式调用。推荐您使用SDK接入,可免去签名验证以及Body格式构建等细节步骤。本文介绍文档审核增强版接入的方式。
步骤一:开通服务
访问开通内容审核增强版服务页面,开通文档审核增强版服务。接口接入使用后系统会按使用量自动出账,具体信息,请参见计费说明。您也可以购买按量抵扣资源包,资源包相较于后付费存在一定阶梯折扣,适合使用量级可预期和较大的用户。
开通文档审核增强版服务后,默认付费方式是按量后付费,且按照实际用量结算当日费用,不调用服务不收费。
步骤二:为RAM用户授权
在接入SDK或者API之前,您需要为RAM用户授权。您可以为RAM用户创建一个访问密钥(AccessKey)。在调用阿里云API时您需要使用AccessKey完成身份验证。获取方式,请参见获取AccessKey。
步骤三:安装并接入文档审核服务
目前支持的接入地域为:
地域 | 外网接入地址 | 内网接入地址 | 支持的服务 |
华东2(上海) | green-cip.cn-shanghai.aliyuncs.com | green-cip-vpc.cn-shanghai.aliyuncs.com | document_detection |
华北2(北京) | green-cip.cn-beijing.aliyuncs.com | green-cip-vpc.cn-beijing.aliyuncs.com | |
华东1(杭州) | green-cip.cn-hangzhou.aliyuncs.com | green-cip-vpc.cn-hangzhou.aliyuncs.com | |
华南1(深圳) | green-cip.cn-shenzhen.aliyuncs.com | green-cip-vpc.cn-shenzhen.aliyuncs.com |
如果需要其他语言的SDK示例代码,您可以通过OpenAPI 开发者门户在线调试工具调试API接口,该工具会自动生成相应API的SDK调用示例代码。
阿里云SDK代码通过定义ALIBABA_CLOUD_ACCESS_KEY_ID
和ALIBABA_CLOUD_ACCESS_KEY_SECRET
环境变量来创建默认的访问凭证。调用接口时,程序直接访问凭证,读取您的访问密钥(即AccessKey)并自动完成鉴权。您在接入SDK代码示例之前,请先配置环境变量。具体操作,请参见身份验证配置。
Java SDK
在pom.xml中加入相应依赖,就可以在Maven工程中使用SDK。
在dependencies中添加如下依赖:
<dependency> <groupId>com.aliyun</groupId> <artifactId>green20220302</artifactId> <version>2.2.7</version> </dependency>
接入Java SDK。
提交文档检测任务代码示例
import com.alibaba.fastjson.JSON; import com.alibaba.fastjson.JSONObject; import com.aliyun.green20220302.Client; import com.aliyun.green20220302.models.FileModerationRequest; import com.aliyun.green20220302.models.FileModerationResponse; import com.aliyun.green20220302.models.FileModerationResponseBody; import com.aliyun.teaopenapi.models.Config; public class FileModerationDemo { public static void main(String[] args) throws Exception { Config config = new Config(); /** * 阿里云账号AccessKey拥有所有API的访问权限,建议您使用RAM用户进行API访问或日常运维。 * 常见获取环境变量方式: * 方式一: * 获取RAM用户AccessKey ID:System.getenv("ALIBABA_CLOUD_ACCESS_KEY_ID"); * 获取RAM用户AccessKey Secret:System.getenv("ALIBABA_CLOUD_ACCESS_KEY_SECRET"); * 方式二: * 获取RAM用户AccessKey ID:System.getProperty("ALIBABA_CLOUD_ACCESS_KEY_ID"); * 获取RAM用户AccessKey Secret:System.getProperty("ALIBABA_CLOUD_ACCESS_KEY_SECRET"); */ config.setAccessKeyId("建议从环境变量中获取RAM用户AccessKey ID"); config.setAccessKeySecret("建议从环境变量中获取RAM用户AccessKey Secret"); //接入区域和地址请根据实际情况修改。 config.setRegionId("cn-shanghai"); config.setEndpoint("green-cip.cn-shanghai.aliyuncs.com"); //连接时超时时间,单位毫秒(ms)。 config.setReadTimeout(6000); //读取时超时时间,单位毫秒(ms)。 config.setConnectTimeout(3000); Client client = new Client(config); JSONObject serviceParameters = new JSONObject(); serviceParameters.put("url", "https://xxx.oss.aliyuncs.com/xxx.pdf"); // 文件URL FileModerationRequest fileModerationRequest = new FileModerationRequest(); // 检测类型:document_detection 通用文档检测 fileModerationRequest.setService("document_detection"); fileModerationRequest.setServiceParameters(serviceParameters.toJSONString()); try { FileModerationResponse response = client.fileModeration(fileModerationRequest); if (response.getStatusCode() == 200) { FileModerationResponseBody result = response.getBody(); System.out.println(JSON.toJSONString(result)); System.out.println("requestId = " + result.getRequestId()); System.out.println("code = " + result.getCode()); System.out.println("msg = " + result.getMessage()); Integer code = result.getCode(); if (200 == code) { FileModerationResponseBody.FileModerationResponseBodyData data = result.getData(); System.out.println("taskId = [" + data.getTaskId() + "]"); } else { System.out.println("file moderation not success. code:" + code); } } else { System.out.println("response not success. status:" + response.getStatusCode()); } } catch (Exception e) { e.printStackTrace(); } } }
获取文档检测任务结果
import com.alibaba.fastjson.JSON; import com.alibaba.fastjson.JSONObject; import com.aliyun.green20220302.Client; import com.aliyun.green20220302.models.DescribeFileModerationResultRequest; import com.aliyun.green20220302.models.DescribeFileModerationResultResponse; import com.aliyun.green20220302.models.DescribeFileModerationResultResponseBody; import com.aliyun.teaopenapi.models.Config; public class DescribeFileModerationResultDemo { public static void main(String[] args) throws Exception { Config config = new Config(); /** * 阿里云账号AccessKey拥有所有API的访问权限,建议您使用RAM用户进行API访问或日常运维。 * 常见获取环境变量方式: * 方式一: * 获取RAM用户AccessKey ID:System.getenv("ALIBABA_CLOUD_ACCESS_KEY_ID"); * 获取RAM用户AccessKey Secret:System.getenv("ALIBABA_CLOUD_ACCESS_KEY_SECRET"); * 方式二: * 获取RAM用户AccessKey ID:System.getProperty("ALIBABA_CLOUD_ACCESS_KEY_ID"); * 获取RAM用户AccessKey Secret:System.getProperty("ALIBABA_CLOUD_ACCESS_KEY_SECRET"); */ config.setAccessKeyId("建议从环境变量中获取RAM用户AccessKey ID"); config.setAccessKeySecret("建议从环境变量中获取RAM用户AccessKey Secret"); //接入区域和地址请根据实际情况修改 config.setRegionId("cn-shanghai"); config.setEndpoint("green-cip.cn-shanghai.aliyuncs.com"); //连接时超时时间,单位毫秒(ms)。 config.setReadTimeout(6000); //读取时超时时间,单位毫秒(ms)。 config.setConnectTimeout(3000); Client client = new Client(config); JSONObject serviceParameters = new JSONObject(); // 提交任务时返回的taskId serviceParameters.put("taskId", "fi_f_O5z5i*********-1xxp0V"); DescribeFileModerationResultRequest describeFileModerationResultRequest = new DescribeFileModerationResultRequest(); describeFileModerationResultRequest.setService("document_detection"); describeFileModerationResultRequest.setServiceParameters(serviceParameters.toJSONString()); try { DescribeFileModerationResultResponse response = client.describeFileModerationResult(describeFileModerationResultRequest); if (response.getStatusCode() == 200) { DescribeFileModerationResultResponseBody result = response.getBody(); System.out.println("requestId=" + result.getRequestId()); System.out.println("code=" + result.getCode()); System.out.println("msg=" + result.getMessage()); if (200 == result.getCode()) { DescribeFileModerationResultResponseBody.DescribeFileModerationResultResponseBodyData data = result.getData(); System.out.println("pageResult = " + JSON.toJSONString(data.getPageResult())); System.out.println("dataId = " + data.getDataId()); System.out.println("url = " + data.getUrl()); } else { System.out.println("file moderation result not success. code:" + result.getCode()); } } else { System.out.println("response not success. status:" + response.getStatusCode()); } } catch (Exception e) { e.printStackTrace(); } } }
Python SDK
支持Python 3.6及以上版本。
执行如下命令安装pip。
pip install alibabacloud_green20220302==2.2.7
接入Python SDK。
提交文档检测任务代码示例
# coding=utf-8 # python version >= 3.6 from alibabacloud_green20220302.client import Client from alibabacloud_green20220302 import models from alibabacloud_tea_openapi.models import Config import json config = Config( # 阿里云账号AccessKey拥有所有API的访问权限,建议您使用RAM用户进行API访问或日常运维。 # 强烈建议不要把AccessKey ID和AccessKey Secret保存到工程代码里,否则可能导致AccessKey泄露,威胁您账号下所有资源的安全。 # 常见获取环境变量方式: # 获取RAM用户AccessKey ID:os.environ['ALIBABA_CLOUD_ACCESS_KEY_ID'] # 获取RAM用户AccessKey Secret:os.environ['ALIBABA_CLOUD_ACCESS_KEY_SECRET'] access_key_id='建议从环境变量中获取RAM用户AccessKey ID', access_key_secret='建议从环境变量中获取RAM用户AccessKey Secret', # 连接超时时间 单位毫秒(ms) connect_timeout=10000, # 读超时时间 单位毫秒(ms) read_timeout=3000, region_id='cn-shanghai', endpoint='green-cip.cn-shanghai.aliyuncs.com' ) clt = Client(config) serviceParameters = { 'url': 'https://detect-obj.oss-cn-hangzhou.aliyuncs.com/sample/xxxx.pdf', } fileModerationRequest = models.FileModerationRequest( # 检测类型 service='document_detection', service_parameters=json.dumps(serviceParameters) ) try: response = clt.file_moderation(fileModerationRequest) if response.status_code == 200: # 调用成功 result = response.body print('response success. result:{}'.format(result)) else: print('response not success. status:{} ,result:{}'.format(response.status_code, response)) except Exception as err: print(err)
获取文档检测任务结果
# coding=utf-8 # python version >= 3.6 from alibabacloud_green20220302.client import Client from alibabacloud_green20220302 import models from alibabacloud_tea_openapi.models import Config import json config = Config( # 阿里云账号AccessKey拥有所有API的访问权限,建议您使用RAM用户进行API访问或日常运维。 # 强烈建议不要把AccessKey ID和AccessKey Secret保存到工程代码里,否则可能导致AccessKey泄露,威胁您账号下所有资源的安全。 # 常见获取环境变量方式: # 获取RAM用户AccessKey ID:os.environ['ALIBABA_CLOUD_ACCESS_KEY_ID'] # 获取RAM用户AccessKey Secret:os.environ['ALIBABA_CLOUD_ACCESS_KEY_SECRET'] access_key_id='建议从环境变量中获取RAM用户AccessKey ID', access_key_secret='建议从环境变量中获取RAM用户AccessKey Secret', # 连接超时时间 单位毫秒(ms) connect_timeout=10000, # 读超时时间 单位毫秒(ms) read_timeout=3000, region_id='cn-shanghai', endpoint='green-cip.cn-shanghai.aliyuncs.com' ) clt = Client(config) # 提交任务时返回的taskId serviceParameters = { "taskId": 'fi_f_11w5THcb*******a-1xx7hH' } describeFileModerationResultRequest = models.DescribeFileModerationResultRequest( # 检测类型 service='document_detection', service_parameters=json.dumps(serviceParameters) ) try: response = clt.describe_file_moderation_result(describeFileModerationResultRequest) if response.status_code == 200: # 调用成功 # 获取审核结果 result = response.body print('response success. result:{}'.format(result)) else: print('response not success. status:{} ,result:{}'.format(response.status_code, response)) except Exception as err: print(err)
PHP SDK
支持PHP 5.6及以上版本。
执行如下命令引入相关依赖。
composer require alibabacloud/green-20220302
接入PHP SDK。
提交文档检测任务代码示例
<?php require('vendor/autoload.php'); use AlibabaCloud\SDK\Green\V20220302\Models\FileModerationRequest; use AlibabaCloud\Tea\Exception\TeaUnableRetryError; use Darabonba\OpenApi\Models\Config; use AlibabaCloud\Tea\Utils\Utils\RuntimeOptions; use AlibabaCloud\SDK\Green\V20220302\Green; $config = new Config([ /** * 阿里云账号AccessKey拥有所有API的访问权限,建议您使用RAM用户进行API访问或日常运维。 * 强烈建议不要把AccessKey ID和AccessKey Secret保存到工程代码里,否则可能导致AccessKey泄露,威胁您账号下所有资源的安全。 * 常见获取环境变量方式: * 获取RAM用户AccessKey ID:getenv("ALIBABA_CLOUD_ACCESS_KEY_ID"); * 获取RAM用户AccessKey Secret:getenv("ALIBABA_CLOUD_ACCESS_KEY_SECRET"); */ "accessKeyId" => '建议从环境变量中获取RAM用户AccessKey ID', "accessKeySecret" => '建议从环境变量中获取RAM用户AccessKey Secret', "endpoint" => "green-cip.cn-shanghai.aliyuncs.com", "regionId" => "cn-shanghai" ]); // 注意,此处实例化的client请尽可能重复使用,避免重复建立连接,提升检测性能。 $client = new Green($config); $request = new FileModerationRequest(); $request->service = "document_detection"; $serviceParameters = array("url" => "https://xxx.oss.aliyuncs.com/xxx.pdf"); $request->serviceParameters = json_encode($serviceParameters); $runtime = new RuntimeOptions(); $runtime->readTimeout = 6000; $runtime->connectTimeout = 3000; try { $response = $client->fileModerationWithOptions($request, $runtime); print_r($response->body); if (200 != $response->statusCode) { print_r("response not success. code:" . $response->statusCode); return; } $body = $response->body; print_r("requestId = " . $body->requestId . "\n"); print_r("code = " . $body->code . "\n"); print_r("message = " . $body->message . "\n"); if (200 != $body->code) { print_r("file moderation not success. code:" . $body->code); } $data = $body->data; print_r("taskId = " . $data->taskId); } catch (TeaUnableRetryError $e) { var_dump($e->getMessage()); var_dump($e->getErrorInfo()); var_dump($e->getLastException()); var_dump($e->getLastRequest()); }
获取文档检测任务结果
<?php require('vendor/autoload.php'); use AlibabaCloud\SDK\Green\V20220302\Models\DescribeFileModerationResultRequest; use AlibabaCloud\Tea\Exception\TeaUnableRetryError; use Darabonba\OpenApi\Models\Config; use AlibabaCloud\Tea\Utils\Utils\RuntimeOptions; use AlibabaCloud\SDK\Green\V20220302\Green; $config = new Config([ /** * 阿里云账号AccessKey拥有所有API的访问权限,建议您使用RAM用户进行API访问或日常运维。 * 强烈建议不要把AccessKey ID和AccessKey Secret保存到工程代码里,否则可能导致AccessKey泄露,威胁您账号下所有资源的安全。 * 常见获取环境变量方式: * 获取RAM用户AccessKey ID:getenv("ALIBABA_CLOUD_ACCESS_KEY_ID"); * 获取RAM用户AccessKey Secret:getenv("ALIBABA_CLOUD_ACCESS_KEY_SECRET"); */ "accessKeyId" => '建议从环境变量中获取RAM用户AccessKey ID', "accessKeySecret" => '建议从环境变量中获取RAM用户AccessKey Secret', "endpoint" => "green-cip.cn-shanghai.aliyuncs.com", "regionId" => "cn-shanghai" ]); // 注意,此处实例化的client请尽可能重复使用,避免重复建立连接,提升检测性能。 $client = new Green($config); $request = new DescribeFileModerationResultRequest(); $request->service = "document_detection"; $serviceParameters = array("taskId" => "fi_f_O5z5iaIis*****NYj7qa-1x****"); $request->serviceParameters = json_encode($serviceParameters); $runtime = new RuntimeOptions(); $runtime->readTimeout = 6000; $runtime->connectTimeout = 3000; try { $response = $client->describeFileModerationResultWithOptions($request, $runtime); if (200 != $response->statusCode) { print_r("response not success. code:" . $response->statusCode); return; } $body = $response->body; print_r("requestId = " . $body->requestId . "\n"); print_r("code = " . $body->code . "\n"); print_r("message = " . $body->message . "\n"); if (280 == $body->code) { print_r("processing file moderation. code:" . $body->code); return; } if (200 != $body->code) { print_r("file moderation result not success. code:" . $body->code); return; } $data = $body->data; print_r("data = " . json_encode($data) . "\n"); print_r("pageResult = " . json_encode($data->pageResult) . "\n"); } catch (TeaUnableRetryError $e) { var_dump($e->getMessage()); var_dump($e->getErrorInfo()); var_dump($e->getLastException()); var_dump($e->getLastRequest()); }
Go SDK
执行如下命令引入相关依赖。
go get github.com/alibabacloud-go/green-20220302
接入Go SDK。
提交文档检测任务代码示例
package main import ( "encoding/json" "fmt" openapi "github.com/alibabacloud-go/darabonba-openapi/v2/client" green20220302 "github.com/alibabacloud-go/green-20220302/v2/client" util "github.com/alibabacloud-go/tea-utils/v2/service" "github.com/alibabacloud-go/tea/tea" "net/http" ) func main() { // 工程代码泄露可能会导致AccessKey泄露,并威胁账号下所有资源的安全性。以下代码示例仅供参考,建议使用更安全的STS方式。 config := &openapi.Config{ /** * 阿里云账号AccessKey拥有所有API的访问权限,建议您使用RAM用户进行API访问或日常运维。 * 强烈建议不要把AccessKey ID和AccessKey Secret保存到工程代码里,否则可能导致AccessKey泄露,威胁您账号下所有资源的安全。 * 常见获取环境变量方式: * 获取RAM用户AccessKey ID:os.Getenv("ALIBABA_CLOUD_ACCESS_KEY_ID") * 获取RAM用户AccessKey Secret:os.Getenv("ALIBABA_CLOUD_ACCESS_KEY_SECRET") */ AccessKeyId: tea.String("建议从环境变量中获取RAM用户AccessKey ID"), AccessKeySecret: tea.String("建议从环境变量中获取RAM用户AccessKey Secret"), RegionId: tea.String("cn-shanghai"), Endpoint: tea.String("green-cip.cn-shanghai.aliyuncs.com"), /** * 请设置超时时间。服务端全链路处理超时时间为10秒,请做相应设置。 * 如果您设置的ReadTimeout小于服务端处理的时间,程序中会获得一个ReadTimeout异常。 */ ConnectTimeout: tea.Int(3000), ReadTimeout: tea.Int(6000), } client, _err := green20220302.NewClient(config) if _err != nil { panic(_err) } // 创建RuntimeObject实例并设置运行参数。 runtime := &util.RuntimeOptions{} runtime.ReadTimeout = tea.Int(10000) runtime.ConnectTimeout = tea.Int(10000) serviceParameters, _ := json.Marshal( map[string]interface{}{ "url": "https://xxx.oss.aliyuncs.com/xxx.pdf", }, ) request := green20220302.FileModerationRequest{ Service: tea.String("document_detection"), ServiceParameters: tea.String(string(serviceParameters)), } result, _err := client.FileModerationWithOptions(&request, runtime) if _err != nil { panic(_err) } if *result.StatusCode != http.StatusOK { fmt.Printf("response not success. status:%d\n", *result.StatusCode) return } body := result.Body fmt.Printf("response success. requestId:%s, code:%d, msg:%s\n", *body.RequestId, *body.Code, *body.Message) if *body.Code != http.StatusOK { fmt.Printf("file moderation not success. code:%d\n", *body.Code) return } data := body.Data fmt.Printf("file moderation taskId:%s\n", *data.TaskId) }
获取文档检测任务结果
package main import ( "encoding/json" "fmt" openapi "github.com/alibabacloud-go/darabonba-openapi/v2/client" green20220302 "github.com/alibabacloud-go/green-20220302/v2/client" util "github.com/alibabacloud-go/tea-utils/v2/service" "github.com/alibabacloud-go/tea/tea" "net/http" ) func main() { // 工程代码泄露可能会导致AccessKey泄露,并威胁账号下所有资源的安全性。以下代码示例仅供参考,建议使用更安全的STS方式。 config := &openapi.Config{ /** * 阿里云账号AccessKey拥有所有API的访问权限,建议您使用RAM用户进行API访问或日常运维。 * 强烈建议不要把AccessKey ID和AccessKey Secret保存到工程代码里,否则可能导致AccessKey泄露,威胁您账号下所有资源的安全。 * 常见获取环境变量方式: * 获取RAM用户AccessKey ID:os.Getenv("ALIBABA_CLOUD_ACCESS_KEY_ID") * 获取RAM用户AccessKey Secret:os.Getenv("ALIBABA_CLOUD_ACCESS_KEY_SECRET") */ AccessKeyId: tea.String("建议从环境变量中获取RAM用户AccessKey ID"), AccessKeySecret: tea.String("建议从环境变量中获取RAM用户AccessKey Secret"), RegionId: tea.String("cn-shanghai"), Endpoint: tea.String("green-cip.cn-shanghai.aliyuncs.com"), /** * 请设置超时时间。服务端全链路处理超时时间为10秒,请做相应设置。 * 如果您设置的ReadTimeout小于服务端处理的时间,程序中会获得一个ReadTimeout异常。 */ ConnectTimeout: tea.Int(3000), ReadTimeout: tea.Int(6000), } client, _err := green20220302.NewClient(config) if _err != nil { panic(_err) } // 创建RuntimeObject实例并设置运行参数。 runtime := &util.RuntimeOptions{} runtime.ReadTimeout = tea.Int(10000) runtime.ConnectTimeout = tea.Int(10000) serviceParameters, _ := json.Marshal( map[string]interface{}{ "taskId": "fi_f_O5z5iaIi******-1x****", }, ) request := green20220302.DescribeFileModerationResultRequest{ Service: tea.String("document_detection"), ServiceParameters: tea.String(string(serviceParameters)), } result, _err := client.DescribeFileModerationResultWithOptions(&request, runtime) if _err != nil { panic(_err) } if *result.StatusCode != http.StatusOK { fmt.Printf("response not success. status:%d\n", *result.StatusCode) return } body := result.Body fmt.Printf("response success. requestId:%s, code:%d, msg:%s\n", *body.RequestId, *body.Code, *body.Message) if *body.Code == 280 { fmt.Printf("processing file moderation. code:%d\n", *body.Code) return } if *body.Code != http.StatusOK { fmt.Printf("file moderation result not success. code:%d\n", *body.Code) return } data := body.Data fmt.Printf("file moderation result:%s\n", data) fmt.Printf("file moderation result pageResult:%s\n", data.PageResult) fmt.Printf("file moderation result dataId:%s\n", data.DataId) }