[英]How to make a Firebase Cloud function with full read/write admin powers on storage?
I have created some functions that upload and download files from firebase storage with cloud functions using the Firebase SDK and they work. 我创建了一些使用Firebase SDK使用云功能从Firebase存储上载和下载文件的功能,它们可以正常工作。 I would like for the functions to be executed as admin so that they don't need to abide by the storage rules. 我希望这些功能以admin身份执行,因此它们不需要遵守存储规则。
I have replaced the firebase SDK with the admin SDK but I found out that my firebase.storage().ref reference doesn't work anymore and by reading around some docs I have realized I now need to use the google cloud services system instead. 我已将firebase SDK替换为admin SDK,但是我发现firebase.storage()。ref引用不再起作用,通过阅读一些文档,我意识到我现在需要使用Google Cloud Services系统。
So my question is, is there a way to have a cloud function have administrator powers on the entire firebase project without having to switch to google cloud functions and if not, is there a work around to do that so that I can somehow authorize my cloud function to have full read/write powers on the entire storage? 所以我的问题是,是否有一种方法可以让云功能具有整个Firebase项目的管理员权限,而不必切换到Google云功能;如果没有,是否有解决方法,以便我可以某种方式授权我的云功能以在整个存储上具有完全的读/写能力? I am puzzled! 我很困惑!
Here is a snippet of my code: 这是我的代码片段:
const firebase = require('firebase-admin');
const functions = require('firebase-functions');
require("firebase-admin")
require("firebase")
require("firebase/storage");
var serviceAccount = require("serviceAccount.json");
var config = {
[...]
credential: firebase.credential.cert(serviceAccount)
};
firebase.initializeApp(config);
var storage = firebase.storage();
var storageRef = storage.ref(); //This returns .ref() is not a function
The Firebase client libraries are not intended to work on backend server environments. Firebase客户端库不适用于后端服务器环境。 The Firebase Admin SDK is meant for backends, but the API to access Cloud Storage is different than the client SDK. Firebase Admin SDK适用于后端,但是访问Cloud Storage的API与客户端SDK不同。 The Admin SDK just wraps the Cloud Storage server SDKs. Admin SDK仅包装了Cloud Storage服务器SDK。 So for node environments, you are actually just going to use the Cloud Storage Node.js client . 因此,对于节点环境,实际上您将只使用Cloud Storage Node.js客户端 。
When you call: 你打电话时:
const admin = require('firebase-admin')
const storage = admin.storage()
you are getting a Storage object from the node SDK. 您正在从节点SDK获取存储对象。 It doesn't have a ref() method. 它没有ref()方法。 You will need to get a Bucket object and use that instead: 您将需要获取一个Bucket对象,并使用该对象:
const bucket = storage.bucket()
From here, you should continue to use the API docs I'm linking to. 从这里开始,您应该继续使用我链接到的API文档。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.