Я устанавливаю значение в базе данных Firebase Realtime из функции Firebase в Node.js. Функция отправляет изображение 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
}