omrikap
omrikap

Reputation: 135

How do I access production Datastore from my local development server using Java?

I have a deployed app using Google App Engine for Java. How can I use the remote Datastore (from production) with a local development server?

My question is very similar to that one, only I'm asking about Java and not Python.

Upvotes: 2

Views: 485

Answers (1)

nshmura
nshmura

Reputation: 6010

Local program can be authonticated by service-account.

Here and here is docs about how to use service-account in java program.

Use env variable:

export GOOGLE_APPLICATION_CREDENTIALS=/path/to/my/key.json

Or supply the JSON credentials file:

import com.google.auth.oauth2.GoogleCredentials;
import com.google.cloud.datastore.Datastore;
import com.google.cloud.datastore.DatastoreOptions;
import com.google.cloud.datastore.Entity;
import com.google.cloud.datastore.Key;
import com.google.cloud.datastore.KeyFactory;

DatastoreOptions options = DatastoreOptions.newBuilder()
  .setProjectId(PROJECT_ID)
  .setCredentials(GoogleCredentials.fromStream(
    new FileInputStream(PATH_TO_JSON_KEY))).build();
Datastore datastore = options.getService();
KeyFactory keyFactory = datastore.newKeyFactory().setKind(KIND);
Key key = keyFactory.newKey(keyName);
Entity entity = datastore.get(key);

Upvotes: 2

Related Questions