I'm having some trouble with serving some dynamic content from Firebase Hosting.
I've written an http.onRequest()
cloud function that returns an image (content-type: image/jpeg) as its response. The function works as expected if I access it directly at its url:
https://us-central1-my-project-id.cloudfunctions.net/hosting-getPartnerImg
Per the documentation, I am using the us-central1
region.
I would like to be able to invoke this function using Firebase Hosting as well, which I've configured as follows:
firebase.json (snippet)
"rewrites" : [
{
"source" : "/pimg",
"function" : "hosting-getPartnerImage"
}
],
"headers": [
{
"source": "/pimg",
"headers": [ {
"key": "Cache-Control",
"value": "max-age=60"
},
{
"key": "Access-Control-Allow-Origin",
"value": "*"
}
]
}]
index.js (snippet)
const functions = require('firebase-functions');
const admin = require('firebase-admin');
let firebaseDefaultConfig = JSON.parse(process.env.FIREBASE_CONFIG);
admin.initializeApp(firebaseDefaultConfig);
const fn = process.env.FUNCTION_NAME;
if(!fn || fn === 'hosting-getPartnerImg'){
exports.hosting = require('./hosting.js');
}
hosting.js
const functions = require('firebase-functions');
const admin = require('firebase-admin');
exports.getPartnerImg = functions.region("us-central1").https.onRequest((req, res) => {
const partnerId = req.query.partner;
const fileName = req.query.file;
res.set("content-type", "image/jpeg");
const bucket = admin.storage().bucket();
let file = bucket.file("partnerImgs/" + partnerId + "/" + fileName);
let readStream = file.createReadStream();
readStream.pipe(res);
});
This one has me stumped. Navigating to a URL like:
https://my-project-id.web.app/pimg?partner=BLAH&file=foo.jpg
does not generate a Page Not Found as other URLs, so I'm reasonably confident that the rewrite is taking hold as it should. Be that the case though, why am I immediately taken to:
appengine.google.com/_ah/loginform...
with the message:
** An application is requesting permission to your Google Account **
Can't HTTP onRequest
cloud functions be used anonymously via Firebase Hosting? Why does the function work when I hit it directly, yet requests permission when I access it via the Firebase Hosting rewrite.
Any ideas would be appreciated.
index.js
in this case? Is it the source that is in the public directory? – robsiembhosting-getPartnerImg
and show the code for that? – robsiembindex.js
is in the functions directory. I put it in as a snippet as it has many statements similar to the conditional shown to segment individual functions into groups. When this pattern is used, theprocess.env.FUNCTION_NAME
becomeshosting-getPartnerImg
rather thanhosting.getPartnerImg
, so yes, I have deployed that function, and it is working fine if I access it directly. – HondaGuygetPartnerImg
function out of thehosting
group. No dice. Then, I changed the name fromgetPartnerImg
togetpartnerimg
, and it worked! It was the character casing! You were right about it not being able to find the function - it couldn't, due to the fact that I had camel-cased the function name. Took forever to crack. Thanks for providing a crucial hint! Many thanks! – HondaGuy