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

試著忘記壹切 提交于 2021-02-07 12:14:26

问题


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.


回答1:


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.




回答2:


In the below example, I am extracting image references from an existing Firestore collection called "images". I am cross referencing the "images" collection with the "posts" collection such that I only get images that relate to a certain post. This is not required.

Docs for getSignedUrl()

const storageBucket = admin.storage().bucket( 'gs://{YOUR_BUCKET_NAME_HERE}.appspot.com' )
const getRemoteImages = async() => {
    const imagePromises = posts.map( (item, index) => admin
        .firestore()
        .collection('images')
        .where('postId', '==', item.id)
        .get()
        .then(querySnapshot => {
            // querySnapshot is an array but I only want the first instance in this case
            const docRef = querySnapshot.docs[0] 
            // the property "url" was what I called the property that holds the name of the file in the "posts" database
            const fileName = docRef.data().url 
            return storageBucket.file( fileName ).getSignedUrl({
                action: "read",
                expires: '03-17-2025' // this is an arbitrary date
            })
        })
        // chained promise because "getSignedUrl()" returns a promise
        .then((data) => data[0]) 
        .catch(err => console.log('Error getting document', err))
    )
    // returns an array of remote image paths
    const imagePaths = await Promise.all(imagePromises)
    return imagePaths
}

Here is the important part consolidated:

const storageBucket = admin.storage().bucket( 'gs://{YOUR_BUCKET_NAME_HERE}.appspot.com' )
const fileName = "file-name-in-storage" // ie: "your-image.jpg" or "images/your-image.jpg" or "your-pdf.pdf" etc.
const remoteImagePath = storageBucket.file( fileName ).getSignedUrl({
    action: "read",
    expires: '03-17-2025' // this is an arbitrary date
})
.then( data => data[0] )


来源:https://stackoverflow.com/questions/47810166/cloud-storage-for-firebase-access-error-admin-storage-ref-is-not-a-functio

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!