我正在尝试使用 Cloud Functions for Firebase 构建一个与 Google Cloud SQL (PostgreSQL) 实例对话的 API。
我正在使用 HTTP(S) 触发器。
当我将桌面的 IP 地址列入白名单时,我可以使用本地计算机上函数的 node.js 代码连接到 Cloud SQL。但是当我部署时,我无法连接,我无法弄清楚 Firebase Function 服务器的 HOST IP 地址,要列入白名单。
您如何从 Cloud Functions for Firebase 与 Google Cloud SQL 对话?
谢谢!
// Code Sample, of what's working on Localhost.
var functions = require('firebase-functions');
var pg = require('pg');
var pgConfig = {
user: functions.config().pg.user,
database: functions.config().pg.database,
password: functions.config().pg.password,
host: functions.config().pg.host
}
exports.helloSql = functions.https.onRequest((request, response) => {
console.log('connecting...');
try {
client.connect(function(err) {
if (err) throw err;
console.log('connection success');
console.log('querying...');
client.query('SELECT * FROM guestbook;', function(err, result){
if (err) throw err;
console.log('querying success.');
console.log('Results: ', result);
console.log('Ending...');
client.end(function(err){
if (err) throw err;
console.log('End success.');
response.send(result);
});
});
});
} catch(er) {
console.error(er.stack)
response.status(500).send(er);
}
});
原文由 Quang Van 发布,翻译遵循 CC BY-SA 4.0 许可协议
新答案:
查看其他答案,现已正式支持。 https://cloud.google.com/functions/docs/sql
旧答案:
目前不可能。然而,这是问题跟踪器 #36388165 上的功能请求:
如果这对您来说是一个重要的功能,我建议您访问问题跟踪器并为功能请求加注星标,以帮助它获得普及。