こちらを参考にしました。
コレクションのすべてのドキュメントを取得する
get_firestore_docs.py
#! /usr/bin/python
#
# get_firestore_docs.py
#
# Apr/02/2022
# ------------------------------------------------------------------
import sys
import json
import firebase_admin
from firebase_admin import credentials
from firebase_admin import firestore
from datetime import date, datetime
# ------------------------------------------------------------------
def json_serial(obj):
# 日付型の場合には、文字列に変換します
if isinstance(obj, (datetime, date)):
return obj.isoformat()
# 上記以外はサポート対象外.
raise TypeError ("Type %s not serializable" % type(obj))
# ------------------------------------------------------------------
sys.stderr.write("*** 開始 ***\n")
project_id='project-apr02-2022'
collection_in='sample_collection'
cred = credentials.ApplicationDefault()
firebase_admin.initialize_app(cred, {
'projectId': project_id
})
db = firestore.client()
docs = db.collection(collection_in).stream()
for doc in docs:
json_str=json.dumps(doc.to_dict(),default=json_serial)
print(json_str)
sys.stderr.write("*** 終了 ***\n")
# ------------------------------------------------------------------
実行スクリプト
export GOOGLE_APPLICATION_CREDENTIALS="key-file-apr02.json"
./get_firestore_docs.py