如何从 Cloud Functions 连接 Google Cloud SQL?

编程入门 行业动态 更新时间:2024-10-22 14:34:04
本文介绍了如何从 Cloud Functions 连接 Google Cloud SQL?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我正在尝试使用 Cloud Functions for Firebase 来构建一个与Google Cloud SQL (PostgreSQL) 实例.

I am trying to use Cloud Functions for Firebase to build an API that talks with a Google Cloud SQL (PostgreSQL) instance.

我正在使用 HTTP(S) 触发器.

I am using HTTP(S) trigger.

当我将桌面的 IP 地址列入白名单时,我可以从本地计算机使用函数的 node.js 代码连接到 Cloud SQL.但是当我部署时,我无法连接,而且我无法弄清楚 Firebase Function 的服务器的 HOST IP 地址,以加入白名单.

When I white-list my desktop's IP address, I can connect to the Cloud SQL with the function's node.js code from my local machine. But when I deploy, I can't connect, and I can't figure out the HOST IP address of Firebase Function's server, to white-list.

您如何通过 Cloud Functions for Firebase 与 Google Cloud SQL 对话?

How do you talk to Google Cloud SQL from Cloud Functions for Firebase?

谢谢!

// 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); } });

推荐答案

新答案:

查看其他答案,现已正式支持.cloud.google/functions/docs/sql

See other answers, it's now officially supported. cloud.google/functions/docs/sql

旧答案:

目前不可能.然而,这是对问题跟踪器的功能请求 #36388165:

It's not currently possible. It is however a feature request on the issue tracker #36388165:

目前无法从 Cloud Functions 连接到 Cloud SQL支持,因为 UNIX 套接字不存在(导致 ENOENT)和没有定义的 IP 范围要列入白名单(导致 ETIMEDOUT).一可能是将 Cloud SQL 实例中的 0.0.0.0/0 列入白名单,但出于安全原因,不建议这样做.

Connecting to Cloud SQL from Cloud Functions is currently not supported, as the UNIX socket does not exist (causing ENOENT) and there is no defined IP range to whitelist (causing ETIMEDOUT). One possibility is to whitelist 0.0.0.0/0 from the Cloud SQL instance but this is not recommended for security reasons.

如果这对您来说是一个重要的功能,我建议您访问 issuetracker 并为功能请求加注星标,以帮助它获得人气.

If this is an important feature for you I would suggest you visit the issuetracker and star the feature request to help it gain popularity.

更多推荐

如何从 Cloud Functions 连接 Google Cloud SQL?

本文发布于:2023-10-24 19:48:13,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1524838.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:Functions   Cloud   SQL   Google

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!