繁体   English   中英

如何使用php访问Google Cloud数据存储区?

[英]How to access Google Cloud datastore with php?

我正在为我的网络应用程序使用Google应用程序引擎,我需要使用NoSQL数据库,所以我最好的选择是Google Cloud Datastore

由于我找不到与php连接的方法,我无法使用它。 官方文档中没有提到php 我想确保有没有办法用PHP访问它?

该库可以帮助人们找到这个帖子

旨在通过PHP轻松实现Google Datatore。

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

从PHP访问Google Cloud Datastore没有记录,但您应该像使用任何其他Google API一样使用Google API Client Library for PHP

首先,使用Composer (依赖管理器)安装官方Google Cloud Datastore PHP API。 您可以通过将"google/cloud-datastore": "^1.0"到composer.json的“require”部分并运行composer update

您可以使用以下命令启动本地数据存储模拟器:

gcloud beta emulators datastore start

这是我编写的一个帮助连接到数据存储区的辅助类:

<?php
// Datastore.php
use Google\Cloud\Datastore\DatastoreClient;

class Datastore {
    private static $ds;

    /**
     * Creates or returns the Datastore instance
     *
     * @return void
     */
    public static function getOrCreate() {
        if (isset(Datastore::$ds)) return Datastore::$ds;

        // gcloud beta emulators datastore start --data-dir=_datastore
        if (Datastore::isDevEnv() == true) {
            putenv('DATASTORE_EMULATOR_HOST=http://localhost:8081');
            // To run locally, you may still need to download a credentials file from console.cloud.google.com
            //putenv('GOOGLE_APPLICATION_CREDENTIALS=' . __DIR__ . '/datastore_creds.json');            
        }

        $datastore = new DatastoreClient([
            'projectId' => Core::getProjectId()  
            // 'keyFilePath' => 'datastore_creds.json'
        ]);

        Datastore::$ds = $datastore;
        return Datastore::$ds;
    }

    /**
     * Returns true if server running in development environment.
     *
     * @return boolean
     */
    static function isDevEnv() {
        if (isset(Core::$_isDevEnv)) return Core::$_isDevEnv;
        Core::$_isDevEnv = (strpos(getenv('SERVER_SOFTWARE'), 'Development') === 0);
        return Core::$_isDevEnv;
    }

    /**
     * Formats fields and indexes for datastore.
     * @param Datastore $ds
     * @param Key $entityKey Datastore key.
     * @param [] $fields
     * @param [string] $indexes Keys to index.
     * @return Entity
     */
    static function entityWithIndexes(DatastoreClient $ds, $entityKey, $fields, $indexes = []) {
        // Create exclude from indexes array.
        $excludedIndexes = [];
        foreach ($fields as $key => $value) {
            if (in_array($key, $indexes) == false) {
                $excludedIndexes[] = $key;
            }
        }

        $entity = $ds->entity($entityKey, $fields, [
            'excludeFromIndexes' => $excludedIndexes
        ]);
        return $entity;
    }
}

以下是如何使用它将新实体插入数据存储区

require 'vendor/autoload.php';
require 'Datastore.php';    
$ds = Datastore::getOrCreate();
$key = $ds->key('MyEntityType');
$data = [
      'name' => 'Foo!'
];
$indexes = ['name'];
$entity = Datastore::entityWithIndexes($ds, $key, $data, $indexes);
$ds->insert($entity);

如果您遇到模拟器问题,请尝试将代码部署到App Engine并查看是否存在相同的错误。 当地的发展环境可能不稳定。

另外,请在此处查看官方PHP数据存储API文档

通过composer包含google/cloud-datastore

$ composer require google/cloud-datastore

你可以使用下面的例子。

<?php 
require 'vendor/autoload.php';

use Google\Cloud\Datastore\DatastoreClient;

$datastore = new DatastoreClient([
    'projectId' => 'my_project'
]);

// Create an entity
$bob = $datastore->entity('Person');
$bob['firstName'] = 'Bob';
$bob['email'] = 'bob@example.com';
$datastore->insert($bob);

// Update the entity
$bob['email'] = 'bobV2@example.com';
$datastore->update($bob);

// If you know the ID of the entity, you can look it up
$key = $datastore->key('Person', '12345328897844');
$entity = $datastore->lookup($key);

更多详情: https//github.com/GoogleCloudPlatform/google-cloud-php#google-cloud-datastore-ga

暂无
暂无

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

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