Google Cloud CLIの設定
Google Cloud Platform (GCP) のリソースをコマンドラインから管理するための設定手順です。
インストール
Section titled “インストール”brew install --cask google-cloud-sdkインストール確認
Section titled “インストール確認”gcloud --version# ブラウザが開いてGoogleアカウントでログインgcloud auth loginアプリケーション認証(開発用)
Section titled “アプリケーション認証(開発用)”# アプリケーションのデフォルト認証情報を設定gcloud auth application-default loginプロジェクト管理
Section titled “プロジェクト管理”プロジェクト一覧の表示
Section titled “プロジェクト一覧の表示”gcloud projects list現在の設定確認
Section titled “現在の設定確認”gcloud config listプロジェクトの切り替え
Section titled “プロジェクトの切り替え”gcloud config set project PROJECT_ID例:
gcloud config set project my-awesome-project-123456設定プロファイルの管理
Section titled “設定プロファイルの管理”複数のプロジェクトを扱う場合、設定プロファイルを使うと便利です:
# 新しい設定プロファイルを作成gcloud config configurations create dev-profile
# プロファイル一覧gcloud config configurations list
# プロファイルの切り替えgcloud config configurations activate dev-profile
# プロファイルにプロジェクトを設定gcloud config set project dev-project-idCloud Storageの操作(gsutil)
Section titled “Cloud Storageの操作(gsutil)”バケット一覧の表示
Section titled “バケット一覧の表示”gsutil lsバケット内のオブジェクト一覧
Section titled “バケット内のオブジェクト一覧”gsutil ls gs://BUCKET_NAME/詳細情報付きで表示
Section titled “詳細情報付きで表示”gsutil ls -L gs://BUCKET_NAME/ファイルのアップロード
Section titled “ファイルのアップロード”# 単一ファイルgsutil cp local-file.txt gs://BUCKET_NAME/
# ディレクトリを再帰的にアップロードgsutil cp -r local-directory/ gs://BUCKET_NAME/ファイルのダウンロード
Section titled “ファイルのダウンロード”# 単一ファイルgsutil cp gs://BUCKET_NAME/file.txt ./
# ディレクトリを再帰的にダウンロードgsutil cp -r gs://BUCKET_NAME/directory/ ./その他の便利なコマンド
Section titled “その他の便利なコマンド”コンピューティングリソース
Section titled “コンピューティングリソース”# Compute Engineインスタンス一覧gcloud compute instances list
# App Engineアプリのデプロイgcloud app deploy
# Cloud Functionsのデプロイgcloud functions deploy FUNCTION_NAME --runtime python39 --trigger-httpサービスアカウント
Section titled “サービスアカウント”# サービスアカウント一覧gcloud iam service-accounts list
# キーの作成gcloud iam service-accounts keys create key.json \ --iam-account=SERVICE_ACCOUNT_EMAILよく使うエイリアス
Section titled “よく使うエイリアス”.zshrcに追加すると便利:
# プロジェクト切り替えのショートカットalias gcp-dev='gcloud config set project dev-project-id'alias gcp-prod='gcloud config set project prod-project-id'
# 現在のプロジェクトを表示alias gcp-current='gcloud config get-value project'
# バケット一覧をプロジェクト付きで表示alias gs-list='echo "Current project: $(gcloud config get-value project)" && gsutil ls'トラブルシューティング
Section titled “トラブルシューティング”# 認証情報をリフレッシュgcloud auth login --force
# アプリケーション認証のリセットgcloud auth application-default revokegcloud auth application-default loginAPIが有効になっていないエラー
Section titled “APIが有効になっていないエラー”# 必要なAPIを有効化gcloud services enable storage-api.googleapis.comgcloud services enable compute.googleapis.com