如何修复错误:此服务需要项目ID,但无法确定

时间:2016-04-17 06:08:41

标签: google-app-engine google-cloud-datastore gcloud gcloud-java

我正在尝试从AppEngine向Google Datastore插入数据,但我收到了错误消息:

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)

这是我的代码:

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); 

如何解决此错误?

2 个答案:

答案 0 :(得分:10)

在我的情况下,下一个代码正在运行:

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

我在构建器中设置了projectId。

答案 1 :(得分:1)

您是在运行AE标准还是AE Flexible

确保您的类路径(WEB-INF / lib目录)中有App Engine api jar