建構服務

使用各種 CRUD 方法建構服務。

深入探索

如需包含這個程式碼範例的詳細說明文件,請參閱下列內容:

程式碼範例

C#

如要瞭解如何安裝及使用 Datastore 模式的用戶端程式庫,請參閱這篇文章。 詳情請參閱 Datastore 模式 C# API 參考說明文件

如要驗證 Datastore 模式,請設定應用程式預設憑證。 詳情請參閱「為本機開發環境設定驗證」。

// Create an authorized Datastore service using Application Default Credentials.
_db = DatastoreDb.Create(projectId);
// Create a Key factory to construct keys associated with this project.
_keyFactory = _db.CreateKeyFactory("Task");

Go

如要瞭解如何安裝及使用 Datastore 模式的用戶端程式庫,請參閱這篇文章。 詳情請參閱 Datastore 模式 Go API 參考說明文件

如要驗證 Datastore 模式,請設定應用程式預設憑證。 詳情請參閱「為本機開發環境設定驗證」。

import (
	"context"
	"log"

	"cloud.google.com/go/datastore"
)

func createClient(projectID string) (*datastore.Client, error) {
	ctx := context.Background()
	client, err := datastore.NewClient(ctx, projectID)
	if err != nil {
		log.Fatalf("Could not create datastore client: %v", err)
	}
	// Note: call the following from main() to ensure the client
	// properly frees all resources.
	// defer client.Close()
	return client, nil
}

Java

如要瞭解如何安裝及使用 Datastore 模式的用戶端程式庫,請參閱這篇文章。 詳情請參閱 Datastore 模式 Java API 參考說明文件

如要驗證 Datastore 模式,請設定應用程式預設憑證。 詳情請參閱「為本機開發環境設定驗證」。

// Create an authorized Datastore service using Application Default Credentials.
private final Datastore datastore = DatastoreOptions.getDefaultInstance().getService();

// Create a Key factory to construct keys associated with this project.
private final KeyFactory keyFactory = datastore.newKeyFactory().setKind("Task");

Node.js

如要瞭解如何安裝及使用 Datastore 模式的用戶端程式庫,請參閱這篇文章。 詳情請參閱 Datastore 模式 Node.js API 參考說明文件

如要驗證 Datastore 模式,請設定應用程式預設憑證。 詳情請參閱「為本機開發環境設定驗證」。

// By default, the client will authenticate using the service account file
// specified by the GOOGLE_APPLICATION_CREDENTIALS environment variable and use
// the project specified by the GCLOUD_PROJECT environment variable. See
// https://googlecloudplatform.github.io/google-cloud-node/#/docs/datastore/latest/guides/authentication
const {Datastore} = require('@google-cloud/datastore');

// Creates a client
const datastore = new Datastore();

PHP

如要瞭解如何安裝及使用 Datastore 模式的用戶端程式庫,請參閱這篇文章。 詳情請參閱 Datastore 模式 PHP API 參考說明文件

如要驗證 Datastore 模式,請設定應用程式預設憑證。 詳情請參閱「為本機開發環境設定驗證」。

use Google\Cloud\Datastore\DatastoreClient;

/**
 * Create a Cloud Datastore client.
 *
 * @param string $projectId The Google Cloud project ID.
 */
function build_service(string $projectId)
{
    $datastore = new DatastoreClient(['projectId' => $projectId]);
    return $datastore;
}

Python

如要瞭解如何安裝及使用 Datastore 模式的用戶端程式庫,請參閱這篇文章。 詳情請參閱 Datastore 模式 Python API 參考說明文件

如要驗證 Datastore 模式,請設定應用程式預設憑證。 詳情請參閱「為本機開發環境設定驗證」。

from google.cloud import datastore

def create_client(project_id):
    return datastore.Client(project_id)

Ruby

如要瞭解如何安裝及使用 Datastore 模式的用戶端程式庫,請參閱這篇文章。 詳情請參閱 Datastore 模式 Ruby API 參考說明文件

如要驗證 Datastore 模式,請設定應用程式預設憑證。 詳情請參閱「為本機開發環境設定驗證」。

require "google/cloud/datastore"

datastore = Google::Cloud::Datastore.new

後續步驟

如要搜尋及篩選其他 Google Cloud 產品的程式碼範例,請參閱Google Cloud 範例瀏覽器