![](/img/trans.png)
[英]How do I get the default bucket name in Firebase, from inside Node.js?
[英]How to get the default bucket name in Firebase Functions?
我正在尝试从 function 中获取 Firebase 中的默认存储桶。 最近 SDK 似乎发生了变化,我再也找不到有关检索它的信息了。 我已经尝试了一百万件事,我当前的代码看起来像这样并返回一个空字符串。
import * as admin from "firebase-admin";
import * as functions from "firebase-functions";
admin.initializeApp({
credential: admin.credential.applicationDefault(),
});
export const processVideo = functions.https.onCall(async (data, context) => {
const bucket = admin.storage().bucket.name;
functions.logger.log("***************Bucket Name****************", bucket);
});
该文档建议您在初始化 SDK 时应该将该值提供给它。 SDK 不仅不知道就知道。
您可以在初始化 Admin SDK 时指定默认存储桶名称。 然后,您可以检索到此存储桶的经过身份验证的引用。 存储桶名称不得包含 gs:// 或任何其他协议前缀。 For example, if the bucket URL displayed in the Firebase console is gs://bucket-name.appspot.com, pass the string bucket-name.appspot.com to the Admin SDK.
initializeApp({ credential: cert(serviceAccount), storageBucket: '<BUCKET_NAME>.appspot.com' });
因此,您必须从 Firebase 控制台获取值并使用它来初始化 SDK。
此外,您需要使用名为bucket()
的方法,而不是属性。 API 文档说它返回一个桶object。
admin.storage().bucket()
事实证明,信息存储在环境变量中,但没有直接导入管理工具。 因此,我发现准备环境以提供默认存储桶名称的最佳方法如下:
const firebaseConfig = JSON.parse(process.env.FIREBASE_CONFIG!);
admin.initializeApp({
credential: admin.credential.applicationDefault(),
storageBucket: firebaseConfig.storageBucket,
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.