最近发布的GAE PHP运行时能否访问本地的GAE数据存储?

3
谷歌刚刚宣布支持 App Engine 的 PHP 运行时。我有一个使用 Java 运行时开发的应用程序,利用原生的 App Engine 数据存储。它目前作为移动客户端的后端运行。我们正在研究开发一个独立的 Web 前端,需要与这个数据存储接口。负责此任务的开发人员更喜欢使用 PHP 进行开发,因此这一公告的发布时间很有趣。
然而,在查看文档时,我只看到了在“存储数据”下作为选项的Google Cloud SQLGoogle Cloud Storage的引用。是否可能使用 PHP 运行时与原生的 App Engine 数据存储进行接口?

1
看起来它们使用的是Cloud Datastore。有趣的是,Google并不想将NoSQL模型作为PHP在AppEngine上的主要数据存储介绍给PHP受众群体。;-) - Tim Hoffman
有些事情需要时间,令人惊讶地(需要时间) ;) - Stuart Langley
有可能使用在GAE上的Quercus,这个项目修改了流行的WordPress PHP博客软件,以使用Google App Engine的Datastore作为后端。 - eQ19
4个回答

4
在I/O大会上,我们还宣布了Cloud Datastore,目前它是你在PHP应用中访问数据存储的方式。

我知道如何为现有的App Engine应用程序激活Cloud Datastore。但是我没有看到关于在PHP应用实例中使用Cloud Datastore的文档,只有Node.js、Python和Java入门指南。我有什么遗漏吗? - Jeff Lockhart
在接下来的几周中,随着我们逐步就位,您应该期望看到更多关于此事的信息。 - Stuart Langley
1
最新的Google PHP API客户端已经添加了支持 - 请查看Google_DatastoreService类。我会尽快做一个快速演示教程。 - Stuart Langley
Stuart提到新的API有类可以使用Datastore。这方面有更多进展吗?我在API中看到了它,但很想看一个例子 :) 目前我正在使用Quarcus在Java上运行PHP。 - Sean Brennan

3
  1. 您需要为您的项目启用Google Cloud datastore,请参阅https://developers.google.com/datastore/docs/activate#google_cloud_datastore_for_an_existing_app_engine_application

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

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

  2. 按照使用Google API客户端库的AppEngine的说明进行操作 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);
}

2

最近我发布了这个库 - 希望能帮助到找到这个线程的人。

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

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

祝使用愉快!


php-gds库现在处于2.0测试版,内置本地GQL支持。 - Tom
感谢您的努力。 - MURATSPLAT

0

参考资料: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);
?>

插入代码:如何使用GQL在管理控制台数据存储查看器中插入记录


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接