我需要将我的代码在Docker映像中发送到一个无服务器平台,在该平台中我无法放置信用文件。
通常,我会在构建时将firestore_creds.json
复制到映像中并在本地运行,但这不是在发布代码时做的正确事情,并且非常不安全。
环境变量
export GOOGLE_APPLICATION_CREDENTIALS="firestore_creds.json"
Python就是这样,Client()
只是从初始化时设置的env变量中读取。
from google.cloud import firestore
client = firestore.Client()
我如何去创建这个Client
没有json信用文件存在?
注意:我真的想避免不得不将creds作为json字符串环境变量,然后读取它,写入文件,然后继续。看起来又乱又丑。
在线程
中提到如果内存中有凭据(环境变量)示例),并且您不想为它专门创建一个文件:
from google.cloud import storage from google.oauth2 import service_account gcp_json_credentials_dict = json.loads(gcp_credentials_string) credentials = service_account.Credentials.from_service_account_info(gcp_json_credentials_dict) client = storage.Client(project=gcp_json_credentials_dict['project_id'], credentials=credentials)
使用python3.7和
google-cloud-storage==1.35.0
你也可以检查这个答案
有一种更简单的方法可以显式地使其工作提及凭据并将其传递给客户端,如下所示。
import os from google.oauth2 import service_account credentials = service_account.Credentials.from_service_account_file("your-json-path-with-filename.json") client = language.LanguageServiceClient(credentials=credentials)
为了获得更多有关通过客户端App安全访问Firestore的信息,您可以查看安全规则和Cloud first store Rest API
这里提供的是与安全访问相关的:Firebase-admin不适用于web和移动客户端。这是对于在您完全控制的环境中运行的后端代码。你的用户将无法访问这个,这意味着它对您可以使用私钥。
您肯定不想发布任何服务帐户凭据与你的应用。你应该使用提供的客户端sdk从应用程序访问Firestore,并使用声明哪些经过身份验证的用户能够访问的安全规则读和写哪些文档