How to connect to Cloud Firestore from Google Cloud Functions with python code

Introduction

We have summarized the correspondence method when you want to connect from python code and Google Cloud Functions instead of Google Cloud Functions for Firebase.

It doesn't say that it will pull the credentials from Firebase, so please refer to other articles.

environment

Google Cloud Functions made with python 3.7

firebase-admin 3.2.1

Limitations when writing python code with Google Cloud Functions

It seems that it only accepts main.py and requirements.txt. I couldn't read the usual serviceAccountKey.json file. It's a common way to authenticate by putting the file path in credentials.Certificate.

Create credentials.Certificate from something other than a file

So I searched for other methods.

First, I checked the API specifications. https://firebase.google.com/docs/reference/admin/python/firebase_admin.credentials

A credential initialized from a JSON certificate keyfile.

So it seems that it is the one to do from the key file. By the way, from the javascript side, the API specification describes how to accept from files other than files, so if you are using javascript, please see the API specification.

I wondered what kind of processing it was, so I went to see the source. https://github.com/firebase/firebase-admin-python/blob/master/firebase_admin/credentials.py

#Quoted from line 81 of the above URL
if isinstance(cert, str):
    with open(cert) as json_file:
    json_data = json.load(json_file)
elif isinstance(cert, dict):
    json_data = cert

Can you do it? So if you make it a dict type and hand it over, it will read it. You read the source.

Let's implement it

In my case, I publish this code on github, so I set the contents of serviceAccountKey.json as it is in the environment variable and load it. It's okay for people to embed it in python code because it's not published, but be careful when handling it as it's bad if it leaks.

The environment variable part uses python-dotenv, but I will omit it.

The version extracted from the client creation code is as follows.

import os
import json
import firebase_admin
from firebase_admin import firestore
from firebase_admin import credentials

cred = credentials.Certificate(json.loads(os.environ.get("FIREBASE_KEY")))
firebase_admin.initialize_app(cred)
db = firestore.client()

Dependent libraries

firebase-admin
google-cloud-firestore

There are two, so please put them in. Only firebase-admin appears in the code, but both are needed because firebase-admin depends on google-cloud-firestore.

in conclusion

Google Cloud Functions For Firebase now supports python (then there was nothing wrong)

Recommended Posts

How to connect to Cloud Firestore from Google Cloud Functions with python code
How to update Google Sheets from Python
Try using Python with Google Cloud Functions
How to connect to Cloud SQL PostgreSQL on Google Cloud Platform from a local environment with Java
How to call Cloud API from GCP Cloud Functions
How to deal with OAuth2 error when using Google APIs from Python
Copy data from Amazon S3 to Google Cloud Storage with Python (boto)
[GCP] [Python] Deploy API serverless with Google Cloud Functions!
Connect to BigQuery with Python
How to scrape image data from flickr with python
Connect to Wikipedia with Python
Connect to sqlite from python
How to create a kubernetes pod from python code
How to extract any appointment in Google Calendar with Python
How to connect to various DBs from Python (PEP 249) and SQLAlchemy
How to upload files to Cloud Storage with Firebase's python SDK
[GCP] How to output Cloud Functions log to Cloud Logging (Stackdriver Logging) (Python)
Let's use Watson from Python! --How to use Developer Cloud Python SDK
Connect Raspberry Pi to Alibaba Cloud IoT Platform with Python
Python: How to use async with
Create folders from '01' to '12' with python
How to get started with Python
Connect to utf8mb4 database from python
How to use FTP with Python
How to calculate date with python
How to access wikipedia from python
[Python / Ruby] Understanding with code How to get data from online and write it to CSV
Post a message from IBM Cloud Functions to Slack in Python
How to build Python and Jupyter execution environment with VS Code
Connect to MySQL with Python within Docker
How to work with BigQuery in Python
Use Google Cloud Vision API from Python
How to do portmanteau test with python
How to search Google Drive with Google Colaboratory
How to display python Japanese with lolipop
[GCP] Operate Google Cloud Storage with Python
How to access RDS from Lambda (python)
How to switch python versions in cloud9
Connect to s3 with AWS Lambda Python
How to enter Japanese with Python curses
[Python] Understand how to use recursive functions
Use C ++ functions from python with pybind11
Connect to pepper with PEPPER Mac's python interpreter
[Python] How to deal with module errors
How to use SQLAlchemy / Connect with aiomysql
Upload images to Google Drive with Python
Connect to coincheck's Websocket API from Python
How to install python3 with docker centos
How to use VS Code (code server) with Google Colab in just 3 lines
How to deal with old Python versions in Cloud9 made by others
How to run the practice code of the book "Creating a profitable AI with Python" on Google Colaboratory
Build an environment to execute C ++ functions from Python with Pybind11 (for Windows & Visual Studio Code people)
How to upload with Heroku, Flask, Python, Git (4)
How to read a CSV file with Python 2/3
Csv output from Google search with [Python]! 【Easy】
How to enjoy programming with Minecraft (Ruby, Python)
How to open a web browser from python
How to do multi-core parallel processing with python
Firebase: Use Cloud Firestore and Cloud Storage from Python
Strategy on how to monetize with Python Java
Study from Python Hour7: How to use classes