Following this example, I keep getting the error:
TypeError: firebase.storage is not a function
From this line in my code:
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.
I encounter the same problem when I test my app locally, but everything works when the project is deployed.
This way, I actually use the following workaround :
if (!firebase.storage) {
// prevent crash when working locally
return;
}
let ref = firebase.storage().ref()
// perform production stuff ...
It's a little bit curious, but it works in my case.