如何从 AppEngine 访问 Cloud Datastore?

How do I access Cloud Datastore from AppEngine?

我创建了一个 AppEngine 项目并使用了 sample java project as a starting point. I want my app to be able to connect to the Cloud Datastore so I went through the getting started 文档并将以下内容添加到框架应用程序中:

datastore = DatastoreFactory.get().create(DatastoreHelper.getOptionsfromEnv()
      .dataset(datasetId).build());

然后我构建并部署了应用程序 (appcfg.sh -A <project> update target/appengine-try-java-1.0) 并发出请求并在日志中收到以下错误:

W 17:40:59.602 2015-05-20  200       0 B   4.12s /demo
   172.26.42.164 - - [20/May/2015:14:40:59 -0700] "POST /demo HTTP/1.1" 200 0 - "Apache-HttpClient/4.2.5 (java 1.5)" "project-id.appspot.com" ms=4122 cpu_ms=4667 cpm_usd=0.000158 loading_request=1 instance=00c61b117c2949ed37017839e6c82982472c2c app_engine_release=1.9.21
W 17:40:59.394 com.google.api.services.datastore.client.DatastoreFactory makeClient: Not using any credentials
W 17:40:59.575 [s~project-id/1.384441993309831713].<stderr>: Error while doing datastore operation
W 17:40:59.575 [s~project-id/1.384441993309831713].<stderr>: DatastoreException(Login Required): beginTransaction 401

我需要在 AppEngine 上设置什么才能连接到 Datastore 吗?它似乎无法在环境中找到任何凭据。

如果您 运行 使用 App Engine,则可以使用本机数据存储区 API (docs)。

使用原生API不需要连接,是'just there',例如:

DatastoreService datastore = DatastoreServiceFactory.getDatastoreService();
Entity foo = new Entity("Foo");
foo.setProperty("bar", "baz");
datastore.put(foo);

Cloud Datastore 往往指的是 Datastore 的版本,它被打包为外部应用程序的服务。