【问题标题】:Firestore Emulator in PythonPython 中的 Firestore 模拟器
【发布时间】:2021-08-25 01:06:21
【问题描述】:

我正在尝试使用 python 3.8 连接到 firestore 模拟器 8.12。我很难找到一种连接方式。我已经从云端的 Firestore 下载了我的密钥。但我正在尝试在本地连接。本地连接模拟器的local.json文件是什么样子的?

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

cred = credentials.ApplicationDefault()
os.environ["GOOGLE_APPLICATION_CREDENTIALS"] = "local.json"

firebase_app = firebase_admin.initialize_app(cred)
db=firestore.client()

data = {
    u'name': u'Los Angeles',
    u'state': u'CA',
    u'country': u'USA'
}

# Add a new doc in collection 'cities' with ID 'LA'
db.collection(u'cities').document(u'LA').set(data)

【问题讨论】:

  • 抱歉没用。

标签: python firebase google-cloud-firestore emulation


【解决方案1】:

从 repo 上的 issue 看来,Python Admin SDK 似乎也需要设置 FIRESTORE_EMULATOR_HOST 环境变量。

os.environ["FIRESTORE_EMULATOR_HOST"] = "127.0.0.1:8080"
os.environ["GOOGLE_APPLICATION_CREDENTIALS"] = "emulator_config.json"
docs = db.collection(u'insights/venues/queue').stream()

【讨论】:

  • 如何获取emulator_config.json文件?
【解决方案2】:

您可以使用匿名凭据。来自 google-auth 文档:

这些对于支持匿名访问的服务很有用 或不使用凭据的本地服务模拟器。

如果您不提供凭据,那么它将像您一样使用默认凭据并描述here

from google.auth import credentials
from google.cloud import firestore
        
client = firestore.Client(credentials=credentials.AnonymousCredentials())

FIRESTORE_EMULATOR_HOST 需要设置

【讨论】:

    猜你喜欢
    • 2021-10-04
    • 1970-01-01
    • 2021-09-17
    • 2020-10-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-01-07
    • 2019-10-03
    相关资源
    最近更新 更多