【问题标题】:Can the recently released GAE PHP runtime access the native GAE datastore?最近发布的 GAE PHP 运行时可以访问本地 GAE 数据存储吗?
【发布时间】:2013-05-17 03:46:13
【问题描述】:

Google 刚刚宣布支持 App Engine 的 PHP 运行时。我有一个使用原生 App Engine 数据存储的 Java 运行时开发的应用程序。它目前用作移动客户端的后端。我们正在研究开发一个单独的 Web 前端,它需要连接这个数据存储。从事此工作的开发人员更喜欢使用 PHP 进行开发,因此发布此公告的时机很有趣。

但是,查看文档时,我只看到对 Google Cloud SQLGoogle Cloud Storage 的引用作为“存储数据”下的选项。是否可以使用 PHP 运行时连接本机 App Engine 数据存储区?

【问题讨论】:

  • 看起来他们的云数据存储就是它。有趣的是,谷歌不想将 nosql 模型作为主要数据存储引入 php 受众;-) 用于 appengine 上的 php。
  • 有些事情确实需要时间,令人惊讶的是 ;)
  • 有可能使用Quercus on GAE,这个项目修改了流行的WordPress PHP博客软件以使用Google App Engine的Datastore作为后端。

标签: php google-app-engine google-cloud-datastore


【解决方案1】:

在 I/O 上,我们还宣布了 Cloud Datastore,目前您应该考虑如何从 PHP 应用程序访问数据存储区。

【讨论】:

  • 我知道如何activate Cloud Datastore for an existing App Engine application。但我没有看到关于在 PHP 应用程序实例中使用 Cloud Datastore 的文档,只有 getting started with Node.js, Python and Java。我错过了什么吗?
  • 随着我们将所有部分都准备好,您应该会在接下来的几周内看到更多关于此的信息。
  • 最新的 google php api 客户端添加了支持 - 查看 Google_DatastoreService 类。我很快就会尝试做一个快速演示教程。
  • Stuart 提到新的 api 具有使用数据存储区的类。在这方面有没有进一步的进展?我在 api 中看到它,但希望有一个示例 :) 目前我正在使用 Quarcus 在 Java 上运行 PHP。
【解决方案2】:
  1. 您需要为您的项目启用 Google Cloud 数据存储,请参阅https://developers.google.com/datastore/docs/activate#google_cloud_datastore_for_an_existing_app_engine_application

    注意:您不需要启用计算引擎

    确保在管理控制台上的应用程序设置云集成部分显示“项目已成功创建。有关详细信息,请参阅基础部分。'

  2. 按照有关如何获取和使用 AppEngine 上的 Google API 客户端库的说明进行操作 https://gaeforphp-blog.appspot.com/2013/08/06/using-the-google-apis-client-library-for-php-with-app-engine/

  3. 查看已解码实体密钥的附加工作示例:Guestbook: name=default_guestbook > Greeting: id=5733953138851840

<?php

const SERVICE_ACCOUNT_NAME = 'your-service-account-id@developer.gserviceaccount.com';


require_once 'libraries/google-api-php-client/src/Google_Client.php';
require_once 'libraries/google-api-php-client/src/contrib/Google_DatastoreService.php';

$client = new Google_Client();
$client->setApplicationName("your_app_id");

$key = file_get_contents('storage/your-hashed-keyid-privatekey.p12');
$client->setAssertionCredentials(
    new Google_AssertionCredentials(
        SERVICE_ACCOUNT_NAME,
        array('https://www.googleapis.com/auth/userinfo.email',
              'https://www.googleapis.com/auth/datastore'),
        $key)
);

$datastore = new Google_DatastoreService($client);

$lookup = new Google_LookupRequest();

$path1 = new Google_KeyPathElement();
$path1->setKind('Guestbook');
$path1->setName('default_guestbook');

$path2 = new Google_KeyPathElement();
$path2->setKind('Greeting');
# this is just an example check a real entity id in your datastore
# if you do not have ancestor entity you only need one (path1) element
$path2->setId('5733953138851840');

$key = new Google_Key();
$key->setPath([$path1,$path2]);

$keyArray = array();
$keyArray[] = $key;
$lookup->setKeys($keyArray);

if(array_key_exists('catchError', $_GET)){
    try{
        $result = $datastore->datasets->lookup('your_project_name', $lookup);
        var_dump($result);
    }
    catch(Google_ServiceException $e){
        echo "<pre>";
        var_dump($e);
        echo "</pre>";
    }
}
else{
    $result = $datastore->datasets->lookup('your_project_name', $lookup);
    var_dump($result);
}

【讨论】:

    【解决方案3】:

    这个库最近(由我发布) - 我希望它可以帮助人们找到这个线程。

    它使从 PHP(在 App Engine 上或不在 App Engine 上)使用 Datastore 变得更加容易。

    https://github.com/tomwalder/php-gds

    享受吧!

    【讨论】:

    • php-gds 库现在是 2.0 测试版,内置本地 GQL 支持
    • 感谢您的努力
    【解决方案4】:

    参考:https://developers.google.com/datastore/docs/concepts/gql#using_literals_sample_code

    <?php
    const APP_NAME='a-test-com';
    const SERVICE_ACCOUNT_NAME='511908@developer.gserviceaccount.com';
    $_PRIVATE_KEY=file_get_contents('data/34672c-privatekey.p12');
    require_once 'google-api-php-client/Google_Client.php';
    
    $client=new Google_Client();
    $credentials=new Google_AssertionCredentials(SERVICE_ACCOUNT_NAME,
                                                 array('https://www.googleapis.com/auth/userinfo.email',
                                                       'https://www.googleapis.com/auth/datastore'
                                                      ),
                                                 $_PRIVATE_KEY
                                                );
    $client->setAssertionCredentials($credentials);
    
    $postBody=json_encode(array('gqlQuery'=>array('allowLiteral'=>true, 'queryString'=>
              "SELECT * FROM Guestbook WHERE __key__=key(Guestbook, 'default_guestbook')"
              )));
    $httpRequest=new Google_HttpRequest('datastore/v1beta2/datasets/'.APP_NAME.'/runQuery', 'POST', null, $postBody);
    $head=array('content-type'=>'application/json; charset=UTF-8',
                'content-length'=>Google_Utils::getStrLen($postBody)
               );
    $httpRequest->setRequestHeaders($head);
    $httpRequest=Google_Client::$auth->sign($httpRequest);
    $result=Google_REST::execute($httpRequest);
    var_export($result);
    ?>
    

    插入代码:How to insert a record using the Admin console Datastore Viewer using GQL

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-10-21
      • 1970-01-01
      • 2013-09-17
      • 1970-01-01
      • 2014-06-27
      • 1970-01-01
      • 2015-02-16
      相关资源
      最近更新 更多