When I start the emulators, the Storage emulator starts and is available at localhost:9199.
Then I access storage with the admin SDK in node.js with:
serviceAccountKey = require('./serviceAccountKey.json');
const config = {
credential: admin.credential.cert(serviceAccountKey),
};
const adminApp = admin.initializeApp(config);
const storage = adminApp.storage();
Now storage points to the emulator, not the real service as before this emulator has been implemented.
How can I get access to the real service?
I have tried setting projectId and the env variable as described here, but it does not work.
OK, so the way is simply to start all services but storage from the command line, and not from firebase.json, this way:
firebase emulators:start --only hosting,functions,firestore,auth,ui
Running firebase emulators:start will start all services in an emulated state. If you want to talk to a production service, you can run firebase serve, or if you want some services emulated but not storage, you can run firebase serve --only {services,to,emulate} (eg, firebase serve --only functions,hosting. This would emulate functions and hosting, but any call to storage (or auth or other non-emulated services) would talk to the live Firebase environment.
Related
I'm learning to set up Firebase Emulators correctly to work on my projects and I came up with a problem. I can setup the emulators and make them work locally, however, when trying to access firestore it seems to try to access the real Firestore Instance instead of the emulator.
Right now I'm initializing the app like this (in Cloud Functions)
admin.initializeApp();
const db = admin.firestore();
But when I'm running a function I'm getting:
Failed to initialize and load triggers. This shouldn't happen: Failed to read credentials from file GOOGLE_APPLICATION_CREDENTIALS.json: Error: ENOENT: no such file or directory, open 'GOOGLE_APPLICATION_CREDENTIALS.json'
The thing is that if I use the credentials I generated for my project it will work with the real Firestore instance instead of the emulator.
How should I make credentials for my emulated services?
If you are using Firebase Functions emulator as well then Admin SDK will connect to all the running emulators e.g. if only Auth emulator is running then it'll use the emulator and connect to production for other services like Firestore. You can explicitly set the FIRESTORE_EMULATOR_HOST environment variable and Admin SDKs will use the emulator then.
Checkout the documentation for more information.
Hi i am having firebase cloud functions, When i run firebase emulator in local they are not displaying in emulator.below is my functions code
const functions = require("firebase-functions");
const admin = require("firebase-admin");
const DomParser = require('dom-parser')
admin.initializeApp(functions.config().firebase);
exports.storeBooking = functions
.region("europe-west1")
.pubsub.topic("responses")
.onPublish((message) => {
/// some logic
});
when i run emulator must show function storeBooking, But in emulator it is showing as below
Watching "D:\backend-functions\booking" for Cloud Functions...
function ignored because the pubsub emulator does not exist or is not running.
function ignored because the pubsub emulator does not exist or is not running.
The emulator will show like this:
Watching "D:\backend-functions\booking" for Cloud Functions…
function ignored because the pubsub emulator does not exist or is not running.
function ignored because the pubsub emulator does not exist or is not running.
Which means function was found but for some reason firebase cannot connect to pubsub emulator, despite all the configs.
As per the documentation, you need to set up admin credentials to test your functions to interact with Google APIs or other Firebase APIs via the Firebase Admin SDK.
The cloud pubsub requires the setup steps described in this section. This applies whether you're using the Cloud Functions shell or firebase emulators:start.
You can follow the instructions To set up admin credentials for emulated functions from documentation.
For more information you can check with this Stackoverflow Link.
Due to problems using a Realtime Database emulator for development in Flutter (see How do I connect to my local Realtime Database emulator in my Flutter app?), I am now simply using my non-local Realtime Database for development. However, I am also using Cloud Functions, and I have created a Functions emulator.
I tried the following to connect to it, based on this StackOverflow-answer:
const functions = require("firebase-functions");
const admin = require("firebase-admin");
functions.config = () => {
return {
firebase: {
databaseURL: "https://mylink.firebaseio.com/",
},
};
};
But the emulator is not responding to changes in the database.
So, is it even possible to connect my local Functions emulator to my non-local Realtime Database? It's kind of a weird setup, but I don't know how I am going to be able test functions otherwise.
It's not possible to have the local Cloud Functions emulator respond to (or "trigger" on) events in a production database. You have to deploy your functions in order for them to execute in response to change in the cloud-hosted database. The local functions emulator only responds to changes in the locally emulated database.
Today i ran into a problem with my Firebase Cloud Function that generates images sizes when new images are being uploaded to the Firebase Storage. This problem causes my quota to exceed after a couple uploads and when the quota exceeds the limit i am not able to use firebase anymore. Now i am trying to run this Cloud Function locally so that my quota will not exceed.
I tried running the emulator in my terminal with this command: firebase emulators:start
This initializes all my Cloud Functions except my Firebase Auth and Firebase Storage functions. The firebase docs say the emulator only supports:
HTTPS functions
Callable functions
Cloud Firestore functions
Is there a way/work around so i can run my Firebase Storage and Firebase Auth functions locally?
From the emulator, no. As you've seen, they're just not supported.
Look into invoking functions using the functions shell, or unit testing your functions with the functions-test module.
I'm building a firebase API with cloud functions and want to reach my production Database for local testing.
My problem :
When I launch my two firebase emulator (functions and firestore) with
firebase emulators:start
1- My custom API endpoints run on http://localhost:5001 (this works)
2- My triggers reaches a local dababase on http://localhost:8080 whereas I didn't set FIRESTORE_EMULATOR_HOST=localhost:8080 (I want to reach my production database)
OR
When I launch only my "functions emulator" with
firebase emulators:start --only function
...but my triggers are not reached, probably because of this warning
i functions[userOnCreate]: function ignored because the firestore emulator does not exist or is not running.
On the other side I'm building a reactjs App also with firebase running on localhost:3000
I call my local API from this app with firebase SDK. To reach my emulator, I add the line :
firebase.functions.useFunctionsEmulator('http://localhost:5001');
The local emulators don't work with actual cloud-hosted instances of databases. Everything must be local.
You are always free to file a feature request.
you have to use firebase serve instead for firebase emulators:start