Cloud Storage for Firebase access error "admin.storage(...).ref is not a function"

Abdullah picture Abdullah · Dec 14, 2017 · Viewed 11.3k times · Source

I am working with Cloud Storage for Firebase and can't figure out how to to access storage files

According to https://firebase.google.com/docs/storage/web/start official guide and https://firebase.google.com/docs/storage/web/create-reference this code should be returning root reference

let admin = require('firebase-admin')
admin.initializeApp({...})
let storageRef = admin.storage().ref()

But it throws an error saying

TypeError: admin.storage(...).ref is not a function

package.json

{
  "name": "functions",
  "description": "Cloud Functions for Firebase",
  "scripts": {...},
  "dependencies": {
    "@google-cloud/storage": "^1.5.1",
    "firebase": "^4.8.0",
    "firebase-admin": "~5.4.2",
    "firebase-functions": "^0.7.1",
    "pdfkit": "^0.8.3",
    "uuid": "^3.1.0"
  },
  "private": true
}

node -v => v7.7.4

My end goal it to download files or upload a pdf file to storage.

Answer

Raeglan picture Raeglan · May 20, 2018

You are using Cloud Functions and the Firebase Admin SDK to try and access your bucket. The Getting Started guide you cited is talking about client sided Web Apps with the Web API for Firebase not the Admin one. The functionality there is different, because it uses a different SDK (even if their names are the same).

The Storage Object you are trying to access doesn't have the ref() functions, only app and bucket().

https://firebase.google.com/docs/reference/admin/node/admin.storage.Storage

Try using the Google Cloud APIs directly:

https://cloud.google.com/storage/docs/creating-buckets#storage-create-bucket-nodejs

-

EDIT: This edit is only to stop scaring people with an post from two years ago. The answer above still applies as of May 2020.