Firebase: Use Cloud Firestore and Cloud Storage from Python

Introduction

--I'll leave a note about how to use Firebase's Cloud Firestore and Cloud Storage from Python. --Please prepare a CSV file and use it when uploading or downloading from python.

table of contents

-Preparation --Add SDK --Preparation of authentication file -[Use Cloud Firestore](Use # cloud-firestore) --Library import --Authentication information initialization --Get data --Add data --Delete data -[Use Cloud Storage](use # cloud-storage) --Library import --Authentication information initialization --Get data --Add data --Delete data --Summary

Preparation

--First, prepare the SDK and authentication file.

Add SDK

--Execute the following command to add the SDK.

python


pip install firebase-admin # firebase firestore
pip install --upgrade google-cloud-storage # cloud storage

Preparation of authentication file

--This is the authentication file used to access firebase programmatically. --Get the json file by following the steps below.

From https://firebase.google.com/docs/admin/setup?hl=ja

  1. In the Firebase console, open Settings> Service Accounts.
  1. Click Generate New Private Key, then click Generate Key to confirm.
  2. Securely store the JSON file containing the key.

Use Cloud Firestore

--I will summarize how to use firestore from python.

Library import

--First, import the libraries to be used.

import


import firebase_admin
from firebase_admin import credentials
from firebase_admin import firestore

Credential initialization

--Allow firebase to be accessed programmatically using the json authentication file prepared earlier. --Initialize with the following code.

initialize


cred = credentials.Certificate('<pass_to_json_file>')
default_app = firebase_admin.initialize_app(cred)

-** Replace <pass_to_json_file> with the path (character string) to the json file. ** ** -** Be sure to initialize before accessing firebase data. ** ** --If you do not initialize, an error will occur and you will not be able to acquire the data.

--It may be a little convenient to reserve cred and default_app as global variables and prepare an initialization function as shown below.

initialize2


cred = None
default_app = None

def initializeFirebaseAPP():
	global cred
	global default_app
	cred = credentials.Certificate("< pass_to_json_file >")
	default_app = firebase_admin.initialize_app(cred)

I didn't use it, but you can use environment variables to pass the path to the json file.

  1. Pass environment variables (in a terminal or terminal).
  2. In this case, initialization is performed without specifying any arguments.

export


export GOOGLE_APPLICATION_CREDENTIALS="<pass_to_json_file>"

initialize


default_app = firebase_admin.initialize_app()

Get data from Firestore

-** This is an example of specifying a single document ** and retrieving the contents. --Use get. --At this time, an exception will be thrown if the document pointed to by doc_ref does not exist. --This exception is google.cloud.exceptions.NotFound, so let's deal with it with try if necessary. --For doc_ref, you can check if the document exists by using the ʻexists` method.

getFirestoreData


def getFirestoreData(collectionName, documentName):
	db = firestore.client()
	doc_ref = db.collection(collectionName).document(documentName)

	doc = doc_ref.get()
	print(u'Document data: {}'.format(doc.to_dict()))

--Use stream for collection to get all ** documents ** directly under the collection.

getFirestoreData2


def getFirestoreData2(collectionName):
	db = firestore.client()
	docs = db.collection(collectionName).stream()

	for doc in docs:
		print(u'{} => {}'.format(doc.id, doc.to_dict()))

Get data from Firestore (recursive use)

--To access the collection in the document, use the collection method for the document. ――You can do it as you intuitively.

getFirestoreData3


def getFirestoreData3(col_name, doc_name, col_name2):
	db = firestore.client()
	doc_ref = db.collection(col_name).document(doc_name)
	col_ref2 = doc_ref.collection(col_name2)

--Currently, it is not possible to get a list of document subcollections in python. ――If you want to get the list of subcollections, use other languages together.

――For example, if you write in ruby, it is as follows.

getCollectionList.rb


require "google/cloud/firestore"

firestore = Google::Cloud::Firestore.new project_id: "<project_id>", keyfile: "<path_to_json>"
doc_ref = firestore.doc "<col_name>/<doc_name>"

doc_ref.cols do |col|
	puts col.collection_id
end

Add data to Firestore

--This is an example of registering dictionary type data by specifying the collection name and document name.

uploadData2Firestore


def uploadData2Firestore(collectionName, documentName, data):
	db = firestore.client()
	doc_ref = db.collection(collectionName).document(documentName)
	doc_ref.set(data)

Update Firestore data

--How to update a field on an existing document.

update_data


def update_data(collectionName, documentName, key, value):
    db = firestore.client()
    doc_ref = db.collection(collectionName).document(documentName)

    doc_ref.update({key: value})

Delete data from Firestore

--This is an example of deleting data by specifying the collection name and document name.

deleteFirestoreDocument


def deleteFirestoreDocument(collectionName, documentName):
	db = firestore.client()
	db.collection(collectionName).document(documentName).delete()

Use Cloud Storage

Library import

--First, import the libraries to be used.

import


import firebase_admin
from firebase_admin import credentials
from google.cloud import storage

Credential initialization

--Allow firebase to be accessed programmatically using the json authentication file prepared earlier. --Initialize with the following code.

initialize


storage_client = storage.Client.from_service_account_json('<pass_to_json_file>')

-** Replace <pass_to_json_file> with the path to the json file. ** ** -** The variables initialized here will be used later. ** **

I didn't use it, but you can use environment variables to pass the path to the json file.

  1. Pass environment variables (in a terminal or terminal).
  2. In this case, initialization is performed without specifying any arguments.

export


export GOOGLE_APPLICATION_CREDENTIALS="<pass_to_json_file>"

initialize


storage_client = storage.Client()

Get data from Cloud Storage

--This is an example of downloading data to a file by specifying the blob url.

download_blob


def download_blob(src_blob_url, dst_file):
	bucket_name = '<bucket_name>'
	bucket = storage_client.get_bucket(bucket_name)

	blob = bucket.blob(src_blob_url)
	storage_client.download_blob_to_file(blob, dst_file)

	print('Blob {} downloaded to {}.'.format(src_blob_url, dst_file))

Add data to Cloud Storage

--This is an example of registering by specifying the file name and data.

upload_blob


def upload_blob(file_name, data_sorce):
	bucket_name = '<bucket_name>'
	bucket = storage_client.get_bucket(bucket_name)
	blob = bucket.blob(file_name)
	blob.upload_from_filename(filename=data_sorce)

	if file_name in bucket.list_blobs():
		print("cloud storage: upload success")
	else:
		print("cloud storage: upload failed")

Delete data from Cloud Storage

--This is an example of deleting data by specifying the blob url.

delete_blob


def delete_blob(blob_url):
	bucket_name = '<bucket_name>'
	bucket = storage_client.get_bucket(bucket_name)
	blob = bucket.blob(blob_url)

	blob.delete()

Summary

――I didn't have much information about how to avoid using environment variables, so I summarized it. ――It may be safer to use environment variables in actual operation, but ...

――The following is a site I often visited. -Try Cloud Firestore --Add data to Cloud Firestore -Get data with Cloud Firestore --Cloud Firestore: python API Reference ――We would appreciate it if you could contact us if you have any typographical errors.

Recommended Posts

Firebase: Use Cloud Firestore and Cloud Storage from Python
Read and use Python files from Python
Using Cloud Storage from Python3 (Introduction)
Use Azure Blob Storage from Python
Use Google Cloud Vision API from Python
Operate Sakura's cloud object storage from Python
Use thingsspeak from python
Use fluentd from python
Use MySQL from Python
Use MySQL from Python
Use BigQuery from python.
Use mecab-ipadic-neologd from python
Use MySQL from Anaconda (python)
Use e-Stat API from Python
Easy to use Nifty Cloud API with botocore and python
Let's use Watson from Python! --How to use Developer Cloud Python SDK
Access Google Cloud Storage from Python (boto) using your service account and key file (p12)
Use Stanford Core NLP from Python
Python, yield, return, and sometimes yield from
[Python / matplotlib] Understand and use FuncAnimation
Forcibly use Google Translate from python
About Python, from and import, as
Use kabu Station® API from Python
Use the Flickr API from Python
Use Cloud Datastore from Compute Engine
Use fastText trained model from Python
Use Google Analytics API from Python
How to connect to Cloud Firestore from Google Cloud Functions with python code
Try using FireBase Cloud Firestore in Python for the time being
Run Cloud Dataflow (Python) from App Engine
From Python to using MeCab (and CaboCha)
[Python] Use and and or when creating variables
Deploy Python face recognition model on Heroku and use it from Flutter ②
Use PostgreSQL data type (jsonb) from Python
How to install and use pandas_datareader [Python]
Use machine learning APIs A3RT from Python
I want to use jar from python
Deploy Python face recognition model on Heroku and use it from Flutter ①
Use PIL and Pillow with Cygwin Python
Connect to postgreSQL from Python and use stored procedures in a loop.
[GCP] Operate Google Cloud Storage with Python
Porting and modifying doublet-solver from python2 to python3.
python: How to use locals () and globals ()
How to use Python zip and enumerate
Use Django from a local Python script
Use C ++ functions from python with pybind11
Copy data from Amazon S3 to Google Cloud Storage with Python (boto)
How to use is and == in Python
Use Python and MeCab with Azure Functions
Use the LibreOffice app in Python (2) Manipulate calc (from macros and externals)
WEB scraping with python and try to make a word cloud from reviews
Study from Python Hour7: How to use classes
Study from Python Reading and writing Hour9 files
Access Cloud Storage from your Compute Engine instance
[Python] How to read data from CIFAR-10 and CIFAR-100
[Bash] Use here-documents to get python power from bash
Wrap C with Cython for use from Python
[Python] Find coordinates from two angles and distance
Load and execute command from yml in python
Use Python in your environment from Win Automation
PHP and Python integration from scratch on Laravel