1

I've followed this tutorial. I notice that I need to set DATASTORE_SERVICE_ACCOUNT, DATASTORE_PRIVATE_KEY_FILE environment variables in order to configure the credential.

I wonder, is there other way to configure the credential without the need to set the environment variables? If there is no other way, Could any one help me to know how I can read these variables from the user and then set them programmatically from java? (I don't want to use the shell, I need to set them using codes) (Note:I'm using mac).

Any help will be appreciated..

PrecariousJimi
  • 1,503
  • 8
  • 20
ebt_dev
  • 149
  • 1
  • 3
  • 12

1 Answers1

1

Your example is kind of a java batch job that runs on Compute Engine, not App Engine.

  • Compute Engine =~ Cloud VM (shell terminal)
  • App Engine =~ Cloud Java Web Server (in fact it's a Jetty server tuned by Google)

In Java you access an environment variable with System.getProperty("ENV_VAR_NAME"); ... I highly doubt that's what you want here though. In your example, these environement variables are there to tell the Compute Engine runtime which Google App Engine datastore you want to use.

With Google App Engine, each application has its own datastore, which means that you don't need any specific configuration. Just getting a datastoreService instance.

See the docs : https://developers.google.com/appengine/docs/java/datastore/?hl=fr

EDIT -> How do I set environment variables from Java?

You would probably be better off doing it from the script that launches your app though..

Another (really simple) solution would be to create an App Engine web app as a back end, and access the datastore through web services. I don't know your use case though.

Community
  • 1
  • 1
Michael Técourt
  • 3,457
  • 1
  • 28
  • 45
  • Actually, I want to connect to the Datastore from local java application (not GAE app). I've followed [these instructions](https://developers.google.com/datastore/docs/activate#google_cloud_datastore_from_other_platforms) to access from other platform instead of Compute Engine. Then,I've followed this [_If you are **not connected** to a Compute Engine instance, make sure to set the following environment variables to configure your credentials_]. I found the variables are needed by DatastoreHelper.getOptionsfromEnv(). Is there any way to set them from code?. Thank you – ebt_dev Mar 18 '14 at 14:43
  • I guess you can try `System.setProperty("key", "value");` or this : http://stackoverflow.com/questions/318239/how-do-i-set-environment-variables-from-java – Michael Técourt Mar 19 '14 at 09:37
  • 1
    @ebt_dev: I would strongly recommend against hard-coding any credentials into your source code. Init script which prepares environment suggested by Michael is a far better idea. – PrecariousJimi Mar 19 '14 at 09:59