如何设置Firebase存储规则以检查Firebase数据库中的参数? [英] How can I setup Firebase Storage rules to check parameters in Firebase Database?

查看:158
本文介绍了如何设置Firebase存储规则以检查Firebase数据库中的参数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Firebase中具有以下数据结构

I have the following data structures in Firebase

Firebase数据库

Firebase Database

House/(id)/Viewers/(UID)/{
    startdate = "Dec 1, 2019"
    endData = "Dec 8, 2019"
}

Firebase存储

Firebase Storage

House/(id)/SensitiveImages/sensitiveImage.png

我想在Firebase存储中编写一条规则,该规则仅在用户UID在查看器列表内并且当前时间在startDate和endDate之间时才允许访问SensitiveImages文件夹.但是,无法从Firebase存储规则内部访问此信息.我该怎么办?

I want to write a rule in Firebase storage that only allows access to the SensitiveImages folder if the users UID is inside of the list of Viewers and the current time is between the startDate and endDate. However, there is no way to access this information from inside the Firebase Storage rules. How can I do this?

推荐答案

一种可能性是编写一个Cloud Function,用作图像请求的处理器".除了直接访问图像,我们还可以公开一个Cloud Function作为图像的代理.然后,Cloud Function可以使用当前日期和请求者身份以及存储在数据库中的数据来评估表达式.如果允许,则Cloud Function可以返回已授权Cloud Function访问的原始数据.

One possibility is to write a Cloud Function that serves as the "processor" for image requests. Rather than access the image directly, we could expose a Cloud Function as the proxy for the image. The Cloud Function could then evaluate an expression using current date and requestor identity in conjunction with the data stored in the database. If allowed, then the Cloud Function could return the raw data which the Cloud Function is authorized to access.

作为Cloud Function返回数据的替代方法,Cloud Function可以返回可用于访问数据的URL.这可能是来自Google Cloud Storage的签名URL 仅授予该URL访问者所有,并且有时间限制,以防止过期后进行访问.

As an alternative to the Cloud Function returning the data, the Cloud Function could return a URL that could be used to access the data. This could be a signed url from Google Cloud Storage that would give only the possessor of that URL access and would also be time bound to prevent access after expiration.

这篇关于如何设置Firebase存储规则以检查Firebase数据库中的参数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆