简体   繁体   English

连接到Google App Engine数据存储区

[英]Connect to Google App Engine Datastore

I am trying to connect to Google App Engine's Datastore from my local machine in Java. 我正在尝试从Java本地计算机连接到Google App Engine的数据存储区。 I will not be using the service with an application on GAE, it will be on AWS. 我不会将该服务与GAE上的应用程序一起使用,它将在AWS上使用。

What I tried 我尝试了什么

I tried using DatastoreService datastore = DatastoreServiceFactory.getDatastoreService(); 我尝试使用DatastoreService datastore = DatastoreServiceFactory.getDatastoreService(); , but I think it is for when the application is hosted on GAE. ,但我认为应用程序是在GAE上托管的。

Right now, I have Google Storage up and running using a json credentials file fetched with the System Variable GOOGLE_APPLICATION_CREDENTIALS . 现在,我使用系统变量GOOGLE_APPLICATION_CREDENTIALS获取的json凭证文件启动并运行Google存储。 The connection works fine, so my guess is that I might have to do something similar to Storage. 连接工作正常,所以我的猜测是我可能需要做类似于存储的事情。 I did something like this for Storage : 我为Storage做了类似的事情:

HttpTransport transport = GoogleNetHttpTransport.newTrustedTransport();
        JsonFactory jsonFactory = new JacksonFactory();
        GoogleCredential credential = GoogleCredential.getApplicationDefault(transport, jsonFactory);

        if (credential.createScopedRequired()) {
            Collection<String> bigqueryScopes = StorageScopes.all();
            credential = credential.createScoped(bigqueryScopes);
        }

        Storage client = new Storage.Builder(transport, jsonFactory, credential)
                .setApplicationName("APPLICATION_NAME")
                .build();

The question 这个问题

So my question is : How can I connect to Google App Engine Datastore from outside of Google App Engine? 所以我的问题是:如何从Google App Engine外部连接到Google App Engine数据存储区?

Thank you for your help! 谢谢您的帮助!

I recommend that you move to the gcloud-java client library . 我建议你转移到gcloud-java客户端库 It is built on top of the code that you are currently using, and makes it easier to work with the Datastore and Storage, although the documentation is virtually non-existent (as of April 2016). 它建立在您当前使用的代码之上,并且使得使用数据存储区和存储更容易,尽管文档几乎不存在(截至2016年4月)。

This is how you connect to the Datastore using this library: 这是使用此库连接到数据存储区的方式:

AuthCredentials credentials = AuthCredentials.createFor(account, key);
DatastoreOptions options = DatastoreOptions.builder()
        .authCredentials(credentials)
        .projectId(projectId)
        .namespace(NAMESPACE)
        .build();
Datastore datastore = options.service();

Same for the Storage, but with StorageOptions. 与存储相同,但使用StorageOptions。

TIP: Open gcloud-java project in GitHub - the link is on the page that I referenced earlier. 提示:在GitHub中打开gcloud-java项目 - 该链接位于我之前引用的页面上。 There is a folder which contains examples of how to use Datastore and Storage. 有一个文件夹,其中包含如何使用数据存储和存储的示例。

With Andrei Volgin's awesome help, I managed to be able to connect to Datastore from an application outside of Google App Engine. 借助Andrei Volgin的大力帮助,我成功地从Google App Engine以外的应用程序连接到Datastore。 Here is what I did as of April 2016 : 这是我在2016年4月所做的事情:

  • Enable Datastore API v1beta3. 启用Datastore API v1beta3。 The default in App Engine for me was v1beta2. App Engine中的默认值为v1beta2。 https://console.cloud.google.com/apis/api/datastore.googleapis.com/overview?project=PROJECT_NAME https://console.cloud.google.com/apis/api/datastore.googleapis.com/overview?project=PROJECT_NAME
  • Get a credentials file for the service with Datastore activated. 获取已激活数据存储的服务的凭据文件。
  • You can set the credentials file in a system variable with GOOGLE_APPLICATION_CREDENTIALS=/path/to/credentials.json 您可以使用GOOGLE_APPLICATION_CREDENTIALS=/path/to/credentials.json在系统变量中设置凭证文件
  • I wrote this code as a test and the connection worked 我将此代码编写为测试并且连接正常

     DatastoreOptions options = DatastoreOptions.builder() .projectId("PROJECT_NAME") .authCredentials(AuthCredentials.createForJson( new FileInputStream("/PATH/TO/CREDENTIALS.json"))).build(); Datastore datastore = options.service(); Query<Entity> query = Query.gqlQueryBuilder(Query.ResultType.ENTITY, "SELECT * FROM user").build(); QueryResults<Entity> results = datastore.run(query); while (results.hasNext()) { Entity result = results.next(); System.out.println(result.getString("id")); } 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM