【问题标题】:firebase.storage is not a function error using node.jsfirebase.storage 不是使用 node.js 的函数错误
【发布时间】:2019-08-02 01:51:14
【问题描述】:

我希望将图像上传到 firebase 存储,该服务仍在运行且未弃用,我不想使用 google-cloud,我应该如何解决这个问题?

所有其他帖子都建议使用 gcloud

我可以使用实时数据库,但没有存储空间

var firebase = require('firebase  ');
app.use(express.static('public'))

var config = {
    apiKey: "xxxxxxxx",
    authDomain:  "xxxxxxxx",
    databaseURL:  "xxxxxxxx",
    projectId:  "xxxxxxxx",
    storageBucket: "xxxxxxxx",
    messagingSenderId:  "xxxxxxxx",
};
firebase.initializeApp(config);

app.get('/home', (request, response) => {
  var storageRef = firebase.storage().ref('/master/'+file.name);
  fs.readFile('public/test.png', function(err, data) {
    if (err) throw err;
    storageRef.put(data);
  });
})

TypeError: firebase.storage 不是函数

【问题讨论】:

    标签: node.js firebase firebase-storage


    【解决方案1】:

    您说您不想使用 Google Cloud,但我有一些坏消息要告诉您,这就是 Firebase 存储的全部内容 - 只是 Google Cloud 存储桶的包装器。如果您使用 Firebase 存储,您将使用 Google Cloud 存储。

    代码方面,您似乎混淆了不同的 Firebase 库。您正在使用 Javascript SDK for the FRONT END... 但您将此问题标记为 node.js - 如果您尝试从服务器执行操作,则需要使用 Javascript SDK (called firebase-admin) for Node.js

    您说您已经找到了解释如何与 Google Cloud 交互的其他答案,我不会写出完整的分步指南,而只是指出在未来朝着正确的方向前进...

    这是 Node.js 的 Firebase 存储的相关页面:https://firebase.google.com/docs/storage/admin/start

    这是服务器端上传到 Firebase 存储的官方页面(同样是真正的 Google Cloud)。它链接到the official Google Cloud docs,用于解释如何上传文件。

    ...因此,将这两件事放在一起,从 Firebase 文档中,您将获得您的存储桶参考:

    var bucket = admin.storage().bucket("my-custom-bucket");
    

    ...然后参考 Google Cloud 文档中的此代码作为上传存储桶参考的示例:

    // Imports the Google Cloud client library
    const {Storage} = require('@google-cloud/storage');
    
    // Creates a client
    const storage = new Storage();
    
    /**
     * TODO(developer): Uncomment the following lines before running the sample.
     */
    // const bucketName = 'Name of a bucket, e.g. my-bucket';
    // const filename = 'Local file to upload, e.g. ./local/path/to/file.txt';
    
    // Uploads a local file to the bucket
    await storage.bucket(bucketName).upload(filename, {
      // Support for HTTP requests made with `Accept-Encoding: gzip`
      gzip: true,
      metadata: {
        // Enable long-lived HTTP caching headers
        // Use only if the contents of the file will never change
        // (If the contents will change, use cacheControl: 'no-cache')
        cacheControl: 'public, max-age=31536000',
      },
    });
    
    console.log(`${filename} uploaded to ${bucketName}.`);
    

    【讨论】:

      猜你喜欢
      • 2017-05-12
      • 1970-01-01
      • 2019-09-27
      • 2018-07-22
      • 2016-03-10
      • 2023-03-05
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多