【GCP】プロジェクト作成してからFirestoreを使えるようにするまで

Firestoreを使えるようにしようと設定したときのメモ
前知識が何もないと全然わからんかったので。

スポンサードサーチ

プロジェクト作成したばかりだと

プロジェクト作成後、Firestoreを使いたいのでGCP上でアクセスすると下記のようなメッセージが表示されます。

現在のプロジェクトは、Datastore モードの Cloud Firestore または Cloud Datastore を使用するように設定されています。メインメニューから Datastore ページに直接移動すれば、いつでもデータベースにアクセスできます。

Cloud DatastoreとCloud Firestoreの違い等は、他者さんがいろいろ記事を書いてくださっているのでそちらを参考にしてください。
今回はCloud Firestoreを使用したいのにDatastoreへのリンクしか出てくれません。
もうこの時点で調べないとできないというモヤモヤ感がでました。(「Firestore使いたいの?このドキュメント読んでね」でも全然構わないから誘導してくれないのものだろうかと)

ドキュメントを読んで進める

ドキュメントでCloud Firestore を使ってみるがあったので、「Cloud Firestore データベースを作成する」を参考に始めます。

Firebaseコンソールでプロジェクトを追加する

1. まだ Firebase プロジェクトを作成していない場合は作成します。Firebase コンソールで [プロジェクトを追加] をクリックし、画面の指示に沿って、Firebase プロジェクトを作成するか、既存の GCP プロジェクトに Firebase サービスを追加します。

Firebase コンソールにアクセスし「プロジェクトを追加」からGCPで作成したプロジェクトを指定します。

2. Firebase コンソールの [Cloud Firestore] セクションに移動します。既存の Firebase プロジェクトを選択するよう求められます。データベース作成ワークフローに従います。

画面下部にある「すべてのFirebase機能を表示」から「Cloud Firestore」をクリックします。

ちなみに、サイドメニューだと「Firestore Database」となっているようですね?

3. Cloud Firestore セキュリティ ルールの開始モードを選択します。

ここからよくわからなくなりました。
GCPで見た文言と同じのが表示されるのみでどこから開始するんだ?っと。
「Google Cloud Consoleに移動」しても冒頭に戻るのみでした。

ちなみに、ここを無視して開発環境を設定するに進むと初期化の処理で下記エラーが発生しました。

google.api_core.exceptions.FailedPrecondition: 400 The Cloud Firestore API is not available for Datastore Mode projects.

自動アップグレード

他にドキュメントを探しているとFirestore への自動アップグレードがありました。
ここに記載されているコマンドを実行していきます。
(gcloud コマンドはGoogle Cloud CLI をインストールする などを参考に実行できるようにしておきます)

gcloud alpha firestore databases describe コマンドを使用して、データベース タイプを表示できます。出力で type フィールドの存在を確認します

$ gcloud alpha firestore databases describe

You do not currently have this command group installed.  Using it
requires the installation of components: [alpha]


Your current Cloud SDK version is: 319.0.0
Installing components from version: 319.0.0

┌──────────────────────────────────────────────┐
│     These components will be installed.      │
├───────────────────────┬────────────┬─────────┤
│          Name         │  Version   │   Size  │
├───────────────────────┼────────────┼─────────┤
│ gcloud Alpha Commands │ 2019.05.17 │ < 1 MiB │
└───────────────────────┴────────────┴─────────┘

For the latest full release notes, please visit:
  https://cloud.google.com/sdk/release_notes

Do you want to continue (Y/n)?  y

╔════════════════════════════════════════════════════════════╗
╠═ Creating update staging area                             ═╣
╠════════════════════════════════════════════════════════════╣
╠═ Installing: gcloud Alpha Commands                        ═╣
╠════════════════════════════════════════════════════════════╣
╠═ Creating backup and activating new installation          ═╣
╚════════════════════════════════════════════════════════════╝

Performing post processing steps...done.

Update done!

Restarting command:
  $ gcloud alpha firestore databases describe

ERROR: (gcloud.alpha.firestore.databases) Invalid choice: 'describe'.
Maybe you meant:
  gcloud firestore databases create
  gcloud firestore operations describe
  gcloud app describe

To search the help text of gcloud commands, run:
  gcloud help -- SEARCH_TERMS

データベースがないから作ってと怒られたので作ります
ERROR: (gcloud.alpha.firestore.databases) Invalid choice: ‘describe’. Maybe you meant: に書かれているコマンドを実行する)

$ gcloud firestore databases create

ERROR: gcloud crashed (RegionNotSpecified): You must specify a region using the --region flag to use this command. The region needs to match the Google App Engine region: --region=asia-northeast1

If you would like to report this issue, please run the following command:
  gcloud feedback

To check gcloud for common problems, please run the following command:
  gcloud info --run-diagnostics

すでに作成していたApp Engineと同じリージョンを指定しろと怒られたので指定します。

$ gcloud firestore databases create --region=asia-northeast1

Waiting for operation [apps/xxxxxx/operations/058a5b1d-534c-4906-a696-a48162cbc368] to complete...done.
Success! Selected Google Cloud Firestore Native database for xxxxxx

xxxxxx部分にはGCPのプロジェクト名が入ります)
Firestoreが作成できたようなので、GCPの方からアクセスしてみると無事使えるようになっていました。
上記の初期化処理でもエラーがでなくなりました。

正直この方法が正しいかわからないですが(開始モードの選択をスルーしているので)
とりあえず、これでFirestoreが使用できるようになりました。

以上

コメントを残す

メールアドレスが公開されることはありません。 が付いている欄は必須項目です