如何使用firebase存储桶节点sdk

问题描述

代码超级简单

const sharp = require('sharp');
const admin = require('firebase-admin');
const fecha = require('fecha');

admin.initializeApp({
    serviceAccountId: '<service-account>.iam.gserviceaccount.com',databaseURL: "<database-url>",storageBucket: "<storage-bucket>",projectId: "<project-id>"
    });

    const storage = admin.storage();

   // console.log(storage.bucket().ref('/general/reviews/1/test.jpg'));
    console.log(storage.ref('/general/reviews/1/file.txt').putString('casacasacasa'));

我将此代码放入云函数中,但它崩溃了

 storage.bucket(...).ref is not a function at exports.madrid (/workspace/index.js:666:30) at process.nextTick (/layers/google.nodejs.functions-framework/functions-framework/node_modules/@google-cloud/functions-framework/build/src/invoker.js:100:17) at process._tickCallback (internal/process/next_tick.js:61:11)

如果我取消注释第一行,它就会去

TypeError: storage.ref is not a function at exports.madrid (/workspace/index.js:666:21) at process.nextTick (/layers/google.nodejs.functions-framework/functions-framework/node_modules/@google-cloud/functions-framework/build/src/invoker.js:100:17) at process._tickCallback (internal/process/next_tick.js:61:11)

我 100% 存储桶存在,并且 android sdk 上的相同代码可以正常工作... 我做错了什么?

解决方法

注意,如果您将 firebase-admin 用于 storage,您必须遵循文档:https://googleapis.dev/nodejs/storage/latest/

使用 firebase-adminref 函数被 file 替换,如下所示:

admin.storage().bucket().file(remotePath).save(buffer)