Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

google function: accessing firestore database of another project

I would like a Google Cloud project A (project-a-id) to access the firestore data of another Google Cloud project B (project-b-id). For the same I added project A default service account viz. [email protected] in the IAM of project B and set the role to Cloud Filestore Editor.

In the cloud function of project A, I am trying to access both project A's (its own) firestore as well as project B's firestore but it keeps showing project A default database for both Apps. The code is:

var primaryAppConfig = {
  databaseURL: 'https://project-a-id.firebaseio.com'
};
var primaryApp = admin.initializeApp(primaryAppConfig, 'primary');
var primarydb = admin.firestore(primaryApp);

var secondaryAppConfig = {
  databaseURL: 'https://project-b-id.firebaseio.com'
};
var secondaryApp = admin.initializeApp(secondaryAppConfig, 'secondary');
var secondarydb = admin.firestore(secondaryApp);

I was under the impression if the default service account of project-a is given rights in project-b it should automatically get rights. At least I found it applicable when I am accessing google cloud storage buckets in this manner.

Is something else to be done? Thanks

like image 434
RmR Avatar asked Mar 09 '19 13:03

RmR


People also ask

How do I trigger a Firebase function?

Cloud Firestore triggersonCreate : triggered when a document is written to for the first time. onUpdate : triggered when a document already exists and has any value changed. onDelete : triggered when a document with data is deleted. onWrite : triggered when onCreate, onUpdate or onDelete is triggered.


1 Answers

I have a cloud-native firestore as opposed to a real-time database in project-a. However, was facing the same issue when I tried to access it from project-b.
Was able to solve it by generating a service account with access to project-a firestore, downloading the credentials and accessing the same from project-b with the following:

credential_path = "pathTo/xxxxx.json"
db = firestore.Client.from_service_account_json(credential_path)
like image 174
Ganesh Parkar Avatar answered Oct 21 '22 10:10

Ganesh Parkar