LoginSignup
1
0

More than 1 year has passed since last update.

GCP Cloud Funtions + Firebase

Posted at

Cloud function develop

GCP Cloud Function & Firebase 開発環境整備

ちょっとした縁でFirebaseを使ったプロジェクトのBackend構築を
お手伝いすることになりました。開発環境構築内容をまとめます。

Firebase:Firestore 一般内容Study

Firestoreの基本は collection -> document -> collenction -> document .. という構造でdocumentに複数のField(Json)を保存することができるものです。このDocumentをCollectionにまとめて階層的に管理していくというのが基本的な使い方です。

SQLDBのように最初にTable定義をする必要はなく、上記の階層を指定して、データを書き込めば途中のcollection, documentは動的に生成されます。

FirebaseのBackend処理パターン

Firestoreに変更が加わったことに応じて処理をBackendで行いたいということが今回実現したいことです。

Backend処理を行うときに下記の3パターンが想定されます
1. GCP Scheduler にて定期的に GCP CloudFucntion(python/Node)を呼び出す
2. Firebase firestore 変更Trigger にて Firebase Cloud Function(Node)を呼び出し処理を行う
3. Firebase firestore 変更Trigger にて Firebase Cloud Function(Node)が、GCP CloudFunction(python) を呼び出し処理を行う

無料な状態ではとれるのは1のみ。 有料にしたときは処理のメイン内容により 2 or 3 を選んでいくのが良いと思います。

一般的に、画像処理、機械学習等はPythonで記述したほうがライブラリが豊富なので有利。
それ以外は実装担当者のスキルにより選んでいけば良いと思います。

参考

GCP CloudFUnctions (python) + Firebase SDK

GCP CloudFuntionから対象のFirebaseにアクセスするために FirebaseAdminSDK用の接続設定jsonが必要である。

  1. 対象のFirebaseをひらき→プロジェクトの設定→サービスアカウントタブ を開く
  2. 新しい秘密鍵を生成する(安全に管理しましょう)
  3. 下記のPythonの例を参考に必要なLibをInstall、生成した秘密鍵を渡して 読み書きできるようにする

Let's enjoy firestore programming!

1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0