Firebase Functions 1.0.0迁移:带有Google Service Account凭据的自定义initializeApp()出现问题

JP *_*Lew 1 google-cloud-storage firebase google-cloud-functions firebase-admin

我刚刚从beta(v0.9.1)更新到v1.0.0,并遇到了一些初始化问题。根据迁移指南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,
})
Run Code Online (Sandbox Code Playgroud)

升级后,我将其更改为以下内容:

admin.initializeApp()
Run Code Online (Sandbox Code Playgroud)

起初,这似乎工作正常,可以识别databaseURL和storageBucket。但是,关于新缺席的credential领域存在问题。要访问Google Cloud Storage,必须填写该字段。我@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}.`)
      })
Run Code Online (Sandbox Code Playgroud)

这会将文件上传到存储,然后将签名的URL插入我的实时数据库。

迁移之后,这段代码会产生以下错误:

SigningError: Cannot sign data without `client_email`
Run Code Online (Sandbox Code Playgroud)

既然client_email是上的一个领域service-account.json,我怀疑我需要重新插入credentials,就像这样:

admin.initializeApp({
  credential: admin.credential.cert(serviceAccount),
})
Run Code Online (Sandbox Code Playgroud)

但是,当我尝试此操作时,出现类似Error: Can't determine Firebase Database URL.或的错误Error: Bucket name not specified or invalid.。因此,如果我手动插入credential,它似乎是期望值databaseURL,也storageBucket将被手动插入。

所以问题是,我该怎么做?

JP *_*Lew 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,
})
Run Code Online (Sandbox Code Playgroud)