Cloud Storage for Firebase访问错误“admin.storage(...)。ref不是函数”

时间:2017-12-14 09:41:15

标签: node.js firebase firebase-storage firebase-admin

我正在使用Cloud Storage for Firebase,无法弄清楚如何访问存储文件

根据https://firebase.google.com/docs/storage/web/start官方指南和https://firebase.google.com/docs/storage/web/create-reference,此代码应返回根参考

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

但它引发了一个错误说

  

TypeError:admin.storage(...)。ref不是函数

的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

我的最终目标是下载文件或将pdf文件上传到存储。

3 个答案:

答案 0 :(得分:6)

您正在使用云功能和Firebase Admin SDK来尝试访问您的存储分区。您引用的入门指南是使用Web Api for Firebase而不是管理员来讨论客户端Web应用程序。那里的功能是不同的,因为它是一个不同的SDK(即使名称保持不变)。 您尝试访问的Storage对象没有ref()个功能,只有appbucket()https://firebase.google.com/docs/reference/admin/node/admin.storage.Storage

尝试直接使用Google Cloud API:https://cloud.google.com/storage/docs/creating-buckets#storage-create-bucket-nodejs

答案 1 :(得分:0)

在以下示例中,我从一个名为“ images”的现有Firestore集合中提取图像引用。我将“ images”集合与“ posts”集合交叉引用,这样我只能得到与某个帖子相关的图像。这不是必需的。

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
}

以下是合并的重要部分:

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] )

答案 2 :(得分:0)

如果您只想临时链接到 Cloud Storage Bucket 中某个文件夹中的所有图像,以下代码片段将实现它。在本例中,我查询文件夹 images/userId 下的所有图像。

exports.getImagesInFolder = functions.https.onRequest(async (req, res) => {
    const storageRef = admin.storage().bucket('gs://{YOUR_BUCKET_NAME_HERE}');
    const query = {
        directory: `images/${req.body.userId}` // query for images under images/userId
    };
    
    const [files] = await storageRef.getFiles(query)
    const urls = await Promise.all(files.map(file => file.getSignedUrl({
        action: "read",
        expires: '04-05-2042' // this is an arbitrary date
    })))

    return res.send({ urls })
})

API 文档:

PS:请记住,这可能允许任何人传递一个 userId 并查询此特定用户的所有图像。