如何在本地开发中配置使用Firebase Storage Emulator
Firebase本地Storage模拟器配置方案
你当前的配置缺失了Storage模拟器的指向声明,Firebase默认会请求云端存储服务,添加对应配置即可解决问题。
配置修改步骤
1. 新增Storage模拟器指向
在你现有的模拟器配置代码后,新增Storage模拟器的声明即可,同时可顺手修正原代码中functions模拟器地址少冒号的拼写错误,修改后的完整代码如下:
import firebase from "firebase/app" import "firebase/auth" import "firebase/firestore" import "firebase/functions" import "firebase/storage" import "firebase/database" firebase.initializeApp({ apiKey: "-------------------", authDomain: "http://localhost:9099", databaseURL: "http://localhost:9000?ns=project-name", projectId: "project-name", storageBucket: "default-bucket", messagingSenderId: "--------------", appId: "----------------------", measurementId: "-----------------", }) firebase.firestore().settings({ host: "localhost:8080", ssl: false }) firebase.auth().useEmulator("http://localhost:9099") // 修正原代码的functions模拟器地址拼写错误 firebase.functions().useEmulator("http://localhost:5001") firebase.database().useEmulator("http://localhost:9090") // 新增Storage模拟器配置,默认端口为9199 firebase.storage().useEmulator("localhost", 9199)
2. 前置依赖确认
- 确保你的
firebase-tools版本≥9.11.0,Web端firebaseSDK版本≥8.14.0,这两个版本开始正式支持Storage模拟器 - 启动模拟器时执行
firebase emulators:start,确认控制台输出中Storage模拟器的监听端口为9199,如果你自定义过端口,要把上面代码里的9199替换为你自定义的端口
验证效果
配置完成后重新启动本地应用,上传文件时请求地址应该变为http://localhost:9199/v0/b/default-bucket/o?name=test-file.csv,代表已经成功连接本地Storage模拟器。
内容的提问来源于stack exchange,提问作者DahWaR
相关产品推荐
相关产品推荐

