我使用firebase云函数为我的QR文件上传编写了以下代码
const functions = require('firebase-functions');
const qrcode = require('qrcode')
const admin = require('firebase-admin');
const spawn = require('child-process-promise').spawn;
const serviceAccount = require("./secret.json");
const gcs = require('@google-cloud/storage')();
admin.initializeApp(functions.config({
credential: admin.credential.cert(serviceAccount),
storageBucket: "https://SECRET.firebaseio.com"
}));
exports.qrcode = functions.https.onRequest((req, res) => {
const storage = admin.storage().bucket();
const dummyFile = storage.file('dummy.png')
new Promise ((resolve, reject) => qrcode.toFileStream(
dummyFile.createWriteStream()
.on('finish', resolve)
.on('error', reject),
'DummyQR'))
.then(console.log("success")) //Doing stuff here later
.catch(console.log)
res.send("<script>window.close();</script>")
});根据文档,我应该能够通过简单地调用admin.storage().bucket(); (https://firebase.google.com/docs/storage/admin/start)连接到桶,但是我得到了以下错误:
Error: Error occurred while parsing your function triggers.
Error: Bucket name not specified or invalid. Specify a valid bucket name via the storageBucket option when initializing the app, or specify the bucket name explicitly when calling the getBucket() method.所以我被困住了不知道该怎么做。如果我尝试手动输入桶admin.storage().bucket("https://SECRET.firebaseio.com");,就会得到错误
{ ApiError: Not Found
at Object.parseHttpRespBody (/user_code/node_modules/firebase-admin/node_modules/@google-cloud/storage/node_modules/@google-cloud/common/src/util.js:193:30)
at Object.handleResp (/user_code/node_modules/firebase-admin/node_modules/@google-cloud/storage/node_modules/@google-cloud/common/src/util.js:131:18)
at /user_code/node_modules/firebase-admin/node_modules/@google-cloud/storage/node_modules/@google-cloud/common/src/util.js:496:12
at Request.onResponse [as _callback] (/user_code/node_modules/firebase-admin/node_modules/retry-request/index.js:195:7)
at Request.self.callback (/user_code/node_modules/firebase-admin/node_modules/request/request.js:185:22)
at emitTwo (events.js:106:13)
at Request.emit (events.js:191:7)
at Request.<anonymous> (/user_code/node_modules/firebase-admin/node_modules/request/request.js:1157:10)
at emitOne (events.js:96:13)
at Request.emit (events.js:188:7)
code: 404,
errors: [ { domain: 'global', reason: 'notFound', message: 'Not Found' } ],
response: undefined,
message: 'Not Found' }发布于 2019-03-10 22:18:27
我也有同样的问题。我只是在初始化应用程序时添加了storageBucket名称,如果您使用的是getSignedUrl方法,则需要包含一个服务帐户,否则该url将在一周后过期(就像我的情况一样)。
const serviceAccount = require('/your/service/account/key/path');
admin.initializeApp({
credential: admin.credential.cert(serviceAccount),
storageBucket: "your-storage-bucket-name.appspot.com",
});不要忘记用
firebase deploy --only functions在命令行上
更新2020年4月6日
发布于 2018-06-07 16:32:48
看起来您没有正确初始化admin。只需调用没有参数的initializeApp以获得所有正确的默认值:
admin.initializeApp();这将使用云函数为您的项目提供的默认服务帐户。此帐户有权在不进行任何其他配置的情况下完成功能中所需的大部分操作。
发布于 2019-09-12 09:09:09
在我的例子中,这类似于这一个 (在使用firebase-admin初始化应用程序时,无法使用云存储而不指定storageBucket )。
我有以下设置:
const admin = require("firebase-admin")
admin.initializeApp()
export * from "./api/api"
export * from "./cron/cronJobs"
export * from "./posts/postsCloudFunctions"改为:
const admin = require("firebase-admin")
export * from "./api/api"
export * from "./cron/cronJobs"
export * from "./posts/postsCloudFunctions"
admin.initializeApp()问题似乎是在访问存储桶之前没有调用initializeApp()。
https://stackoverflow.com/questions/50745147
复制相似问题