python - 如何将 Google Cloud Firestore 本地模拟器用于 python 和测试目的
问题描述
我试图找出如何将firestore本地模拟器用于python和用于测试目的。但我找不到操作方法文档。
有人可以帮我吗?
解决方案
使用firebase_admin
python 模块,遵循Cloud Firestore 文档中记录的标准设置
initialize_app
这将涉及使用上下文调用,credentials
然后创建一个传统的 Firestore 客户端firestore.client()
例如:
from firebase_admin import credentials, firestore, initialize_app
firebase_credentials_file_path = ...
cred = credentials.Certificate(firebase_credentials_file_path)
initialize_app(cred)
db = firestore.client()
接下来,您将需要安装并运行Firestore Emulator,它将托管本地 Firestore 实例localhost:8080
。
npx firebase setup:emulators:firestore
npx firebase --token $FIREBASE_TOKEN emulators:start --only firestore --project $PROJECT_KEY
最后,在已经实例化的实例中注入重定向,以firestore.client
使用不安全的 GRPC 通道与本地模拟器主机/端口进行交互:
import grpc
from google.cloud.firestore_v1.gapic import firestore_client
from google.cloud.firestore_v1.gapic.transports import firestore_grpc_transport
channel = grpc.insecure_channel("localhost:8080")
transport = firestore_grpc_transport.FirestoreGrpcTransport(channel=channel)
db._firestore_api_internal = firestore_client.FirestoreClient(transport=transport)
现在,您的db
对象将毫无问题地与本地模拟器进行交互。
推荐阅读
- siebel - 我可以在 Siebel 7.8 中动态禁用向下钻取吗?
- gem5 - 如何在 Gem5 中编辑用于执行的数据
- python - 带 ffill 的 GroupBy 删除组并且不将组放入索引
- c# - 在控制台应用程序中使用图形 sdk 从 o365 获取用户邮件
- oracle - oracle rest数据服务中如何使用procedure
- yolo - 在 COCO 上训练 YOLOv3 给了我不好的平均精度
- amazon-web-services - 如何在 Jenkins 中将 Docker 镜像推送到 ECR?
- swift - 场景委托如何在 iOS 13 之前使用?
- css - 如何将 textarea 的占位符对齐到右下角?
- php - 以下哪项最适合内存使用?