【问题标题】:Using Firestore in a Google App Engine project在 Google App Engine 项目中使用 Firestore
【发布时间】:2019-12-09 16:16:59
【问题描述】:

我想在我的 Google App Engine 项目中访问 Firestore 的实时数据库作为附加数据库。

从 Google App Engine 后端写入 Firestore 的最佳方式是什么?使用 Firestore Rest API 是否合适?或者有没有更好的方法?

两个项目之间的身份验证如何工作?因为据我了解,Google App Engine 项目和 Firestore 项目是两个不同的东西。

【问题讨论】:

    标签: google-app-engine google-cloud-platform google-cloud-firestore


    【解决方案1】:

    是的,最佳做法是使用 Firestore Rest API。

    关于身份验证,应用引擎服务有其默认服务帐户,您可以在应用引擎页面上找到该帐户。 只需在项目 B 上授予此服务帐户访问和编写 firestore 的权限,Lib SDK 将顺利处理一切!

    您使用哪种语言?所以我可以用演示代码编辑我的答案

    编辑 1:

    您可能会误解项目的概念。项目是您可以使用服务的信封。在此示例中:您可以拥有一个包含 Firestore 和 App Engine 的 GCP 项目。

    编辑2:

    正如我所想,Google 演示存储库中有一个代码示例。看一下这个 ! https://github.com/GoogleCloudPlatform/java-docs-samples/tree/master/firestore

    【讨论】:

    • Java - 还有 8 个字符
    • 关于您的第二次编辑,在您发布的链接上显示:“注意:您不能在同一个项目中同时使用 Cloud Firestore 和 Cloud Datastore,这可能会影响使用 App Engine 的应用程序。尝试使用具有不同项目的 Cloud Firestore。” - 这就是我的情况。我有 Datastore,也想使用 Firestore。所以我需要两个看起来像的项目。但是,您的链接很有帮助。
    【解决方案2】:

    访问Google Cloud Firestore 的最佳方式是使用其客户端库(而不是 REST API)。我的意思是,您可以使用 REST API,这并没有什么问题,但它通常需要您编写更多代码,而客户端库则承担更多繁重工作。

    使用客户端库,设置 API 客户端是一个 1-liner,然后访问它很简单。这是一个简单的网络应用程序的代码 sn-p,它跟踪所有网站访问,只是为了向您展示基本用法(适用于 2.x 和 3.x):

    'fs_visits.py -- Cloud Firestore sample Python snippet for web visit registry'
    
    from __future__ import print_function
    from datetime import datetime
    from google.cloud import firestore
    
    def store_visit(timestamp):
        visits = fs_client.collection('visitX')
        visits.add({'timestamp': timestamp})
    
    def fetch_visits(limit):
        visits = fs_client.collection('visitX')
        return visits.order_by(u'timestamp',
                direction=firestore.Query.DESCENDING).limit(limit).stream()
    
    TOP = 10                            # limit to 10 results
    fs_client = firestore.Client()         # create Cloud Firestore client
    
    if __name__ == '__main__':
        print('** Adding another visit')
        store_visit(datetime.now())          # store a "visit" object
        print('** Last %d visits' % TOP)
        times = fetch_visits(TOP)            # fetch most recent "visits"
        for obj in times:
            print('-', obj.to_dict()['timestamp'])
    

    这是我运行它时得到的示例输出:

    $ python fs-snip.py
    ** Adding another visit
    ** Last 10 visits
    - 2020-03-23 09:14:09.742027+00:00
    - 2020-03-11 01:06:47.103570+00:00
    - 2020-03-11 01:03:29.487141+00:00
    - 2020-03-11 01:03:16.583822+00:00
    - 2020-03-11 01:02:35.844559+00:00
    - 2020-03-11 00:59:51.939175+00:00
    - 2020-03-11 00:59:39.874525+00:00
    - 2020-03-11 00:58:51.127166+00:00
    - 2020-03-11 00:58:34.768755+00:00
    - 2020-03-11 00:58:21.952063+00:00
    

    它没有解释太多,但如果你能弄清楚代码,你就会知道它是如何工作的。这是官方的Quickstart tutorial 和后续的deeper dive tutorial,它比我的小代码 sn-p 更详细地介绍了所有内容。

    FWIW,我尝试提出相同的方法,但使用 the REST API 并遇到了一些烫发问题 (403)。也许我以后会再访。 (尝试 15-20 分钟对我来说太难了。)

    就项目而言,Google Cloud(平台)项目是应用程序及其资源的单一逻辑实体。目前,每个项目都可以有一个 App Engine 应用和一个 NoSQL 数据库(Cloud Datastore [现在在 Datastore 模式下称为 Cloud Firestore] 或 Cloud Firestore [在 Native 模式下称为 Cloud Firestore]...you pick)。这意味着它们可以在同一个项目中,因此您不必担心跨项目权限。

    【讨论】:

      猜你喜欢
      • 2014-09-01
      • 2011-08-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-02-11
      • 2011-04-12
      • 2015-01-28
      • 1970-01-01
      相关资源
      最近更新 更多