使用Nodejs作为后端,自动提交Altizure任务.
本例图像放置于images文件夹下
const { GraphQLClient } = require('graphql-request'), shasum = require('shasum'), OSS = require('ali-oss'), Promise = require("bluebird"); const path = require('path'), fs = require('fs'); const projectName = 'MyDEMO' // 获取token和key,这里我是在浏览器手动获取的 const client = new GraphQLClient('https://api.altizure.cn/graphql', { headers: { altitoken: '<应用token>', key: '<你的key>' } }) let PID //创建任务,因为支持同名任务需要注意不要反复运行 client.request(`mutation { createProject(name: "${projectName}", type: pro) { id } }`) .then(data => { PID = data.createProject.id; console.log(`PID:${PID}`) // 获取最近的阿里云对象存储 return client.request(`{ getGeoIPInfo{ nearestBuckets { bucket, } } }`) }) .then(async data => { //数据上传是比较繁琐的一部分,共享的变量比较多,就不拆分了,直接在卸载一起 let bucketPlace = data.getGeoIPInfo.nearestBuckets[0].bucket //取得阿里云对象存储STS let result = await client.request(`mutation { uploadImageOSS(pid: "${PID}", bucket: ${bucketPlace}, filename: "") { sts { id, secret, token, bucket, region, endpoint }, } }`) let { uploadImageOSS: { sts: { id: accessKeyId, region, bucket, secret, token } } } = result //实例化一个对象存储客户端 let ossClient = new OSS({ region, accessKeyId, accessKeySecret: secret, bucket, stsToken: token }); //获取本目录下images文件夹下的所有图片 let images = fs.readdirSync('./images') // 影像一般较大,所以一次上传一个就够了,不需要多个上传并行 return Promise.each(images, imagePath => { let _imagePath = path.join('./images', imagePath) let image = fs.readFileSync(_imagePath) // 计算sha1sum let sha1sum = shasum(image) //检测在本项目中是否存在 return client.request(`mutation { hasImage(pid:"${PID}",checksum: "${sha1sum}") { id, name, state, error } }`) .then(result => { //存在的直接跳过 if (result.hasImage && result.hasImage.state == "Ready") throw 'updated' }) //获取上传影像的元数据 .then(() => client.request(`mutation { uploadImageOSS(pid: "${PID}", bucket: ${bucketPlace}, filename: "${imagePath}") { image { filename, id } } }`)) .then(async result => { let { uploadImageOSS: { image: { filename, id } } } = result //上传前 await client.request(`mutation { startImageUpload(id: "${id}") { id } }`) //上传到对象存储 await ossClient.put(path.join(PID, filename), _imagePath) //完成上传 await client.request(`mutation { doneImageUpload(id: "${id}"){ name } }`) }).catch(() => {}) }) }) // 预约自动开始 .then(() => client.request(`mutation { preStartReconstruction(id: "${PID}",options:{}) }`)) // 没什么用,这里可以检测到错误代码02,意味正在进行图像验证 .then(() => client.request(`mutation { startReconstructionWithError(id: "${PID}") { error { code } task { id state } } }`)) .catch(err => { console.log(err) process.exit(1) })