Agent Platform Workbench クライアント ライブラリ

このページでは、Notebooks API の Cloud クライアント ライブラリの使用を開始する方法を説明します。クライアント ライブラリを使用すると、サポートされている言語から Google Cloud API に簡単にアクセスできます。サーバーにリクエストを送信してGoogle Cloud API を直接利用することもできますが、クライアント ライブラリを使用すると、記述するコードの量を大幅に削減できます。

Cloud クライアント ライブラリと以前の Google API クライアント ライブラリの詳細については、クライアント ライブラリの説明をご覧ください。

クライアント ライブラリをインストールする

C++

このクライアント ライブラリの要件と依存関係のインストールについては、 GitHub README をご覧ください。

C#

Install-Package Google.Cloud.Notebooks.V1 -Pre

詳細については、C# 開発環境の設定をご覧ください。

Go

go get cloud.google.com/go/notebooks

詳細については、Go 開発環境の設定をご覧ください。

Java

Maven を使用している場合は、次のものを pom.xml ファイルに追加します。BOM の詳細については、 Google Cloud Platform ライブラリ BOMをご覧ください

<dependencyManagement>
  <dependencies>
    <dependency>
      <groupId>com.google.cloud</groupId>
      <artifactId>libraries-bom</artifactId>
      <version>26.79.0</version>
      <type>pom</type>
      <scope>import</scope>
    </dependency>
  </dependencies>
</dependencyManagement>

<dependencies>
  <dependency>
    <groupId>com.google.cloud</groupId>
    <artifactId>google-cloud-notebooks</artifactId>
  </dependency>
</dependencies>

Gradle を使用している場合は、 次のものを依存関係に追加します。

implementation 'com.google.cloud:google-cloud-notebooks:1.89.0'

sbt を使用している場合は、 次のものを依存関係に追加します。

libraryDependencies += "com.google.cloud" % "google-cloud-notebooks" % "1.89.0"

詳細については、Java 開発環境の設定をご覧ください。

Node.js

npm install @google-cloud/notebooks

詳細については、Node.js 開発環境の設定をご覧ください。

PHP

composer require google/cloud

詳細については、Google Cloud での PHP の使用をご覧ください。

Python

Mac/Linux

pip install virtualenv
virtualenv ENVIRONMENT_NAME
source ENVIRONMENT_NAME/bin/activate
ENVIRONMENT_NAME/bin/pip install google-cloud-notebooks

Windows

pip install --upgrade google-cloud-notebooks
pip install virtualenv
virtualenv ENVIRONMENT_NAME
ENVIRONMENT_NAME\Scripts\activate
ENVIRONMENT_NAME\Scripts\pip.exe install google-cloud-notebooks

詳細については、Python 開発環境の設定をご覧ください。

Ruby

gem install google-cloud-notebooks

詳細については、Ruby 開発環境の設定をご覧ください。

認証を設定する

Google Cloud API の呼び出しを認証するために、クライアント ライブラリではアプリケーションのデフォルト認証情報(ADC)がサポートされています。このライブラリは、一連の定義済みロケーションの中から認証情報を探し、それらの認証情報を使用して API へのリクエストを認証します。ADC を使用すると、アプリケーション コードを変更することなく、ローカルでの開発や本番環境など、さまざまな環境のアプリケーションで認証情報を使用できるようになります。

本番環境では、ADC の設定方法はサービスとコンテキストによって異なります。詳細については、アプリケーションのデフォルト認証情報を設定するをご覧ください。

ローカル開発環境では、Google アカウントに関連付けられている認証情報を使用して ADC を設定できます。

  1. Google Cloud CLI をインストールします。 インストール後、 初期化するには、次のコマンドを実行して Google Cloud CLI を初期化します。

    gcloud init

    外部 ID プロバイダ(IdP)を使用している場合は、まず フェデレーション ID を使用して gcloud CLI にログインする必要があります

  2. ローカルシェルを使用している場合は、ユーザー アカウントのローカル認証情報を作成します。

    gcloud auth application-default login

    Cloud Shell を使用している場合は、この操作を行う必要はありません。

    認証エラーが返され、外部 ID プロバイダ (IdP)を使用している場合は、フェデレーション ID を使用して gcloud CLI にログインしていることを確認します。

    ログイン画面が表示されます。ログインすると、 ADC で使用されるローカル認証情報ファイルに認証情報が保存されます。

クライアント ライブラリの使用

次の例は、使用可能な言語の一部でクライアント ライブラリを使用する方法を示しています。

C++

// Copyright 2022 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

//! [all]
#include "google/cloud/notebooks/v2/notebook_client.h"
#include "google/cloud/location.h"
#include <iostream>

int main(int argc, char* argv[]) try {
  if (argc != 3) {
    std::cerr << "Usage: " << argv[0] << " project-id location-id\n";
    return 1;
  }

  auto const location = google::cloud::Location(argv[1], argv[2]);

  namespace notebooks = ::google::cloud::notebooks_v2;
  auto client = notebooks::NotebookServiceClient(
      notebooks::MakeNotebookServiceConnection());

  for (auto i : client.ListInstances(location.FullName())) {
    if (!i) throw std::move(i).status();
    std::cout << i->DebugString() << "\n";
  }

  return 0;
} catch (google::cloud::Status const& status) {
  std::cerr << "google::cloud::Status thrown: " << status << "\n";
  return 1;
}
//! [all]

Node.js

// Copyright 2020 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//      http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

'use strict';

async function main(projectId, location) {
  /**
   * TODO(developer): Uncomment these variables before running the sample.
   */
  // const projectId = 'my-project';
  // const location = 'global';

  // Imports the Google Cloud Some API library
  const {NotebookServiceClient} = require('@google-cloud/notebooks');
  const client = new NotebookServiceClient();
  async function listInstances() {
    const [instances] = await client.listInstances({
      parent: `projects/${projectId}/locations/${location}`,
    });
    for (const instance of instances) {
      console.info(`instance: ${instance.name}`);
    }
  }
  listInstances();
}

main(...process.argv.slice(2));
process.on('unhandledRejection', err => {
  console.error(err.message);
  process.exitCode = 1;
});

参考情報

C++

次のリストは、C++ のクライアント ライブラリに関連するその他のリソースへのリンクを示します。

C#

次のリストは、C# のクライアント ライブラリに関連するその他のリソースへのリンクを示します。

Go

次のリストは、Go のクライアント ライブラリに関連するその他のリソースへのリンクを示します。

Java

次のリストは、Java のクライアント ライブラリに関連するその他のリソースへのリンクを示します。

Node.js

次のリストは、Node.js のクライアント ライブラリに関連するその他のリソースへのリンクを示します。

PHP

次のリストは、PHP のクライアント ライブラリに関連するその他のリソースへのリンクを示します。

Python

次のリストは、Python のクライアント ライブラリに関連するその他のリソースへのリンクを示します。

Ruby

次のリストは、Ruby のクライアント ライブラリに関連するその他のリソースへのリンクを示します。