用于Firebase的云功能图像下载功能错误

时间:2017-06-05 12:38:08

标签: node.js firebase firebase-authentication google-cloud-functions node-request

我正在使用Firebase和Firebase的新功能Cloud Functions构建Web应用程序。我创建了一个函数,它使用一个URL并使用节点模块请求和request-promise-native将图像下载到一个64位编码的字符串中,如下所示:

module.exports = {
downloadImageFromUrl: function (url) {
    var options = {
        method: 'GET',
        uri: url,
        resolveWithFullResponse: true,
        simple: false,
        family: 4
    };
    return rp.get(options)
        .then(function (res) {
            return "data:" + res.headers["content-type"] + ";base64," + new Buffer(res.body).toString('base64');
        })
        .catch(function (error) {
            console.log("ERROR GETTING image", error);
            return error;
        });
    }
};

top函数在本地完美运行,但一旦在firebase上它就会出错:

RequestError: Error: getaddrinfo EAI_AGAIN lh6.googleusercontent.com:443
at new RequestError (/user_code/node_modules/request-promise/node_modules/request-promise-core/lib/errors.js:14:15)
at Request.plumbing.callback (/user_code/node_modules/request-promise/node_modules/request-promise-core/lib/plumbing.js:87:29)
at Request.RP$callback [as _callback] (/user_code/node_modules/request-promise/node_modules/request-promise-core/lib/plumbing.js:46:31)
at self.callback (/user_code/node_modules/request/request.js:188:22)
at emitOne (events.js:96:13)
at Request.emit (events.js:188:7)
at Request.onRequestError (/user_code/node_modules/request/request.js:884:8)
at emitOne (events.js:96:13)
at ClientRequest.emit (events.js:188:7)
at TLSSocket.socketErrorListener (_http_client.js:310:9)
at emitOne (events.js:96:13)
at TLSSocket.emit (events.js:188:7)
at connectErrorNT (net.js:1020:8)
at _combinedTickCallback (internal/process/next_tick.js:74:11)
at process._tickDomainCallback (internal/process/next_tick.js:122:9)

我在如下创建用户时调用firebase auth触发器中的函数:

exports.createUser = functions.auth.user().onCreate(event => {
    if (event.data.photoURL) {
    utils.downloadImageFromUrl(event.data.photoURL)
        .then(function(res){
            console.log("User Photo", res);

        })
        .catch(function(error){
            console.log("Error", error);
        })
}
});

非常感谢任何帮助。

1 个答案:

答案 0 :(得分:0)

如果这是答案,还不完全确定,但在阅读文档后,我阅读了他们的免费计划,该计划表明你无法提出任何约束请求。所以我想从Url获​​取图像算作出站请求。在我开始支付他们的服务费用之后,我会回来验证这是否是问题。