限制Firebase存储路径中的文件数量 [英] Limit number of files in a firebase storage path

查看:187
本文介绍了限制Firebase存储路径中的文件数量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想限制用户可以上传到Firebase存储路径的文件数量。
这是我的firebase存储规则:

$ p $ service $ firebase.storage {
match / b / jobsdbn。 appspot.com/o {
match / images / Business / {userId} / {imageId} {
允许读取
允许写入:if request.auth.uid == userId&& (request.resource == null ||((list(/ images / Business / $(username))。size()< = 5)& imageId.size()< 50&& resource.size< 10 * 1024 * 1024&& request.resource.contentType.matches('image /.*')))

}
}
}

我已经添加了列表(/ images / Business / $(username))。size()< = 5从这篇文章: https://groups.google.com/论坛/#!topic / firebase-talk / ZtJPcEJr0Mc



但是我总是得到403,不确定username是否是全局变量,使用不同的变量,仍然无法使它工作。

解决方案

如果任何人需要一个答案我找到了解决方案,通过使用firebase云函数:

$ p $ exports.getNumOfFile = functions.storage.object()。onChange(event => {
const object = event.data; // The存储对象。
const fileBucket = object.bucket; //包含文件的存储桶。
const bucket = gcs.bucket(fileBucket);
bucket.getFiles({
prefix:[path],//像你用来获取存储文件的引用的路径
},(error,files)=> {
console.log(files.length);
});
});

您可以在这里找到更多选项:

https:/ /googlecloudplatform.github.io/google-cloud-node/#/docs/storage/1.1.0/storage/bucket?method=getFiles


I want to limit the number of files that a user can upload to a firebase storage path. Here is my firebase storage rule:

   service firebase.storage {
  match /b/jobsdbn.appspot.com/o {
    match /images/Business/{userId}/{imageId} {
       allow read
       allow write: if request.auth.uid == userId && (request.resource == null || ( (list(/images/Business/$(username)).size() <= 5) && imageId.size() < 50 && request.resource.size < 10 * 1024 * 1024 && request.resource.contentType.matches('image/.*')))

    }
  }
}

I have added list(/images/Business/$(username)).size() <= 5 from this article: https://groups.google.com/forum/#!topic/firebase-talk/ZtJPcEJr0Mc

but I always get 403 and not sure if "username" is global variable or not but I have used different variables and still cannot make it work.

解决方案

In case any one needs an answer I found the solution by using firebase cloud functions:

exports.getNumOfFile = functions.storage.object().onChange(event => {
    const object = event.data; // The Storage object.
    const fileBucket = object.bucket; // The Storage bucket that contains the file.
    const bucket = gcs.bucket(fileBucket);
    bucket.getFiles({
        prefix: "[path]", //Path like what you use to get reference of your files in storage 
    }, (error, files) => {
        console.log(files.length);
    });
});

You can find more options here:

https://googlecloudplatform.github.io/google-cloud-node/#/docs/storage/1.1.0/storage/bucket?method=getFiles

这篇关于限制Firebase存储路径中的文件数量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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