BigQuery を始めるにあたって Google Cloud SDK も必要になったのでインストールから認証までをまとめました。AWS CLI を使ったことがあると何となく感覚的に分かりやすいと思います。
Google Cloud SDKのインストール
Google Cloud SDK とは Google Cloud Platform プロダクト関連のコマンドラインツールを一つにまとめたもので AWS CLI のようなものです。昔は単体でインストールする必要があったようです。
Google Cloud SDKをインストールします。途中で確認や入力を求められますので青色で注釈いれてます。
Cloud SDK — Google Cloud Platform
$ curl https://sdk.cloud.google.com | bash % Total % Received % Xferd Average Speed Time Time Time Current Dload Upload Total Spent Left Speed 100 421 0 421 0 0 640 0 --:--:-- --:--:-- --:--:-- 640 Downloading Google Cloud SDK install script: https://dl.google.com/dl/cloudsdk/channels/rapid/install_google_cloud_sdk.bash ######################################################################## 100.0% Running install script from: /var/folders/kg/_v71n_1d3s77yhxrpy02stpw0000gn/T/tmp.XXXXXXXXXX.cYL2yB57/install_google_cloud_sdk.bash which curl curl -# -f https://dl.google.com/dl/cloudsdk/channels/rapid/google-cloud-sdk.tar.gz ######################################################################## 100.0% Installation directory (this will create a google-cloud-sdk subdirectory) (/Users/hogehoge): <-- 指定したディレクトリにインストールされる mkdir -p /Users/hogehoge tar -C /Users/hogehoge -xvf /var/folders/kg/_v71n_1d3s77yhxrpy02stpw0000gn/T/tmp.XXXXXXXXXX.JuIyjKQb/google-cloud-sdk.tar.gz ... /Users/hogehoge/google-cloud-sdk/install.sh Welcome to the Google Cloud SDK! To help improve the quality of this product, we collect anonymized data on how the SDK is used. You may choose to opt out of this collection now (by choosing 'N' at the below prompt), or at any time in the future by running the following command: gcloud config set --scope=user disable_usage_reporting true Do you want to help improve the Google Cloud SDK (Y/n)? Y <-- Google Cloud SDK の改善に協力するか? ... Your current Cloud SDK version is: 0.9.82 Installing components from version: 0.9.82 ┌────────────────────────────────────────────────────────────────────────────┐ │ These components will be installed. │ ├─────────────────────────────────────────────────────┬────────────┬─────────┤ │ Name │ Version │ Size │ ├─────────────────────────────────────────────────────┼────────────┼─────────┤ │ BigQuery Command Line Tool │ 2.0.18 │ < 1 MiB │ │ BigQuery Command Line Tool (Platform Specific) │ 2.0.18 │ < 1 MiB │ │ Cloud SDK Core Libraries (Platform Specific) │ 2015.09.03 │ < 1 MiB │ │ Cloud Storage Command Line Tool │ 4.15 │ 2.7 MiB │ │ Cloud Storage Command Line Tool (Platform Specific) │ 4.14 │ < 1 MiB │ │ Default set of gcloud commands │ 2015.10.08 │ < 1 MiB │ └─────────────────────────────────────────────────────┴────────────┴─────────┘ ... Modify profile to update your $PATH and enable shell command completion? (Y/n)? Y <-- 補完を有効にするか? ...
環境変数を有効にするためにシェルを再起動します。
$ exec -l $SHELL
Googleアカウントの認証
インストールができたら初期設定をします。公式にあるとおりgcloud init
を実行してください。ここではインストールされたクライアントの OAuth フローが使われるので、認証を完了するにはブラウザを使用することになります。
$ gcloud init Welcome! This command will take you through the configuration of gcloud. Your current configuration has been set to: [default] <--- default の設定にセットされます。 To continue, you must login. Would you like to login (Y/n)? Y <-- ブラウザ起動するので認証するためにログインします。 Your browser has been opened to visit: ... You are now logged in as: [hoge.user@gmail.com] <-- ログインしたらアカウントのmailが表示されます。 Enter project id you would like to use: xxxx-xxxx-xxxx <-- プロジェクトIDを入力してください。 Your current project has been set to: [xxxx-xxxx-xxxxxx]. ... Generally projects have a repository named [default]. Would you like to try clone it? (Y/n)? Y <-- リポジトリがCloneされます。これは何の意味があるんだろう? Where would you like to clone [default] repository to [/Users/hogehoge/Documents/bigquery]: <-- Cloneするディレクトリを指定します。 Cloning into '/Users/hogehoge/Documents/bigquery/default'... warning: You appear to have cloned an empty repository. Checking connectivity... done. Git repository has been cloned to [/Users/hogehoge/Documents/bigquery/default] gcloud has now been configured! You can use [gcloud config] to change more gcloud settings. Your current configuration is: [default] ...
認証情報は以下のディレクトリに保存されています。
$ ll ~/.config/gcloud/ total 32 -rw-r--r-- 1 hogehoge staff 7 10 16 12:21 active_config -rw------- 1 hogehoge staff 203 10 16 12:22 application_default_credentials.json drwxr-xr-x 3 hogehoge staff 102 10 16 12:23 completion_cache drwxr-xr-x 3 hogehoge staff 102 10 16 12:21 configurations -rw------- 1 hogehoge staff 2281 10 16 16:10 credentials -rw------- 1 hogehoge staff 5 10 16 16:10 gce drwx------ 3 hogehoge staff 102 10 16 12:22 legacy_credentials drwxr-xr-x 3 hogehoge staff 102 10 16 11:34 logs
ブラウザの使えないサーバーや仮想マシン内での認証方法
ブラウザの使えないサーバー内でも問題なくアカウントの認証はできるようになっています。
もうひとつgcloud init
の代わりにgcloud auth login
もありますのでそちらで認証しました。init
は全て含んだ初期設定コマンドですが、auth
は認証関連のコマンドです。
$ gcloud auth login Go to the following link in your browser: https://accounts.google.com/o/oauth2/auth?xxxxxxxxxx <-- このURLをローカルマシンのブラウザで開く Enter verification code: xxxxxxxxxxxxxxxxxxxxxxxxxxx <--- 認証できたらコードが表示されるので貼り付ける Saved Application Default Credentials. You are now logged in as [hoge.user@gmail.com]. <--- 認証完了 Your current project is [None]. You can change this setting by running: $ gcloud config set project PROJECT_ID
プロジェクトIDも個別にセットしなければいけません。
$ gcloud config set project [PROJECT_ID] WARNING: Creating and activating new configuration [default]. WARNING: Importing legacy user properties.
サービスアカウントを使った認証(秘密鍵)
Googleアカウントじゃなくてサービスアカウントを使って認証をます。サービスアカウントは秘密鍵さえあれば認証が可能なのでAPIを使うときにも役に立ちます。
JSONが推奨されてるので選択します。
アカウント情報を記述しているファイルがダウンロードされますので保存しておきます。同じファイルは再度作れません。
作成できたらアカウントが追加されていますので、表示されているメールアドレスはこのあと使います。
activate-service-account ~
の後にはサービスアカウントのメールアドレスを、--key-file
にはサービスアカウントを作成した際にダウンロードした鍵ファイルを指定します。
$ gcloud auth activate-service-account 332......@developer.gserviceaccount.com --key-file my_bigquery.json Activated service account credentials for: [332......@developer.gserviceaccount.com]
アカウントの切り替えと削除
Google アカウントとサービスアカウントの両方を設定した場合、次のコマンドで切り替えられます。
$ gcloud config set account hoge.user@gmail.com
認証を削除したい場合はauth revoke
を実行します。現在アクティブなアカウントが削除されますのでauth list
で確認して実行しましょう。
$ gcloud auth list Credentialed accounts: - 332......@developer.gserviceaccount.com - hoge.user@gmail.com (active) $ gcloud auth revoke Revoked credentials for hoge.user@gmail.com.
認証後の確認
BigQuery や GoogleCloudStorage のls
コマンドを実行してみましょう。認証が通っていればデフォルトのプロジェクト内のリストが表示されます。
$ bq ls datasetId ----------- sample $ gsutil ls gs://my_bucket/
参考書籍
オライリージャパン
売り上げランキング: 166,295