user3420056
user3420056

Reputation: 13

How to setup the datastore so I can access my local machine through a non App Engine Java App

I'm having problem with accessing google cloud datastore from my local machine in a java app (non app engine).

When I'm trying to accessing the datastore with the code below I'm getting an exception saying that "datastore dataset not set in options", which make some seen because its not sett in GoogleCredential.

System.setProperty("DATASTORE_DATASET", "xxxx-722");
     String emailAddress = "[email protected]";
     JsonFactory JSON_FACTORY = JacksonFactory.getDefaultInstance();
     HttpTransport httpTransport = GoogleNetHttpTransport.newTrustedTransport();

     GoogleCredential credential = new GoogleCredential.Builder()
         .setTransport(httpTransport)
         .setJsonFactory(JSON_FACTORY)
         .setServiceAccountId(emailAddress)
         .setServiceAccountPrivateKeyFromP12File(new File("D:\\Key\\xxxxx.p12"))
         .setServiceAccountScopes(Collections.singleton(StorageScopes.DEVSTORAGE_FULL_CONTROL))         
         .build();

And if i do as the code below I'm getting an exception that I'm not using any credentials...

System.setProperty("DATASTORE_SERVICE_ACCOUNT", "[email protected]");
     System.setProperty("DATASTORE_PRIVATE_KEY_FILE", "D:\\Key\\xxxxxx.p12");
     System.setProperty("DATASTORE_DATASET", "xxxxxx-722");
     Datastore datastore = DatastoreFactory.get().create(DatastoreHelper.getOptionsfromEnv().dataset("xxxxx-722").build()); //(credential).build());

So my question is how do i setup the datastore object correctly so i can access it on my local machine through a non app engine Java App? All help is much appreciated.

Upvotes: 0

Views: 339

Answers (1)

Ed Davisson
Ed Davisson

Reputation: 2927

Note that DatastoreHelper looks at environment variables rather than system properties.

The easiest way to create the Datastore object is to set three environment variables:

export DATASTORE_DATASET=<dataset>
export DATASTORE_SERVICE_ACCOUNT=<service-account>
export DATASTORE_PRIVATE_KEY_FILE=<path-to-private-key-file>

and then in code call:

Datastore datastore = DatastoreFactory.get().getDatastoreFromEnv();

If you need to set or override some of the options programmatically, you can do something like:

Datastore datastore = DatastoreFactory.get().create(DatastoreHelper.getOptionsfromEnv()
    .dataset("<dataset>")
    .build())

Upvotes: 1

Related Questions