google.golang.org/appengine/datastore 对比 cloud.google.com/go/datastore

google.golang.org/appengine/datastore vs cloud.google.com/go/datastore

我构建了一个小的 go 应用程序,用于 AppEngine。我 运行 在本地通过 go run ..,我正在尝试使用数据存储。

我 运行 通过 gcloud beta emulators datastore start 在本地使用数据存储模拟器并通过导出 DATASTORE_EMULATOR_HOST Go 应用程序可以在本地连接到它。

所以我使用 cloud.google.com/go/datastore 构建了我的应用程序,但是当我将其部署到 AppEngine 时,任何连接到数据存储区的代码似乎都会使整个应用程序因超时而失败。

在尝试调试时,我 运行 使用 google.golang.org/appengine/datastore 编写代码并编写一些测试代码,我在 AppEngine 上工作。

但是这个数据存储似乎无法连接到数据存储模拟器。

  1. 有没有人运行遇到使用cloud.google.com/go/datastore时的超时问题?我似乎读到了不同的答案,人们说出于某种原因使用 this 包而不是 golang.org 包。
  2. 我可以在 AppEngine 上使用 cloud.google.com/go/datastore 吗?我更愿意使用这个包,因为它适用于数据存储模拟器。

我正在使用以下代码,简而言之:

import "cloud.google.com/go/datastore"
...
ctx := appengine.NewContext(r)
...
client, err := datastore.NewClient(dsCtx, projectID)
...
key := datastore.IDKey(TestKind, testID, nil)
err = client.Get(ctx, key, &data)

这是在 AppEngine 上超时的代码,但在本地运行良好。

同样适用于 AppEngine 的代码是这样的:

import "google.golang.org/appengine/datastore"
...
ctx := appengine.NewContext(r)
key := datastore.NewKey(ctx, TestKind, "", testID, nil)
err := datastore.Get(ctx, key, &data)

这将在本地失败并显示以下内容:

Metadata fetch failed: Get http://metadata/computeMetadata/v1/instance/attributes/gae_project: dial tcp: lookup metadata: no such host

如有任何帮助,我们将不胜感激。

来自Connecting to Cloud Datastore with App Engine section in the App Engine Cloud Datastore Overview

You cannot use the Cloud Datastore client library with Go applications in the App Engine standard environment.

基本上,cloud.google.com/go/datastore 用于在 App Engine 标准环境之外使用 Cloud Datastore 。这包括非 App Engine 环境以及 App Engine 柔性环境。

google.golang.org/appengine/datastore 用于在 App Engine 标准环境中使用它

对于 App Engine 标准环境的本地测试,请研究使用 dev_appserver.py,它通过 --support_datastore_emulator 标志提供与 Cloud Datastore 模拟器的集成。