我刚从beta(v0.9.1)更新到v1.0.0,并在初始化时遇到了一些问题。根据{{3}},functions.config().firebase
现已弃用。这是我之前的初始化:
const serviceAccount = require('../service-account.json')
admin.initializeApp({
credential: admin.credential.cert(serviceAccount),
databaseURL: functions.config().firebase.databaseURL,
storageBucket: functions.config().firebase.storageBucket,
projectId: functions.config().firebase.projectId,
})
以下是我在升级后将其更改为:
admin.initializeApp()
首先,这似乎工作正常,dataURL和storageBucket正在被识别。但是,新缺少的credential
字段存在问题。该字段是访问Google云端存储所必需的。我在我的应用中访问了@google-cloud/storage
API,如下所示:
import { Bucket, File } from '@google-cloud/storage'
import * as admin from 'firebase-admin'
bucket: Bucket = admin.storage().bucket()
this.bucket
.upload(filepath, {
destination: uploadPath,
})
.then((fileTuple: [File]) => {
// after uploading, save a reference to the audio file in the DB
fileTuple[0]
.getSignedUrl({ action: 'read', expires: '03-17-2025' })
.then((url: [string]) => {
dbRef.child('audioFiles').push(url[0])
})
.catch(err => console.log('Error:', err))
console.log(`${filepath} uploaded to ${this.bucket.name}.`)
})
这会将文件上传到存储,然后将签名的URL插入我的实时数据库。
迁移后,这段代码会出现以下错误:
SigningError: Cannot sign data without `client_email`
由于client_email
是service-account.json
上的一个字段,我怀疑我需要重新插入credentials
,如下所示:
admin.initializeApp({
credential: admin.credential.cert(serviceAccount),
})
但是,当我尝试此操作时,会出现Error: Can't determine Firebase Database URL.
或Error: Bucket name not specified or invalid.
等错误。因此,如果我手动插入credential
,则似乎也会手动插入databaseURL
和storageBucket
。
所以问题是,我该怎么做?
答案 0 :(得分:5)
自己想出来。我必须像这样重写我的初始化调用:
const serviceAccount = require('../service-account.json')
const firebaseConfig = JSON.parse(process.env.FIREBASE_CONFIG)
admin.initializeApp({
credential: admin.credential.cert(serviceAccount),
databaseURL: firebaseConfig.databaseURL,
storageBucket: firebaseConfig.storageBucket,
projectId: firebaseConfig.projectId,
})