Reputation:
I am trying to connect to Google App Engine's Datastore from my local machine in Java. I will not be using the service with an application on GAE, it will be on AWS.
What I tried
I tried using
DatastoreService datastore = DatastoreServiceFactory.getDatastoreService();
, but I think it is for when the application is hosted on GAE.
Right now, I have Google Storage up and running using a json credentials file fetched with the System Variable GOOGLE_APPLICATION_CREDENTIALS. The connection works fine, so my guess is that I might have to do something similar to Storage. I did something like this for Storage :
HttpTransport transport = GoogleNetHttpTransport.newTrustedTransport();
JsonFactory jsonFactory = new JacksonFactory();
GoogleCredential credential = GoogleCredential.getApplicationDefault(transport, jsonFactory);
if (credential.createScopedRequired()) {
Collection<String> bigqueryScopes = StorageScopes.all();
credential = credential.createScoped(bigqueryScopes);
}
Storage client = new Storage.Builder(transport, jsonFactory, credential)
.setApplicationName("APPLICATION_NAME")
.build();
The question
So my question is : How can I connect to Google App Engine Datastore from outside of Google App Engine?
Thank you for your help!
Upvotes: 1
Views: 1620
Reputation:
With Andrei Volgin's awesome help, I managed to be able to connect to Datastore from an application outside of Google App Engine. Here is what I did as of April 2016 :
GOOGLE_APPLICATION_CREDENTIALS=/path/to/credentials.json
I wrote this code as a test and the connection worked
DatastoreOptions options = DatastoreOptions.builder()
.projectId("PROJECT_NAME")
.authCredentials(AuthCredentials.createForJson(
new FileInputStream("/PATH/TO/CREDENTIALS.json"))).build();
Datastore datastore = options.service();
Query<Entity> query =
Query.gqlQueryBuilder(Query.ResultType.ENTITY, "SELECT * FROM user").build();
QueryResults<Entity> results = datastore.run(query);
while (results.hasNext()) {
Entity result = results.next();
System.out.println(result.getString("id"));
}
Upvotes: 0
Reputation: 41099
I recommend that you move to the gcloud-java client library. It is built on top of the code that you are currently using, and makes it easier to work with the Datastore and Storage, although the documentation is virtually non-existent (as of April 2016).
This is how you connect to the Datastore using this library:
AuthCredentials credentials = AuthCredentials.createFor(account, key);
DatastoreOptions options = DatastoreOptions.builder()
.authCredentials(credentials)
.projectId(projectId)
.namespace(NAMESPACE)
.build();
Datastore datastore = options.service();
Same for the Storage, but with StorageOptions.
TIP: Open gcloud-java project in GitHub - the link is on the page that I referenced earlier. There is a folder which contains examples of how to use Datastore and Storage.
Upvotes: 2