Altizure Node.js後端DEMO

使用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)
    })
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章