"projectId必须与以下模式匹配"在Eclipse的Google Cloud工具上运行Objectify时出现异常 [英] "projectId must match the following pattern" exception when running Objectify on Google Cloud tools for Eclipse

查看:116
本文介绍了"projectId必须与以下模式匹配"在Eclipse的Google Cloud工具上运行Objectify时出现异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用objectify在Google Cloud上运行一个非常简单的hello world应用程序,但是在尝试访问数据存储以保存实体时出现异常.

I am trying to run a very simple hello world application on Google Cloud with objectify but get an exception when trying to access the datastore for saving an entity.

我正在将最新的Google Cloud Tools(1.6.1)用于Eclipse(Oxygen 4.7.3a)和Java 8. 按照官方的 Google快速指南指南,我能够创建一个标准的Java项目并运行来自eclipse的本地服务器上的hello word示例应用程序.由于插件允许您将Objectify库添加到项目中,因此我决定尝试一下.这是我编写的用于定义实体并将其保存到数据存储区的代码.

I am using the latest Google Cloud Tools (1.6.1) for eclipse (Oxygen 4.7.3a) and Java 8. Following the official Google quick star guide I was able to create a standard java project and run the hello word sample app on my local server from eclipse. Since the plugin let you add Objectify libraries to the project I decided to give it a try. Here is the code that I wrote to define an Entity and try to save it to the datastore.

//HelloAppEngine.java
package app;

import java.io.IOException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.googlecode.objectify.ObjectifyService;
import static com.googlecode.objectify.ObjectifyService.ofy;

@WebServlet(
    name = "HelloAppEngine",
    urlPatterns = {"/hello"}
)
public class HelloAppEngine extends HttpServlet {

  @Override
  public void doGet(HttpServletRequest request, HttpServletResponse response) 
      throws IOException {
      
      Car porsche = new Car("2FAST", 4);
      ofy().save().entity(porsche).now();    // async without the now()

      assert porsche.id != null;    // id was autogenerated

      Car fetched2 = ofy().load().type(Car.class).id(porsche.id).now();
      
    response.setContentType("text/plain");
    response.setCharacterEncoding("UTF-8");

    response.getWriter().print("Hello App Engine!\r\n");
    
    response.getWriter().print(porsche.id);
  }
  
  public void init()  {
      ObjectifyService.init();
      ObjectifyService.register(Car.class);
  }
}

//Car.java
package app;

import com.googlecode.objectify.annotation.Entity;
import com.googlecode.objectify.annotation.Id;
import com.googlecode.objectify.annotation.Index;

@Entity
public class Car {
    @Id Long id;
    @Index String license;
    int color;
    
    Car(String lic, int c) {
        license = lic;
        color = c;
    }
}

在本地主机上以App Engine标准运行项目时,出现以下异常:

When running the project as App Engine standard on localhost I got the following exception:

java.lang.IllegalArgumentException: projectId must match the following pattern: ([a-z\d\-]{1,100}~)?([a-z\d][a-z\d\-\.]{0,99}:)?([a-z\d][a-z\d\-]{0,99})
at com.google.common.base.Preconditions.checkArgument(Preconditions.java:122)
at com.google.cloud.datastore.Validator.validateDatabase(Validator.java:42)
at com.google.cloud.datastore.BaseKey$Builder.<init>(BaseKey.java:58)
at com.google.cloud.datastore.KeyFactory.<init>(KeyFactory.java:35)
at com.google.cloud.datastore.DatastoreHelper.newKeyFactory(DatastoreHelper.java:58)
at com.google.cloud.datastore.DatastoreImpl.newKeyFactory(DatastoreImpl.java:466)
at com.googlecode.objectify.impl.Keys.createRawIncomplete(Keys.java:179)
at com.googlecode.objectify.impl.KeyMetadata.getIncompleteKey(KeyMetadata.java:184)
at com.googlecode.objectify.impl.KeyMetadata.setKey(KeyMetadata.java:153)
at com.googlecode.objectify.impl.KeyPopulator.save(KeyPopulator.java:29)
at com.googlecode.objectify.impl.translate.ClassPopulator.save(ClassPopulator.java:156)
at com.googlecode.objectify.impl.translate.ClassTranslator.saveSafe(ClassTranslator.java:131)
at com.googlecode.objectify.impl.translate.NullSafeTranslator.save(NullSafeTranslator.java:31)
at com.googlecode.objectify.impl.EntityMetadata.save(EntityMetadata.java:113)
at com.googlecode.objectify.impl.WriteEngine.save(WriteEngine.java:69)
at com.googlecode.objectify.impl.SaverImpl.entities(SaverImpl.java:60)
at com.googlecode.objectify.impl.SaverImpl.entity(SaverImpl.java:35)
at app.HelloAppEngine.doGet(HelloAppEngine.java:26)

知道我在这里想念什么吗? 只要在本地开发服务器上运行,AFAIK就不需要项目ID.

Any idea what am I missing here? AFAIK as long as I run on the local dev server I don't need a project id.

推荐答案

首先,如您所见,在使用本地仿真器时,确实需要提供项目ID.您不能简单地忽略它.在Eclipse中,转到 运行>运行配置> App Engine> App Engine本地服务器,然后选择运行配置的 Cloud Platform 标签.在该选项卡中,选择一个项目.这将分配要在本地运行中使用的项目ID.选择哪个项目都没有关系.您实际上不会连接到它.

First, you do need to provide a project ID when working with the local emulator, as you discovered. You can't simply omit it. In Eclipse go to Run > Run Configurations > App Engine > App Engine Local Server and select the Cloud Platform tab of your run configuration. In that tab, select a project. This will assign a project ID to be used in the local run. It doesn't matter which project you select. You won't actually connect to it.

或者,如果您没有登录或没有Cloud Project,则可以在运行配置的环境"标签中将GOOGLE_CLOUD_PROJECT环境变量设置为合法字符串,例如MyProjectId.

As an alternative if you aren't logged in or don't have a Cloud Project, you can instead set the GOOGLE_CLOUD_PROJECT environment variable to a legal string such as MyProjectId in the environment tab of the run configuration.

除此之外,Objectify 6.0似乎在dev_appserver中与捆绑的数据存储模拟器一起使用时出现问题.它确实可以与基于Beta gcloud的数据存储区模拟器一起使用.要使用它,请启动终端并运行

Beyond that, Objectify 6.0 seems to have an issue working with the bundled datastore emulator in the dev_appserver. It does work with the beta gcloud based datastore emulator. To use that instead, launch a terminal and run

$ gcloud beta emulators datastore start

仿真器启动时,您会看到如下消息:

As the emulator starts up, you will see a message like this one:

[datastore] API endpoint: http://localhost:8081
[datastore] If you are using a library that supports the DATASTORE_EMULATOR_HOST environment variable, run:
[datastore] 
[datastore]   export DATASTORE_EMULATOR_HOST=localhost:8081
[datastore] 
[datastore] Dev App Server is now running.

您需要在环境"选项卡中将DATASTORE_EMULATOR_HOST环境变量添加到Eclipse运行配置中.在此示例中,您将名称设置为DATASTORE_EMULATOR_HOST,并将值设置为localhost:8081.

You need to add the DATASTORE_EMULATOR_HOST environment variable to your Eclipse run configuration in the Environment tab. In this example, you'd set name to DATASTORE_EMULATOR_HOST and value to localhost:8081.

这篇关于"projectId必须与以下模式匹配"在Eclipse的Google Cloud工具上运行Objectify时出现异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆