简体   繁体   中英

Cloud Functions for Firebase Image download function Error

I am building a web application using Firebase and the new feature Cloud Functions for Firebase. I have created a function that takes a URL and downloads the image into a 64-bit encoded string as below using the node modules request and request-promise-native:

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

The top function works perfectly running locally but once on firebase it gives the error:

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)

I am calling the function in the firebase auth trigger when a user is created as below:

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

Any help would be greatly appreciated.

Not entirely sure yet if this is the answer, but after reading the documentation, I read their free plan which says you cannot make any out bound requests. So I guess getting an image from a Url counts as an outbound request. After I start paying for their service, I will come back to verify if this was the problem.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM