kris
kris

Reputation: 23592

How do I access Cloud Datastore from AppEngine?

I created an AppEngine project and used the 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 docs and added the following to the skeleton app:

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

Then I built and deployed the app (appcfg.sh -A <project> update target/appengine-try-java-1.0) and made a request and got the following error in the logs:

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

Is there something I need to set up on AppEngine to let it connect to Datastore? It doesn't seem to be able to find any credentials in the environment.

Upvotes: 0

Views: 245

Answers (1)

tx802
tx802

Reputation: 3564

If you're running on App Engine, you can use the native Datastore API (docs).

Using the native API you don't need to connect, it's 'just there', e.g.:

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

Cloud Datastore tends to refer to the version of Datastore that's packaged as a service for external applications.

Upvotes: 2

Related Questions