將應用程式連結至 Cloud Storage for Firebase 模擬器之前,請務必瞭解 Firebase Local Emulator Suite 的整體工作流程,並安裝及設定 Local Emulator Suite,以及查看其 CLI 指令。
選擇 Firebase 專案
Firebase Local Emulator Suite 會模擬單一 Firebase 專案的產品。
如要選取要使用的專案,請在啟動模擬器前,在 CLI 的目前使用的目錄中執行 firebase use。或者,您也可以將 --project 旗標傳遞至每個模擬器指令。
Local Emulator Suite 支援模擬「實際」Firebase 專案和「示範」專案。
| 專案類型 | 功能 | 搭配模擬器使用 |
|---|---|---|
| 實數 |
實際的 Firebase 專案是指您建立及設定的專案 (通常是透過 Firebase 控制台)。 實際專案含有實際資源,例如資料庫執行個體、Storage bucket、函式,或您為該 Firebase 專案設定的其他資源。 |
使用實際的 Firebase 專案時,您可以為任何或所有支援的產品執行模擬器。 對於未模擬的產品,應用程式和程式碼會與實際資源 (資料庫執行個體、儲存空間 bucket、函式等) 互動。 |
| 示範 |
Firebase 示範專案沒有「實際」Firebase 設定,也沒有任何實際資源。這類專案通常可透過程式碼實驗室或其他教學課程存取。 示範專案的專案 ID 具有 |
使用 Firebase 示範專案時,應用程式和程式碼「只會」與模擬器互動。如果應用程式嘗試與未執行模擬器的資源互動,該程式碼就會失敗。 |
建議您盡可能使用試用專案。包括以下優點:
- 設定更簡單,因為您不必建立 Firebase 專案,就能執行模擬器
- 安全性更高,因為如果程式碼不小心叫用非模擬 (正式環境) 資源,就不會發生資料變更、用量和計費
- 離線支援更完善,因為不需要連上網際網路,就能下載 SDK 設定。
檢測應用程式,以便與模擬器通訊
Android、Apple 平台和網頁 SDK
請按照下列步驟設定應用程式內設定或測試類別,以便與 Cloud Storage for Firebase 模擬器互動。
Kotlin
// 10.0.2.2 is the special IP address to connect to the 'localhost' of // the host computer from an Android emulator. val storage = Firebase.storage storage.useEmulator("10.0.2.2", 9199)
Java
// 10.0.2.2 is the special IP address to connect to the 'localhost' of // the host computer from an Android emulator. FirebaseStorage storage = FirebaseStorage.getInstance(); storage.useEmulator("10.0.2.2", 9199);
Swift
Storage.storage().useEmulator(withHost: "127.0.0.1", port: 9199)
Unity
FirebaseStorage storage = FirebaseStorage.DefaultInstance; // Connects to the Storage Emulator running on port 9199 storage.UseEmulator("127.0.0.1", 9199);
Web
const { getStorage, connectStorageEmulator } = require("firebase/storage"); const storage = getStorage(); if (location.hostname === "localhost") { // Point to the Storage emulator running on localhost. connectStorageEmulator(storage, "127.0.0.1", 9199); }
Web
var storage = firebase.storage(); if (location.hostname === "localhost") { // Point to the Storage emulator running on localhost. storage.useEmulator("127.0.0.1", 9199); }
如要使用模擬器測試由 Cloud Storage for Firebase 事件觸發的 Cloud 函式,不需額外設定。Cloud Storage for Firebase 和 Cloud Functions 模擬器同時執行時,會自動搭配運作。
Admin SDK 秒
設定 FIREBASE_STORAGE_EMULATOR_HOST 環境變數後,Firebase Admin SDK 會自動連線至 Cloud Storage for Firebase 模擬器:
export FIREBASE_STORAGE_EMULATOR_HOST="127.0.0.1:9199"
請注意,Cloud Functions 模擬器會自動偵測 Cloud Storage for Firebase 模擬器,因此在測試 Cloud Functions 和 Cloud Storage for Firebase 模擬器之間的整合時,可以略過這個步驟。系統會自動為 Cloud Storage for Firebase 中的 Admin SDK 設定環境變數。
如要將 Admin SDK 程式碼連結至在其他環境中執行的共用模擬器,則必須指定使用 Firebase CLI 設定的相同專案 ID。您可以直接將專案 ID 傳遞至 initializeApp,或設定 GCLOUD_PROJECT 環境變數。
Node.js Admin SDK
admin.initializeApp({ projectId: "your-project-id" });
環境變數
export GCLOUD_PROJECT="your-project-id"
匯入及匯出資料
資料庫和 Cloud Storage for Firebase 模擬器可讓您從執行中的模擬器執行個體匯出資料。定義一組基準資料,用於單元測試或持續整合工作流程,然後匯出資料,與團隊成員共用。
firebase emulators:export ./dir在測試中,模擬器啟動時匯入基準資料。
firebase emulators:start --import=./dir您可以指示模擬器在關閉時匯出資料,方法是指定匯出路徑,或直接使用傳遞至 --import 旗標的路徑。
firebase emulators:start --import=./dir --export-on-exit這些資料匯入和匯出選項也適用於 firebase emulators:exec 指令。詳情請參閱模擬器指令參考資料。
Cloud Storage for Firebase 模擬器與正式環境的差異
在測試用戶端應用程式時,Cloud Storage for Firebase 模擬器在 Firebase API 介面區域方面幾乎與實際工作環境完全一致。所有 Firebase 指令都應可在一般 Firebase SDK (網頁、Android 和 Apple 平台) 之間運作。
測試伺服器端應用程式時,會有一些限制。Firebase Admin SDK 會使用 Google Cloud API 介面,但並非這個 API 的所有端點都會模擬。一般來說,凡是可透過用戶端 SDK 執行的操作 (上傳或刪除檔案、取得及設定中繼資料),也可透過 Admin SDK 執行,但除此之外的操作則無法執行。以下列出值得注意的排除項目。
與 Google Cloud Storage 的差異
Cloud Storage for Firebase 產品 (包括 Storage 模擬器) 提供 Google Cloud Storage (GCS) 的部分功能,著重於儲存物件,對於開發 Firebase 應用程式非常實用。Cloud Storage for Firebase 與 GCS 的差異如下:
- Cloud Storage for Firebase 目前不支援用於建立、列出、取得或刪除儲存空間 bucket 的
BucketAPI。 - Google Cloud Storage Objects API 支援下列方法:
copy、delete、get、insert、list、patch、rewrite、update。
Cloud IAM
Firebase 模擬器套件不會嘗試複製或遵循任何與 IAM 相關的執行行為。模擬器會遵循提供的 Firebase 安全性規則,但在通常會使用 IAM 的情況下 (例如設定 Cloud Functions 叫用服務帳戶,進而設定權限),模擬器無法設定,並會使用開發機器上的全域可用帳戶,類似於直接執行本機指令碼。
Pub/Sub 通知
Cloud Storage for Firebase 模擬器未與 Cloud Pub/Sub 模擬器整合,因此不支援為儲存空間物件變更建立管道/通知。建議您直接使用 Cloud Functions Storage 觸發條件。
Bucket 層級中繼資料
Cloud Storage for Firebase 模擬器不支援任何 bucket 層級設定,包括儲存空間級別、bucket 層級 CORS 設定、標籤或保留政策。Firebase 預計會逐步改善這項支援。
後續步驟
- 如需精選影片和詳細的 How-to 範例,請觀看 Firebase 模擬器訓練播放清單。
- 觸發函式通常會與 Cloud Storage for Firebase 整合, 如要進一步瞭解 Cloud Functions for Firebase 模擬器,請參閱「 在本機執行函式」。