繁体   English   中英

如何在Node.js服务器上的函数内访问Firebase存储?

[英]How do I access Firebase Storage inside of Functions on Node.js server?

我正在运行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)

我的实时数据库一切正常,但我无法正确集成存储。 根据文档 ,我需要像这样设置:

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 /火力管理员/ lib目录/ index.d.ts

declare namespace admin.storage {
  interface Storage {
    app: admin.app.App;
    bucket(name?: string): Bucket;
  }
}

node_modules /火力/ 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) admin.initializeApp() 但是当我尝试启动给出致命错误database: Firebase: Firebase service named 'database' already registered (app/duplicate-service).函数时database: Firebase: Firebase service named 'database' already registered (app/duplicate-service).

现在我创建了一个单独的应用程序,并尝试将存储功能委派给该辅助应用程序。 有没有更好的办法?

谢谢。


UDPATE(答案):

感谢Renaud的建议,我了解到我最初的尝试实际上是正确的。 原来你应该通过admin.storage()访问你的存储实例。 接口定义确实不同,因为客户端SDK和管理员(服务器端)SDK可满足不同需求。 如果要包含类型定义,则需要从“@ google-cloud / storage”导入它们。

以下是基于官方文档如何使用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)
  })

(我希望我能正确理解您的问题)以下是如何从云功能访问云存储以流式传输文件的示例:

    const config = {
        projectId: '.....',
        keyFilename: './......json'
    };
    const storage = require('@google-cloud/storage')(config);
    const yourStorageBucket = 'xyz.appspot.com';


    const bucket = storage.bucket(yourStorageBucket);

    const file = bucket.file(filename);
    const stream = file.createWriteStream({resumable: false});
    ....

您可以访问以下网址查看更多详细信息: https//cloud.google.com/nodejs/getting-started/using-cloud-storage#uploading_to_cloud_storagehttps://firebase.google.com/docs/storage/extend-与函数

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM