简体   繁体   English

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

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

I am using Google app engine for my web app and I need to use a NoSQL database, so my best option is Google Cloud Datastore 我正在为我的网络应用程序使用Google应用程序引擎,我需要使用NoSQL数据库,所以我最好的选择是Google Cloud Datastore

Since I can't find a way to connect it with php I can't use it. 由于我找不到与php连接的方法,我无法使用它。 In the official documentation php is not mentioned. 官方文档中没有提到php I want to make sure that is there a way to access it with php ? 我想确保有没有办法用PHP访问它?

This library may help people finding this thread 该库可以帮助人们找到这个帖子

Designed to make Google Datatore easy from PHP. 旨在通过PHP轻松实现Google Datatore。

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

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

First, install the official Google Cloud Datastore PHP API using Composer (a dependency manager). 首先,使用Composer (依赖管理器)安装官方Google Cloud Datastore PHP API。 You can do this by adding "google/cloud-datastore": "^1.0" to the 'require' section of your composer.json and running composer update 您可以通过将"google/cloud-datastore": "^1.0"到composer.json的“require”部分并运行composer update

You can start the local Datastore emulator with the command: 您可以使用以下命令启动本地数据存储模拟器:

gcloud beta emulators datastore start

Here's a helper class I wrote that handles connecting to the datastore: 这是我编写的一个帮助连接到数据存储区的辅助类:

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

Here's how you would use it to insert a new entity into the datastore 以下是如何使用它将新实体插入数据存储区

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

If you run into issues with the emulator, try deploying your code to App Engine and seeing if you have the same error. 如果您遇到模拟器问题,请尝试将代码部署到App Engine并查看是否存在相同的错误。 The local development environment can be flaky. 当地的发展环境可能不稳定。

Also, check out the official PHP Datastore API docs here . 另外,请在此处查看官方PHP数据存储API文档

Include google/cloud-datastore library via composer 通过composer包含google/cloud-datastore

$ composer require google/cloud-datastore

and you can use as Example below. 你可以使用下面的例子。

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

More details: https://github.com/GoogleCloudPlatform/google-cloud-php#google-cloud-datastore-ga 更多详情: 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