Node.js Firebase函数将Base64图像发送到外部API
我在Node.js中使用带有存储触发器的Firebase函数将上传的图像数据发送到上传照片的外部API端点 我目前正在拍摄上传到Firebase存储中的bucket中的图像,将其转换为base64字符串,并将其插入我的字典中以供请求 我目前的问题是,这本词典似乎被删节了。我查看了Firebase控制台上的控制台日志,它似乎在base64变量之后结束 我不确定这是一个语法错误,还是我使用base64的方式,还是Firebase函数的错误。如果有人知道会发生什么,请告诉我Node.js Firebase函数将Base64图像发送到外部API,node.js,firebase,base64,google-cloud-functions,firebase-storage,Node.js,Firebase,Base64,Google Cloud Functions,Firebase Storage,我在Node.js中使用带有存储触发器的Firebase函数将上传的图像数据发送到上传照片的外部API端点 我目前正在拍摄上传到Firebase存储中的bucket中的图像,将其转换为base64字符串,并将其插入我的字典中以供请求 我目前的问题是,这本词典似乎被删节了。我查看了Firebase控制台上的控制台日志,它似乎在base64变量之后结束 我不确定这是一个语法错误,还是我使用base64的方式,还是Firebase函数的错误。如果有人知道会发生什么,请告诉我 const request
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: "[apikey]",
authDomain: "[PROJECT_ID].firebaseapp.com",
databaseURL: "https://[PROJECT_ID].firebaseio.com",
storageBucket: "[PROJECT_ID].appspot.com",
};
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': '[id]',
'app_key': '[key]'
},
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']['transaction']['subject_id']
console.log("Transaction " + JSON.stringify(apiResult))
})
.catch(function(err) {
console.log('API call failed')
})
});
})
// 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');
}
图像输出:
const apireult=repos['images']['transaction']['subject\u id']
可能会抛出错误。你知道你在干什么吗?他们似乎都是200岁。如果您调用了console.log(err)
,而不是console.log('API调用失败”)
,您可能会获得更多信息,因为您刚刚意识到['images']['transaction']['subject\u id']不是我试图获取的信息的正确路径。谢谢你的建议@JamesPoag!直到我把错误打印出来,我才知道我犯了什么错误。