开发者社区> 问答> 正文

阿里云OpenAPI 看起来也没有,为啥这个10s不生效呢?

阿里云OpenAPI 看起来也没有,为啥这个10s不生效呢?

import ocr_api20210707, as $ocr_api20210707 from '@alicloud/ocr-api20210707';
import
as $OpenApi from '@alicloud/openapi-client';
import as $Util from '@alicloud/tea-util';
import Stream from '@alicloud/darabonba-stream';
import
as $tea from '@alicloud/tea-typescript';
import axios from "axios";
export class OCRService {
static createClient(): ocr_api20210707 {
let config = new $OpenApi.Config({
accessKeyId: 'xxxxx',
accessKeySecret: 'xxxxx',
opts: {
connectTimeout: 10000,
readTimeout: 10000,
timeout: 10000
}
});
config.endpoint = ocr-api.cn-hangzhou.aliyuncs.com;
return new ocr_api20210707(config);
}

async recognizeHKIdCard(base64ImageUrl: string) {
let client = OCRService.createClient();
const preSignedUrl = Buffer.from(base64ImageUrl, 'base64').toString('ascii');
const response = await axios.get(preSignedUrl, {
responseType: 'arraybuffer',
});

const imageBuffer = Buffer.from(response.data);

//fix China block the site URL
let bodyStream = Stream.readFromBytes(imageBuffer);
let recognizeHKIdcardRequest = new $ocr_api20210707.RecognizeHKIdcardRequest({
body: bodyStream,
});

let runtime = new $Util.RuntimeOptions({ });
try {
const response = await client.recognizeHKIdcardWithOptions(recognizeHKIdcardRequest, runtime);
const { data } = response.body;
return {
headers: {
'Content-Type': 'application/json'
},
statusCode: 200,
body: data
}
} catch (error) {
return {
headers: {
'Content-Type': 'application/json'
},
statusCode: 500,
body: JSON.stringify(error)
}
}
}

}

展开
收起
夹心789 2024-06-10 20:13:20 22 0
1 条回答
写回答
取消 提交回答
问答排行榜
最热
最新

相关电子书

更多
AutoTalk第七期:自动化工具-OpenAPI在线调试 立即下载
最大化阿里云OpenAPI能力的方法和实践 立即下载
低代码开发师(初级)实战教程 立即下载