【发布时间】:2018-08-19 11:31:49
【问题描述】:
我正在运行一个 Firebase Functions 实例,如下所示:
import * as functions from 'firebase-functions'
import * as express from 'express'
import * as admin from 'firebase-admin'
import { MyApi } from './server'
admin.initializeApp(functions.config().firebase)
const firebaseDb: admin.database.Database = admin.database()
const app: express.Application = MyApi.bootstrap(firebaseDb).app
export const myApp = functions.https.onRequest(app)
我的实时数据库一切正常,但我无法正确集成存储。根据the docs,我需要这样设置:
var config = {
apiKey: '<your-api-key>',
authDomain: '<your-auth-domain>',
databaseURL: '<your-database-url>',
storageBucket: '<your-storage-bucket>'
};
firebase.initializeApp(config);
// Get a reference to the storage service, which is used to create references in your storage bucket
var storage = firebase.storage();
var storageRef = storage.ref();
但这对我不起作用,因为我使用的是 Admin SDK。我根本没有导入firebase 库。我尝试像这样访问存储:
const fbStorage = admin.storage()
这感觉不对。 Admin 存储方式的接口与 Firebase 客户端 SDK 的接口完全不同:
node_modules/firebase-admin/lib/index.d.ts
declare namespace admin.storage {
interface Storage {
app: admin.app.App;
bucket(name?: string): Bucket;
}
}
node_modules/firebase/index.d.ts
declare namespace firebase.storage {
interface Storage {
app: firebase.app.App;
maxOperationRetryTime: number;
maxUploadRetryTime: number;
ref(path?: string): firebase.storage.Reference;
refFromURL(url: string): firebase.storage.Reference;
setMaxOperationRetryTime(time: number): any;
setMaxUploadRetryTime(time: number): any;
}
值得注意的是,管理存储界面缺少ref() 和ref().put() 方法,因此没有任何处理上传文件的文档适用。我可以通过admin.storage().bucket().file('path/to/file.jpg') 访问我的文件,但这似乎相当迂回,我不确定我是否应该这样做。
作为一种解决方法,我尝试在 admin.initializeApp() 之上初始化一个非管理员 Firebase 应用程序 (firebase.initializeApp(config))。但是当我尝试启动给出致命错误database: Firebase: Firebase service named 'database' already registered (app/duplicate-service).的函数时@
现在我制作了一个单独的应用程序,并尝试将存储功能委托给该辅助应用程序。有没有更好的办法?
谢谢。
UDPATE(回答):
感谢 Renaud 的建议,我了解到我最初的尝试实际上是正确的。原来你应该通过admin.storage() 访问你的存储实例。接口定义确实不同,因为客户端 SDK 和管理(服务器端)SDK 满足不同的需求。如果要包含类型定义,则需要从“@google-cloud/storage”导入它们。
下面是一个基于the official docs的Bucket API使用示例:
import { Bucket } from '@google-cloud/storage'
const filename = 'path/to/myfile.mp3'
const bucket: Bucket = admin.storage().bucket()
bucket
.upload(filename, {
destination: 'audio/music/myfile.mp3',
})
.then(() => {
console.log(`${filename} uploaded to ${bucket.name}.`)
})
.catch(err => {
console.error('ERROR:', err)
})
【问题讨论】:
标签: node.js firebase google-cloud-storage google-cloud-functions firebase-storage