如何使用firebase存储桶节点sdk
how to use firebase storage bucket node 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-admin
,ref
函数被 file
替换,如下所示:
admin.storage().bucket().file(remotePath).save(buffer)
代码超级简单
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-admin
,ref
函数被 file
替换,如下所示:
admin.storage().bucket().file(remotePath).save(buffer)