1
votes

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.

1
Whats your use case? you could depoly your app to a different project id, and use datastore admin to clone the datastore from one project to the new one. By the way i'm pretty sure that bulkloader.py solution from the question you linked is just a standalone script you can run. it doesn't matter that your project is in java. - Alex

1 Answers

2
votes

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);