一尘不染

使用Node.js将文件上传到Firebase存储

node.js

我试图了解如何使用Node.js在Firebase Storage中上传文件。我的第一次尝试是使用Firebase库:

"use strict";

var firebase = require('firebase');

var config = {
    apiKey: "AIz...kBY",
    authDomain: "em....firebaseapp.com",
    databaseURL: "https://em....firebaseio.com",
    storageBucket: "em....appspot.com",
    messagingSenderId: "95...6"
};

firebase.initializeApp(config);

// Error: firebase.storage is undefined, so not a function
var storageRef = firebase.storage().ref();

var uploadTask = storageRef.child('images/octofez.png').put(file);

// Register three observers:
// 1. 'state_changed' observer, called any time the state changes
// 2. Error observer, called on failure
// 3. Completion observer, called on successful completion
uploadTask.on('state_changed', function(snapshot){
    ...
}, function(error) {
    console.error("Something nasty happened", error);
}, function() {
  var downloadURL = uploadTask.snapshot.downloadURL;
  console.log("Done. Enjoy.", downloadURL);
});

但是事实证明,Firebase无法从服务器端上传文件,正如在文档中明确指出的那样:

Firebase存储不包含在服务器端Firebase npm模块中。相反,您可以使用gcloud Node.js客户端。

$ npm install --save gcloud

在代码中,您可以使用以下方式访问存储分区:

var gcloud = require('gcloud')({ ... }); var gcs = gcloud.storage();
var bucket = gcs.bucket('<your-firebase-storage-bucket>');
  • 我们可以在gcloud没有Google Cloud Platform帐户的情况下使用吗?怎么样?

  • 如果没有,怎么可能从客户端将文件上传到Firebase Storage?

  • 我们不能只创建一个从服务器端发出相同请求的库吗?

  • Firebase Storage到底如何与Google Cloud Platform连接?为什么Firebase允许我们仅从客户端上传图像?


我的第二次尝试是使用gcloud库,如文档中所述:

var gcloud = require("gcloud");

// The following environment variables are set by app.yaml when running on GAE,
// but will need to be manually set when running locally.
// The storage client is used to communicate with Google Cloud Storage
var storage = gcloud.storage({
  projectId: "em...",
  keyFilename: 'auth.json'
});

storage.createBucket('octocats', function(err, bucket) {

    // Error: 403, accountDisabled
    // The account for the specified project has been disabled.

    // Create a new blob in the bucket and upload the file data.
    var blob = bucket.file("octofez.png");
    var blobStream = blob.createWriteStream();

    blobStream.on('error', function (err) {
        console.error(err);
    });

    blobStream.on('finish', function () {
        var publicUrl = `https://storage.googleapis.com/${bucket.name}/${blob.name}`;
        console.log(publicUrl);
    });

    fs.createReadStream("octofez.png").pipe(blobStream);
});

阅读 295

收藏
2020-07-07

共1个答案

一尘不染

在服务器上使用Firebase库时,您通常会授权使用服务帐户,因为这将使您可以例如对实时数据库进行管理员访问。您可以使用同一服务帐户的凭据文件来授权gcloud

顺便说一句:Firebase项目本质上也是Google Cloud
Platform项目,您可以在https://console.firebase.google.comhttps://console.cloud.google.com以及https上访问Firebase项目
//console.developers.google.com
您可以在Firebase控制台>项目设置Cloud
Console仪表板中
查看您的项目ID

使用gcloud SDK时,请确保使用与Firebase Storage相同的存储桶(已存在)。您可以在Firebase Web
config对象或Firebase存储选项卡中找到存储桶名称。基本上,您的代码应该像这样开始:

var gcloud = require('gcloud');

var storage = gcloud.storage({
  projectId: '<projectID>',
  keyFilename: 'service-account-credentials.json'
});

var bucket = storage.bucket('<projectID>.appspot.com');

...
2020-07-07