首页 > 解决方案 > 如何知道文件名在firebase云存储中是否包含其他字符串?

问题描述

我想检查上传的图片名称是否包含"thumb_"。如果文件名包含 "thumb_",则用户不允许上传图片。类似的东西:

 match/users/{id}/{image}{
      if:  request.resource.contentType.matches('image/.*') &&
                                 request.resource.size < 1024 *1024 * 2  &&
                                 image.size()<32 &&
                                 !image.includes('thumb_')}

但它给出了错误

Error running simulation — Error: simulator.rules line [13], column [27]. Function not found error: Name: [includes].

我们不能includes()在安全规则中使用函数。那么我应该使用什么来代替 includes()

标签: firebase-storagefirebase-security

解决方案


根据Cloud Storage 安全规则的 API 文档,在字符串数据上没有称为“包含”的方法。您可以看到有一个名为“matches”的方法可以让您对字符串执行正则表达式匹配。查看 inimage中的字符串是否包含字符串“thumb_”:

image.matches('thumb_')

推荐阅读