How to fix error: A project ID is required for this service but could not be determined
Asked Answered
B

3

13

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?

Bunkum answered 17/4, 2016 at 6:8 Comment(0)
C
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).

Curvy answered 17/4, 2016 at 20:39 Comment(8)
I'm running standard AE. You are right, lack of api jar in classpath was the reason of error. Thank you for help.Bunkum
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?Pyrene
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 (cloud.google.com/appengine/docs/flexible/custom-runtimes/build)? Generally speaking auto-detecting project Id should be based on the steps described here github.com/GoogleCloudPlatform/… and this code github.com/GoogleCloudPlatform/google-cloud-java/blob/master/…Curvy
Use the steps described here - cloud.google.com/appengine/docs/flexible/java/… to see if any of the expected environment variables are set correctly.Curvy
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.Pyrene
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.Pyrene
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 cloud.google.com/appengine/docs/flexible/custom-runtimes/build related to the environment variables.Curvy
I'm having the same issue with standard env. Deployed with "mvn appengine:update"Happily
H
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.

Hypersensitize answered 11/5, 2017 at 17:4 Comment(1)
Google should add this to the provided examples.Ambroseambrosi
S
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

Sacaton answered 8/7, 2022 at 8:27 Comment(0)
C
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).

Curvy answered 17/4, 2016 at 20:39 Comment(8)
I'm running standard AE. You are right, lack of api jar in classpath was the reason of error. Thank you for help.Bunkum
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?Pyrene
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 (cloud.google.com/appengine/docs/flexible/custom-runtimes/build)? Generally speaking auto-detecting project Id should be based on the steps described here github.com/GoogleCloudPlatform/… and this code github.com/GoogleCloudPlatform/google-cloud-java/blob/master/…Curvy
Use the steps described here - cloud.google.com/appengine/docs/flexible/java/… to see if any of the expected environment variables are set correctly.Curvy
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.Pyrene
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.Pyrene
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 cloud.google.com/appengine/docs/flexible/custom-runtimes/build related to the environment variables.Curvy
I'm having the same issue with standard env. Deployed with "mvn appengine:update"Happily

© 2022 - 2024 — McMap. All rights reserved.