开发者问题收集

如何为 Firebase 存储模拟器播种?

2021-06-20
643

我正在尝试为 Firebase 存储模拟器播种。我已将 json 文件添加到我的项目中,并尝试在播种阶段将此文件上传到存储模拟器:

await firebaseAdmin
  .storage()
  .bucket()
  .upload(path.join('./data/cars.json'), {
    destination: `vehicles/cars.json`,
  });

不幸的是,这似乎不起作用,我遇到了以下错误: TypeError:无法读取未定义的属性“length”

3个回答

我使用 firebase-tools 来实现此目的。

firebaseCli.emulators.start({
  project: "project-id",
  import: "path-to-file.json"
})

仅当正确设置了 firebase 模拟器并且您尝试导入的数据具有正确的格式时,此方法才会起作用。

am1991
2021-06-23

为了使 Firebase 存储模拟器正常工作,您需要在 Admin SDK 中对其进行初始化:

firebaseAdmin.initializeApp({
  credential: firebaseAdmin.credential.cert('path/to/credential'),
  storageBucket: "bucket-name",
});
Tristan Diependael
2021-08-19

如果您使用 Firebase CLI 启动模拟器,则可以使用选项 --export-on-exit=./data-dir--import=./data-dir ,如这里所述 https://firebase.google.com/docs/emulator-suite/install_and_configure#startup

似乎还有一个 emulators:export 命令您可以尝试一下。

--export-on-exit=

Optional . Use with the Authentication, Cloud Firestore, Realtime Database or Cloud Storage for Firebase emulator. Instruct the emulator(s) to export data to a directory when shutdown occurs, as described for the emulators:export command. The export directory can be specified with this flag: firebase emulators:start --export-on-exit=./saved-data . If --import is used, the export path defaults to the same; for example: firebase emulators:start --import=./data-path --export-on-exit . Lastly, if desired, pass different directory paths to the --import and --export-on-exit flags.

--import=import_directory

Optional . Use with the Authentication, Cloud Firestore, Realtime Database or Cloud Storage for Firebase emulator. Import data saved using the --export-on-exit startup option or the emulators:export command to a running Authentication, Cloud Firestore, Realtime Database or Cloud Storage for Firebase emulator instance. Any data currently in emulator memory will be overwitten.

James Brown
2023-11-24