10

I'm trying to insert data to Google Datastore from AppEngine and I'm getting an error:

java.lang.IllegalArgumentException: A project ID is required for this service but could not be determined from the builder or the environment.  Please set a project ID using the builder.
    at com.google.common.base.Preconditions.checkArgument(Preconditions.java:92)
    at com.google.cloud.ServiceOptions.<init>(ServiceOptions.java:324)
    at com.google.cloud.datastore.DatastoreOptions.<init>(DatastoreOptions.java:85)
    at com.google.cloud.datastore.DatastoreOptions.<init>(DatastoreOptions.java:32)
    at com.google.cloud.datastore.DatastoreOptions$Builder.build(DatastoreOptions.java:75)
    at com.google.cloud.datastore.DatastoreOptions.defaultInstance(DatastoreOptions.java:123)

Here's my code:

Datastore datastore = DatastoreOptions.defaultInstance().service();     
             KeyFactory keyFactory = datastore.newKeyFactory().kind("keyKind"); 
             Key key = keyFactory.newKey("keyName"); 
             Entity entity = Entity.builder(key) 
                 .set("name", "John Doe") 
                 .set("age", 30) 
                 .set("access_time", DateTime.now()) 
                 .build(); 
             datastore.put(entity); 

How do I fix this error?

Dan McGrath
  • 41,220
  • 11
  • 99
  • 130
Gregory Was
  • 103
  • 1
  • 1
  • 6

3 Answers3

21

In my case the next code is working:

    BigQuery bigquery = BigQueryOptions.newBuilder().setProjectId("XXXXX")
            .setCredentials(
                    ServiceAccountCredentials.fromStream(new FileInputStream("key.json"))
            ).build().getService();

I set the projectId in the builder.

Manfred Radlwimmer
  • 13,257
  • 13
  • 53
  • 62
jose rivera
  • 463
  • 2
  • 6
  • 14
3

I could solve this by setting the following environment variable to project id

GCLOUD_PROJECT

if this does not work try

GCP_PROJECT

more info here https://cloud.google.com/functions/docs/configuring/env-var

Ansel Zandegran
  • 636
  • 8
  • 18
2

Are you running AE standard or AE Flexible?

Make sure you have the App Engine api jar in your classpath (WEB-INF/lib directory).

ozarov
  • 1,051
  • 6
  • 7
  • I'm running standard AE. You are right, lack of api jar in classpath was the reason of error. Thank you for help. – Gregory Was Apr 18 '16 at 19:13
  • I'm having the same issue using a custom VM on appengine flexible environment. Adding the jar to the application classpath didn't work for me. Any idea? – Fábio Uechi Sep 28 '16 at 04:23
  • So, in your case you are not using AE Flexible compat (compatible with AE APIs) but rather a "vanilla" version? Which base image are you using (https://cloud.google.com/appengine/docs/flexible/custom-runtimes/build)? Generally speaking auto-detecting project Id should be based on the steps described here https://github.com/GoogleCloudPlatform/google-cloud-java#specifying-a-project-id and this code https://github.com/GoogleCloudPlatform/google-cloud-java/blob/master/google-cloud-core/src/main/java/com/google/cloud/ServiceOptions.java#L264 – ozarov Sep 28 '16 at 15:43
  • Use the steps described here - https://cloud.google.com/appengine/docs/flexible/java/debugging-an-instance to see if any of the expected environment variables are set correctly. – ozarov Sep 28 '16 at 15:46
  • My base image is: openjdk:8-jre-alpine. The environment variables aren't there. After setting thr projectId explicitly I started getting: ComputeEngineCredentials cannot find the metadata server. This is likely because code is not running on Google Compute Engine. – Fábio Uechi Sep 28 '16 at 17:18
  • Using "gcr.io/google_appengine/openjdk8" as base image worked! Only caveat is that my final image has 600Mb now. With alpine it was 150Mb. – Fábio Uechi Sep 28 '16 at 17:49
  • Glad to hear that it works for you. All AE Flexible is running on compute Engine. My guess is that there is something that is done in the base images that are mentioned in https://cloud.google.com/appengine/docs/flexible/custom-runtimes/build related to the environment variables. – ozarov Sep 28 '16 at 19:52
  • I'm having the same issue with standard env. Deployed with "mvn appengine:update" – Ivan Balashov Jan 13 '17 at 09:10