2024-04-26 10:25:57 发布
网友
我试图了解如何使用firestorelocalemulator for python和进行测试。但我找不到如何记录。在
有人能帮帮我吗?在
使用^{}python模块,遵循Cloud Firestore Docs中记录的标准设置
这将涉及使用credentials上下文调用initialize_app,然后使用firestore.client()创建一个传统的Firestore客户机
credentials
initialize_app
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,它将在localhost:8080上托管本地Firestore实例。在
localhost:8080
最后,在已经实例化的firestore.client实例中注入重定向,以使用不安全的GRPC通道与本地仿真器主机/端口交互:
firestore.client
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对象将与本地仿真器交互,不会出现任何问题。在
db
向John Carter致谢figuring this out on the gcloud internal api
欢迎使用SO:)
cloudfirestore仿真器(目前)的主要目的似乎是测试安全规则,如文档所述here。This section状态:“当前唯一支持模拟器的SDK是节点.jsSDK。“
令人困惑的是,Google云客户端库还有theseRuby文档。在Python中似乎还没有同样的功能。在
Here是作为googlecloudsdk的一部分运行模拟器的说明。
考虑在数据存储模式下使用Cloud Firestore,它有更好的工具(它可能只是有更多的时间成熟)。您可以在Running the Datastore mode Emulator页上找到运行其仿真器的说明。在
使用Choosing between Native Mode and Datastore Mode页来决定要走哪个方向。如果您觉得需要额外的“本机模式”特性,那么直接连接到云中的一个真正的Firestore实例可能是最容易的。在
使用^{} python模块,遵循Cloud Firestore Docs中记录的标准设置
这将涉及使用
credentials
上下文调用initialize_app
,然后使用firestore.client()
创建一个传统的Firestore客户机例如:
接下来,您需要安装并运行Firestore Emulator,它将在
^{pr2}$localhost:8080
上托管本地Firestore实例。在最后,在已经实例化的
firestore.client
实例中注入重定向,以使用不安全的GRPC通道与本地仿真器主机/端口交互:现在,
db
对象将与本地仿真器交互,不会出现任何问题。在向John Carter致谢figuring this out on the gcloud internal api
欢迎使用SO:)
cloudfirestore仿真器(目前)的主要目的似乎是测试安全规则,如文档所述here。This section状态:“当前唯一支持模拟器的SDK是节点.jsSDK。“
令人困惑的是,Google云客户端库还有theseRuby文档。在Python中似乎还没有同样的功能。在
Here是作为googlecloudsdk的一部分运行模拟器的说明。
考虑在数据存储模式下使用Cloud Firestore,它有更好的工具(它可能只是有更多的时间成熟)。您可以在Running the Datastore mode Emulator页上找到运行其仿真器的说明。在
使用Choosing between Native Mode and Datastore Mode页来决定要走哪个方向。如果您觉得需要额外的“本机模式”特性,那么直接连接到云中的一个真正的Firestore实例可能是最容易的。在
相关问题 更多 >
编程相关推荐