最近发布的GAE PHP运行时是否可以访问本地GAE数据存储


Can the recently released GAE PHP runtime access the native GAE datastore?

谷歌刚刚宣布支持应用引擎的PHP运行时。我有一个使用Java运行时开发的应用程序,它利用了本机应用程序引擎数据存储。它目前充当移动客户端的后端。我们正在考虑开发一个单独的web前端,该前端需要与该数据存储接口。从事这方面工作的开发人员更喜欢用PHP进行开发,因此发布这一消息的时机很有意思。

然而,在查看文档时,我只看到在"存储数据"下的选项中提到了谷歌云SQL和谷歌云存储。是否可以使用PHP运行时接口本地应用程序引擎数据存储?

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

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

这个库是(我(最近发布的,我希望它能帮助人们找到这个线程。

它使使用PHP中的数据存储(无论是否在应用程序引擎上(变得更加容易。

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

享受吧!

引用: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 使用管理控制台数据存储查看器插入记录