【问题标题】:How to limit file uploads by time on Firebase Storage如何在 Firebase 存储上按时间限制文件上传
【发布时间】:2022-01-05 11:32:44
【问题描述】:

我正在尝试编写一个规则来阻止恶意用户快速更新他们的个人资料图片。

我在docs找到了这个例子

// Allow reads if the resource was updated less than an hour ago
allow read: if resource.updated < request.time + duration.value(60, "m")

我想做类似的事情,我想让用户仅在自上次更新后经过 x 时间时才更新文件。

假设我不想让用户每小时更新他们的头像超过一次,我写了这条规则。

request.time >= resource.updated + duration.value(1, "h")

此规则每次都阻止写入,为了测试建议我将持续时间设置为 10 秒 duration.value(10, "s"),但即使我等待 10 分钟,上传仍然失败。

我不确定为什么这不起作用,我知道问题出在这条规则上,因为当我删除它时一切都按预期工作。

这是我对write的规则

allow write: if request.resource.size < 5 * 1024 * 1024
                   && request.resource.contentType.matches('image/.*')
                   // Only allow uploads once every hour
                   && request.time >= resource.updated + duration.value(1, "h")
                   // Require Authentication 
                   && request.auth != null && uid == request.auth.uid
                   // Don't allow anonymous users
                   && request.auth.token.firebase.sign_in_provider != 'anonymous';

文档没有显示写作示例,我开始怀疑这是否仅适用于read

【问题讨论】:

    标签: firebase-storage


    【解决方案1】:

    将其更改为“=”并使用 resource.data:

    allow write: if request.time < resource.data.updatedAt + duration.value(1, 'h')
    

    【讨论】:

    • 使用 resource.data.updatedAt 不起作用,我想在 x 小时后而不是 x 小时内允许更新,所以 >= 是正确的条件。 firebase 团队在去年的 firebase 峰会上确认这是不可能的,但我被告知他们会将此视为功能请求
    • 正如目前所写,您的答案尚不清楚。请edit 添加其他详细信息,以帮助其他人了解这如何解决所提出的问题。你可以找到更多关于如何写好答案的信息in the help center
    猜你喜欢
    • 2018-11-27
    • 2018-05-30
    • 2021-12-02
    • 2020-04-08
    • 2017-02-19
    • 2018-10-13
    • 1970-01-01
    • 2020-09-27
    • 2017-01-25
    相关资源
    最近更新 更多