Google Firebase:使用 Python 获取、更新或创建文档
Google Firebase: Get, update or create documents using Python
我无法使用 Python.
在 Google Firebase (Cloud Firestore) 数据库中获取、更新或创建文档
我有:
A) 包含集合和文档的数据库(在网络上手动插入):
B) 保存为 test.json
的凭据 JSON 文件(在文档中经常被称为 path/to/serviceKey.json
),看起来像这样(编辑):
{
"type": "service_account",
"project_id": "test-6f02d",
"private_key_id": "fffca ... 5b7",
"private_key": "-----BEGIN PRIVATE KEY-----\n ... 1IHE=\n-----END PRIVATE KEY-----\n",
"client_email": "test-admin@test-6f02d.iam.gserviceaccount.com",
"client_id": "112 ... 060",
"auth_uri": "https://accounts.google.com/o/oauth2/auth",
"token_uri": "https://oauth2.googleapis.com/token",
"auth_provider_x509_cert_url": "https://www.googleapis.com/oauth2/v1/certs",
"client_x509_cert_url": "https://www.googleapis.com/robot/ ... .gserviceaccount.com"
}
此用户的角色是所有者。
C) firebase_admin 安装(使用 virtualenv,pip),我可以做:
import firebase_admin
from firebase_admin import credentials, firestore
databaseURL = {'databaseURL': "https://test-6f02d.firebaseio.com"}
cred = credentials.Certificate("test.json")
firebase_admin.initialize_app(cred, databaseURL)
<firebase_admin.App object at 0x7f20056534e0>
以下有效:
db = firestore.client()
for k in db.collection('items').get():
print(k)
我正在获取 3 个文档,我可以访问文档的 id
<google.cloud.firestore_v1beta1.document.DocumentSnapshot object at 0x7f2003bebc18>
<google.cloud.firestore_v1beta1.document.DocumentSnapshot object at 0x7f2003bebdd8>
<google.cloud.firestore_v1beta1.document.DocumentSnapshot object at 0x7f2003bebcf8>
print(k.id)
a3BxcpWpavHmuz6DpZH3
但是,这是我能得到的最大值。
1) 我不知道如何访问文档的值。像这样:
from firebase_admin import db
ref = db.reference('items')
print(ref)
<firebase_admin.db.Reference object at 0x7f20013b2828>
# GET?
ref.get()
# empty
2) 我不知道如何直接访问这些值(例如,使用浏览器或 requests
),例如:
https://test-6f02d.firebaseio.com/items.json
returns
{
"error" : "Permission denied"
}
3) 我不知道如何更新现有文档或在集合中创建新文档 items
。
# UPDATE?
# PUSH?
我试图遵循 this blog and the documentation(但它没有示例)和这里的几个答案,但没有成功。
提前致谢。
改天晚上我可以自己回答(感谢 Doug 在讨论中的提示):
我的问题是有两个类似的文档(对于 Python 部分,第二个文档比 Python 更广泛)。我发现第一个更有帮助,但有时我也需要使用第二个的一部分:
- https://googleapis.github.io/google-cloud-python/latest/firestore/index.html(特别是 API 参考)
- https://firebase.google.com/docs/firestore/
1) 访问文档:
import firebase_admin
from firebase_admin import credentials, firestore
databaseURL = {
'databaseURL': "https://test-6f02d.firebaseio.com"
}
cred = credentials.Certificate("test.json")
firebase_admin.initialize_app(cred, databaseURL)
database = firestore.client()
col_ref = database.collection('items') # col_ref is CollectionReference
results = col_ref.where('name', '==', 'Pepa').get() # one way to query
results = col_ref.order_by('date',direction='DESCENDING').limit(1).get() # another way - get the last document by date
for item in results:
print(item.to_dict())
print(item.id)
# item is DocumentSnapshot
# note: the documentation says get() is depreciated in favour of stream(), however stream() did not work for me
2) 仍然不知道,但我不需要它,因为 1) 工作正常。
3) 更新或创建文档:
# Continuing from 1)
# Udpdate:
doc = col_ref.document(item.id) # doc is DocumentReference
field_updates = {"description": "Updated description"}
doc.update(field_updates)
# Create:
import datetime
new_values = {
"name": "Newbie",
"description": "Shiny New Document",
"date": datetime.datetime.now()
}
col_ref.document().create(new_values)
import firebase_admin
from firebase_admin import credentials
from firebase_admin import firestore
cred = credentials.Certificate("serviceAccountKey.json")
firebase_admin.initialize_app(cred)
db = firestore.client()
docs = db.collection("persons").where("Province","==",cprovince.get()).get()
for doc in docs:
print(doc.to_dict())
我无法使用 Python.
在 Google Firebase (Cloud Firestore) 数据库中获取、更新或创建文档我有:
A) 包含集合和文档的数据库(在网络上手动插入):
B) 保存为 test.json
的凭据 JSON 文件(在文档中经常被称为 path/to/serviceKey.json
),看起来像这样(编辑):
{
"type": "service_account",
"project_id": "test-6f02d",
"private_key_id": "fffca ... 5b7",
"private_key": "-----BEGIN PRIVATE KEY-----\n ... 1IHE=\n-----END PRIVATE KEY-----\n",
"client_email": "test-admin@test-6f02d.iam.gserviceaccount.com",
"client_id": "112 ... 060",
"auth_uri": "https://accounts.google.com/o/oauth2/auth",
"token_uri": "https://oauth2.googleapis.com/token",
"auth_provider_x509_cert_url": "https://www.googleapis.com/oauth2/v1/certs",
"client_x509_cert_url": "https://www.googleapis.com/robot/ ... .gserviceaccount.com"
}
此用户的角色是所有者。
C) firebase_admin 安装(使用 virtualenv,pip),我可以做:
import firebase_admin
from firebase_admin import credentials, firestore
databaseURL = {'databaseURL': "https://test-6f02d.firebaseio.com"}
cred = credentials.Certificate("test.json")
firebase_admin.initialize_app(cred, databaseURL)
<firebase_admin.App object at 0x7f20056534e0>
以下有效:
db = firestore.client()
for k in db.collection('items').get():
print(k)
我正在获取 3 个文档,我可以访问文档的 id
<google.cloud.firestore_v1beta1.document.DocumentSnapshot object at 0x7f2003bebc18>
<google.cloud.firestore_v1beta1.document.DocumentSnapshot object at 0x7f2003bebdd8>
<google.cloud.firestore_v1beta1.document.DocumentSnapshot object at 0x7f2003bebcf8>
print(k.id)
a3BxcpWpavHmuz6DpZH3
但是,这是我能得到的最大值。
1) 我不知道如何访问文档的值。像这样:
from firebase_admin import db
ref = db.reference('items')
print(ref)
<firebase_admin.db.Reference object at 0x7f20013b2828>
# GET?
ref.get()
# empty
2) 我不知道如何直接访问这些值(例如,使用浏览器或 requests
),例如:
https://test-6f02d.firebaseio.com/items.json
returns
{
"error" : "Permission denied"
}
3) 我不知道如何更新现有文档或在集合中创建新文档 items
。
# UPDATE?
# PUSH?
我试图遵循 this blog and the documentation(但它没有示例)和这里的几个答案,但没有成功。
提前致谢。
改天晚上我可以自己回答(感谢 Doug 在讨论中的提示):
我的问题是有两个类似的文档(对于 Python 部分,第二个文档比 Python 更广泛)。我发现第一个更有帮助,但有时我也需要使用第二个的一部分:
- https://googleapis.github.io/google-cloud-python/latest/firestore/index.html(特别是 API 参考)
- https://firebase.google.com/docs/firestore/
1) 访问文档:
import firebase_admin
from firebase_admin import credentials, firestore
databaseURL = {
'databaseURL': "https://test-6f02d.firebaseio.com"
}
cred = credentials.Certificate("test.json")
firebase_admin.initialize_app(cred, databaseURL)
database = firestore.client()
col_ref = database.collection('items') # col_ref is CollectionReference
results = col_ref.where('name', '==', 'Pepa').get() # one way to query
results = col_ref.order_by('date',direction='DESCENDING').limit(1).get() # another way - get the last document by date
for item in results:
print(item.to_dict())
print(item.id)
# item is DocumentSnapshot
# note: the documentation says get() is depreciated in favour of stream(), however stream() did not work for me
2) 仍然不知道,但我不需要它,因为 1) 工作正常。
3) 更新或创建文档:
# Continuing from 1)
# Udpdate:
doc = col_ref.document(item.id) # doc is DocumentReference
field_updates = {"description": "Updated description"}
doc.update(field_updates)
# Create:
import datetime
new_values = {
"name": "Newbie",
"description": "Shiny New Document",
"date": datetime.datetime.now()
}
col_ref.document().create(new_values)
import firebase_admin
from firebase_admin import credentials
from firebase_admin import firestore
cred = credentials.Certificate("serviceAccountKey.json")
firebase_admin.initialize_app(cred)
db = firestore.client()
docs = db.collection("persons").where("Province","==",cprovince.get()).get()
for doc in docs:
print(doc.to_dict())