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

时间:2013-05-17 03:46:14

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

Google刚宣布支持App Engine的PHP运行时。我有一个使用Java运行时开发的应用程序,它使用本机App Engine数据存储区。它目前作为移动客户端的后端。我们正在研究开发一个单独的Web前端,它需要连接这个数据存储区。开发这个的开发人员更喜欢用PHP开发,所以这个公告的时间很有意思。

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

4 个答案:

答案 0 :(得分:4)

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

答案 1 :(得分:3)

  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>问候语:id = 5733953138851840

  4.   

    <?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 :(得分:2)

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

它使得使用PHP的数据存储(在App Engine上或不在App Engine上)更容易。

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

享受!

答案 3 :(得分: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);
?>

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