簡體   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