使用的是阿里云文档
获取到 对应的accessKeyId和 accessKeySecret
新建test.js 和 green-nodejs-invoker.js 文件 这两个文件要在同一目录下,不然引入报错
test.js代码如下:
'use strict';
const uuidV1 = require('uuid/v1');
const greenNodejs = require('./green-nodejs-invoker.js');
// 默认参数
const accessKeyId = '<your accessKeyId>';
const accessKeySecret = '<your accessKeySecret>';
const greenVersion = '2017-01-12';
const hostname = 'green.cn-shanghai.aliyuncs.com';
const path = '/green/image/scan'; // 需要请求的接口
const clientInfo = {
'ip': '127.0.0.1',
};
// 测试调用
(async () => {
// 请求体,根据需要调用相应的算法
let requestBody = JSON.stringify({
'bizType': 'Green',
'scenes': [ 'ocr' ], // 指定检测场景 ocr 图片文字识别 //支持多种形式
'tasks': [{ // 指定检测对象,JSON数组中的每个元素是一个OCR图文检测任务结构体(image表)。最多支持10个元素,即对10张图片进行识别
'dataId': uuidV1(),
'url': 'https://dss2.bdstatic.com/8_V1bjqh_Q23odCf/pacific/1939251838.png',
}, { // 支持多个参数
'dataId': uuidV1(),
'url': 'https://dss3.baidu.com/-rVXeDTa2gU2pMbgoY3K/it/u=842202544,1222336704&fm=202&mola=new&crop=v1',
}],
});
let bizCfg = {
'accessKeyId': accessKeyId,
'accessKeySecret': accessKeySecret,
'path': path,
'clientInfo': clientInfo,
'requestBody': requestBody,
'hostname': hostname,
'greenVersion': greenVersion,
};
let result = await greenNodejs.getImageInfo(bizCfg);
console.log('result==========>', result);
})();
green-nodejs-invoker.js代码如下:
'use strict';
const http = require('http');
const crypto = require('crypto');
const uuidV1 = require('uuid/v1');
module.exports = {
async getImageInfo(bizCfg, callback) {
try {
let path = bizCfg['path'];
let clientInfo = bizCfg['clientInfo'];
let requestBody = bizCfg['requestBody'];
let greenVersion = bizCfg['greenVersion'];
let hostname = bizCfg['hostname'];
let gmtCreate = new Date().toUTCString();
let md5 = crypto.createHash('md5');
// 请求头
let requestHeaders = {
'Accept': 'application/json',
'Content-Type': 'application/json',
'Content-MD5': md5.update(requestBody).digest().toString('base64'),
'Date': gmtCreate,
'x-acs-version': greenVersion,
'x-acs-signature-nonce': uuidV1(),
'x-acs-signature-version': '1.0',
'x-acs-signature-method': 'HMAC-SHA1',
};
// 对请求的签名
signature(requestHeaders, bizCfg);
// HTTP请求设置
let options = {
'hostname': hostname,
'port': 80,
'path': encodeURI(path + '?clientInfo=' + JSON.stringify(clientInfo)),
'method': 'POST',
'headers': requestHeaders,
};
let result = await new Promise((resolve, reject) => {
let chunks = [];
let req = http.request(options, function(res) {
// res.setEncoding('utf8');
res.on('error', function(error) {
console.log(error);
});
res.on('data', function(chunk) {
chunks.push(chunk);
});
res.on('end', function() {
let data = Buffer.concat(chunks).toString();
return resolve(data);
});
});
req.on('error', function(e) {
console.log('err===>', e);
return reject(e);
});
// 将数据写入请求体
req.write(requestBody);
req.end();
});
return result;
} catch (error) {
return error;
}
},
};
// 签名
function signature(requestHeaders, bizCfg) {
let accessKeyId = bizCfg['accessKeyId'];
let accessKeySecret = bizCfg['accessKeySecret'];
let path = bizCfg['path'];
let clientInfo = bizCfg['clientInfo'];
let signature = [];
signature.push('POST\n');
signature.push('application/json\n');
signature.push(requestHeaders['Content-MD5'] + '\n');
signature.push('application/json\n');
signature.push(requestHeaders['Date'] + '\n');
signature.push('x-acs-signature-method:HMAC-SHA1\n');
signature.push('x-acs-signature-nonce:' + requestHeaders['x-acs-signature-nonce'] + '\n');
signature.push('x-acs-signature-version:1.0\n');
signature.push('x-acs-version:2017-01-12\n');
signature.push(path + '?clientInfo=' + JSON.stringify(clientInfo));
let authorization = crypto.createHmac('sha1', accessKeySecret)
.update(signature.join(''))
.digest()
.toString('base64');
requestHeaders.Authorization = 'acs ' + accessKeyId + ':' + authorization;
}
返回结果如下: