可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
Following this example, I keep getting the error:
TypeError: firebase.storage is not a function
From this line in my code:
var storageRef = firebase.storage().ref();
(And when I simply try to initialize storage from the storage guide, linked from firebase's npm site, I get the same error.)
In my Node.js project, I'm including the following libraries:
const firebase = require('firebase');
var admin = require('firebase-admin');
const fs = require('fs');
Up to this point, I've successfully been able to read from and write to the firebase database, creating a reference to the database with var db = admin.database()
, then var ref = db.ref("/")
... So I know I've configured Firebase and firebase-database correctly. But I'm stuck on storage
, and have tried both admin.storage().ref()
and firebase.storage().ref()
, and firebase.storage().ref("/")
with the same error message.
I've also tried:
var storage = firbase.storage(); var storageRef = storage.ref();
and
const app = firebase.initializeApp(config); var storage = app.storage();
and with ref()
's void argument ()
and with "/"
... but have the same message, yet to no avail.
I'm using:
- "firebase": "^3.6.4"
- "firebase-admin": "^4.0.4"
- Node.js : v6.9.1
What must I do to successfully create a reference to storage?
回答1:
According to this answer, instead of firebase storage, in Node.js, google-cloud
package storage should be used, and it seems that this answer should confirm it. Code example:
npm i --save google-cloud
Then:
const gcloud = require('google-cloud') const storage = gcloud.storage({ projectId: '', keyFilename: 'service-account-credentials.json', }); const bucket = storage.bucket('.appspot.com')
Or using only the storage part of the package:
npm install --save @google-cloud/storage
And then:
var storage = require('@google-cloud/storage')
Also check the docs for more.
回答2:
Some details to note:
- Firebase Storage is not longer used with Node.js, all documentation there is useless for Node.js. Instead use
google-cloud
. The references and guides for Firebase and Google Cloud do not reflect this as of today. - Unlike Firebase, google-cloud costs money, even for small projects.
- In my case, I'm using the
firebase-admin
SDK so I don't have to mess with user authentication at the moment.
Purpose
To create a single Node.js project which uses Firebase and Google Cloud. Why? Firebase has a useful database, among other features, and Google Cloud allows cloud file storage and retrieval.
Directions
Step 1: Project Creation
Create Firebase and Google Cloud (Storage) projects.
Step 2: Install Packages
Using npm, install firebase-admin
and google-cloud
in Node.js
project.
Note 1: I used the admin SDK, so after creating the Firebase project, you'll need to go to:
- Settings(the gear) > Project Settings > Service Accounts > Firebase Admin SDK
- Then you: Select
Node.js
> [Copy/paste the generated code into your project] > [click "Generate New Private Key"] > [download the generated json
to preferred location] > [replace "path/to...AccountKey.json"
with the path to the key you just generated]
Note 2: the generated key can be reused in firebase or google-cloud credentials.
Step 3: Firebase Setup
Once your project is created, import the firebase-admin
sdk:
The code should look like this, but filled with your info:
var admin = require("firebase-admin"); admin.initializeApp({ credential: admin.credential.cert("/path/to/generated/json/here.json"), databaseURL: "database-url-from-firebase" });
To find the databaseURL, go to 'Storage' in Firebase, and note the URL starting with gs:
and copy/paste it the the value field of databaseURL.
Next, get a reference to the database you can use:
var db = admin.database(); var ref = db.ref("/"); console.log('DB ref: ' + ref); //just to debug, if undefined, there's a problem.
To learn more about reading/writing to the datbase, follow Firebase's own documentation.
Step 4: Google-Cloud Billing Setup
After creating a project on Google Cloud, add billing information; buckets cannot be used without billing info.
Step 5: Google-Cloud Storage Setup
- Scrolling through the menu (the horizontal 3-bars), click "Storage", then "Enable Billing". Yes, you added billing info, now you need to enable it for that project's buckets.
- You should see that a bucket should already exists from your Firebase project.
- Click on menu again(3-bar icon), then > IAM & Admin > Settings
- At settings, you'll see "Project ID" which should look like "projectName-00000" or "projectName-Some#", copy that project ID
Step 6: Google Cloud in Node.js
In your index.js
:
var gcloud = require('google-cloud'); var gcs = gcloud.storage({ projectId: 'paste-that-project-id-here', keyFilename: 'paste-that-path-to-the-previously-downloaded-json-from-firebase-here' });
Now you can send a file to your storage by:
var bucket = gcs.bucket('bucket_name'); var remoteFile = bucket.file('somefile-inThisCaseASong.mp3'); var localFilename = '/Users/you/Music/somefile-inThisCaseASong.mp3'; bucket.upload(localFilename, function(err, file) { if (!err) { console.log('somefile-inThisCaseASong.mp3 is now in your bucket.'); } else { console.log('Error uploading file: ' + err); } });
Step 7: Verify
If the file is visible in Firebase Storage and Google Cloud Storage, you're done!
回答3:
When using Storage with Firebase, you're correct that you can't add buckets on the free tier. However, you DO get a bucket (just one) by default. My (eventually) successful approach was to:
Add Storage to my project in Firebase (NOT Google Cloud)
Add the Admin SDK and set up the necessary Service Account as per the Google Docs: https://firebase.google.com/docs/admin/setup?authuser=1
Add the "@google-cloud/storage" package as per the instructions on using the Admin SDK with storage: https://firebase.google.com/docs/storage/admin/start?authuser=1
Initialize the app:
admin.initializeApp({ credential: admin.credential.cert("/path/to/generated/json/here.json"), storageBucket: "folder-URL-from-Storage-page-excluding-gs://" });
Access the bucket object with (from Admin SDK docs):
const bucket = admin.storage().bucket()
Operate on the bucket with the storage library. Example:
bucket.upload('/path/file.ext', function(err, file, apiResponse) { //Do Stuff });
NOTE: I spent a couple of hours convinced it wasn't working because I didn't have a bucket, but it turned out my mistake was including the "gs://" in the path to my storage bucket when initializing.
回答4:
Does it change if you try to do it like the following?
// Initialize the default app: const app = firebase.initializeApp(appConfig) // Initialize storage from the *app* object: const storage = app.storage()
From the docs, it seems that this is an alternate method. I prefer this one, as this links the storage to the app, making the code more readable, and more functional
回答5:
It looks like they have fixed it in a new version of the library. Just run npm update
on the relevant package(s) and it should work.