首页 文章

从Firebase函数在实时数据库中设置子值时无法获取URI

提问于
浏览
0

我正在从Node.js中的Firebase函数设置Firebase实时数据库中的值 . 函数将base64映像发送到API,并将API返回的数据存储在实时数据库中 . 我的所有服务帐户都设置为编辑角色 . 当我的代码尝试设置该错误时,控制台中会显示此错误:

Could not fetch URI /computeMetadata/v1beta1/instance/service-accounts/default/token

at Request._callback (/user_code/node_modules/@google-cloud/storage/node_modules/google-auth-library/lib/transporters.js:100:13)
at Request.self.callback (/user_code/node_modules/@google-cloud/storage/node_modules/google-auth-library/node_modules/request/request.js:187:22)
at emitTwo (events.js:106:13)
at Request.emit (events.js:191:7)
at Request.<anonymous> (/user_code/node_modules/@google-cloud/storage/node_modules/google-auth-library/node_modules/request/request.js:1044:10)
at emitOne (events.js:96:13)
at Request.emit (events.js:188:7)
at IncomingMessage.<anonymous> (/user_code/node_modules/@google-cloud/storage/node_modules/google-auth-library/node_modules/request/request.js:965:12)
at emitNone (events.js:91:20)
at IncomingMessage.emit (events.js:185:7)
at endReadableNT (_stream_readable.js:974:12)
at _combinedTickCallback (internal/process/next_tick.js:80:11)
at process._tickDomainCallback (internal/process/next_tick.js:128:9)

我的节点代码是:

const request = require('request-promise');
const gcs = require('@google-cloud/storage')();
const path = require('path');
const os = require('os');
const fs = require('fs');
const firebase = require('firebase');

exports.identifyUpdate = functions.storage.object().onFinalize((object) => {

    const fileBucket = object.bucket;
    const filePath = object.name;
    const contentType = object.contentType;
    const fileName = path.basename(filePath);

    if(!filePath.substring(0,filePath.indexOf('/')) == 'updates') {
        console.log("Triggered by non-update photo")
        return null;
    }

    console.log("Update photo added")

    // Create Firebase app (for Realtime Database access)

    var config = {
        apiKey: "xxxxx",
        authDomain: "xxxxx",
        databaseURL: "xxxxx",
        storageBucket: "xxxxx",
    };

    if(!firebase.apps.length) {
        firebase.initializeApp(config);
    }

    // Trace back to Update stored in Realtime Database

    const database = firebase.database().ref()
    const pendingRef = database.child('pendingUpdates')

    console.log(filePath)

    const splitPath = filePath.split(path.sep)

    const patientID = splitPath[1]
    console.log('Patient ID: ' + patientID)

    const updateID = splitPath[2]
    console.log('Update ID: ' + updateID)

    const updateRef = pendingRef.child(patientID).child(updateID)

    console.log('Found Update reference')

    const photoRef = updateRef.child('photoURLs').child(fileName)

    console.log('Photo Reference: ' + photoRef)

    // Download and convert image to base64

    const bucket = gcs.bucket(fileBucket)
    const tempFilePath = path.join(os.tmpdir(), fileName)
    const metadata = {
        contentType: contentType
    };

    var base64;

    return bucket.file(filePath).download({
        destination: tempFilePath
    }).then(() => {
        console.log('Image downloaded locally to', tempFilePath)
    }).then(() => {

        base64 = base64_encode(tempFilePath)
        console.log("Base 64: " + base64)

    }).then(() => {
    // Send image data to Kairos

        var options = {
            method: 'POST',
            uri: 'https://api.kairos.com/recognize',
            body: {
                'image': base64,
                'gallery_name': 'gallerytest1'
            },
            headers: {
                'app_id': 'xxxxx',
                'app_key': 'xxxxx'
            },
            json: true
        }

        return new Promise (() => {
            console.log(options)
            request(options)
            .then(function(repos) {

                console.log('API call succeeded');

                console.log('Kairos response: ' + repos);

                const apiResult = repos['images']
                console.log("Result \n" + JSON.stringify(apiResult))

                const faceData = face_data(apiResult)

                console.log("face data\n" + JSON.stringify(face_data(apiResult)))

                const photoURL = photoRef.once('value')
                console.log(photoURL)
                updateRef.child('faceData').set({photoURL : faceData})

            }) 
            .catch(function(err) {
                console.log(err)
            })
        });

    })

    // Delete app instance (to prevent concurrency leaks)

    const deleteApp = () => app.delete().catch(() => null);
    deleteApp.call

})

function base64_encode(file) {
    // read binary data
    var bitmap = fs.readFileSync(file);
    // convert binary data to base64 encoded string
    return new Buffer(bitmap).toString('base64');
}

function face_data(response) {

    var faceData = {};

    for(i = 0; i < response.length; i++) {

        const face = response[i]["transaction"];
        const id = face["subject_id"]
        const topLeftX = face["topLeftX"]
        const topLeftY = face["topLeftY"]
        const width = face["width"]
        const height = face["height"]

        faceData[i] = {
            'face_id': id,
            'topLeftX': topLeftX,
            'topLeftY': topLeftY,
            'width': width,
            'height': height
        }

        console.log(faceData[i])        

    }

    return faceData

}

1 回答

  • 1

    您是否处于付费计划(Flame或Blaze)?如果不是,则无法从 Cloud 功能调用外部服务 .

    请参阅https://firebase.google.com/pricing/,其中解释了"The Spark plan allows outbound network requests only to Google-owned services"(您必须将鼠标悬停在接近"Cloud Functions" Headers 的问号上) .

相关问题