首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >如何使用允许列表保护在Firebase存储上上载的文件?

如何使用允许列表保护在Firebase存储上上载的文件?
EN

Stack Overflow用户
提问于 2022-03-30 12:44:25
回答 1查看 301关注 0票数 1

关于如何通过其安全规则特性保护文件上载,Firebase存储有一个很好的示例:

代码语言:javascript
运行
复制
rules_version = '2';
service firebase.storage {
  match /b/{bucket}/o {
    match /images {
      // Cascade read to any image type at any path
      match /{allImages=**} {
        allow read;
      }

      // Allow write files to the path "images/*", subject to the constraints:
      // 1) File is less than 5MB
      // 2) Content type is an image
      // 3) Uploaded content type matches existing content type
      // 4) File name (stored in imageId wildcard variable) is less than 32 characters
      match /{imageId} {
        allow write: if request.resource.size < 5 * 1024 * 1024
                    && request.resource.contentType.matches('image/.*')
                    && request.resource.contentType == resource.contentType
                    && imageId.size() < 32
      }
    }
  }
}

但是,如果您希望允许更多的文件(而不仅仅是图像),怎么办?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2022-03-30 12:44:25

matches方法在request.resource.contentType.matches('image/.*')中按照[消]火基文件接受正则表达式,因此您可以这样做:

代码语言:javascript
运行
复制
request.resource.contentType.matches('image/jpeg|image/png|image/gif')

如果您认为您将有一个很长的mime类型列表,您可能应该开始在Firebase规则中使用自定义函数。下面是上面的相同示例,包含自定义函数和更多的mime类型:

代码语言:javascript
运行
复制
rules_version = '2';
service firebase.storage {
  match /b/{bucket}/o {
    match /images {
      // Cascade read to any image type at any path
      match /{allImages=**} {
        allow read;
      }

      // Allow write files to the path "images/*", subject to the constraints:
      // 1) File is less than 5MB
      // 2) Content type is an image
      // 3) Uploaded content type matches existing content type
      // 4) File name (stored in imageId wildcard variable) is less than 32 characters
      match /{imageId} {
        // True if the uploaded file is part of the allow-list
        function isAllowedFile(request) {
          // Mime-types list source: https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Content-Disposition
          let allowList = "audio/aac|application/x-abiword|application/x-freearc|image/avif|video/x-msvideo|application/vnd.amazon.ebook|application/octet-stream|image/bmp|application/x-bzip|application/x-bzip2|application/x-cdf|application/x-csh|text/css|text/csv|application/msword|application/vnd.openxmlformats-officedocument.wordprocessingml.document|application/vnd.ms-fontobject|application/epub+zip|application/gzip|image/gif|image/vnd.microsoft.icon|text/calendar|image/jpeg|application/json|application/ld+json|audio/midi|audio/x-midi|audio/mpeg|video/mp4|video/mpeg|application/vnd.apple.installer+xml|application/vnd.oasis.opendocument.presentation|application/vnd.oasis.opendocument.spreadsheet|application/vnd.oasis.opendocument.text|audio/ogg|video/ogg|application/ogg|audio/opus|font/otf|image/png|application/pdf|application/x-httpd-php|application/vnd.ms-powerpoint|application/vnd.openxmlformats-officedocument.presentationml.presentation|application/vnd.rar|application/rtf|application/x-sh|image/svg+xml|application/x-tar|image/tiff|video/mp2t|font/ttf|text/plain|application/vnd.visio|audio/wav|audio/webm|video/webm|image/webp|font/woff|font/woff2|application/vnd.ms-excel|application/vnd.openxmlformats-officedocument.spreadsheetml.sheet|application/xml|application/vnd.mozilla.xul+xml|application/zip|video/3gpp|audio/3gpp|video/3gpp2|audio/3gpp2|application/x-7z-compressed";
          return request.resource.contentType.matches(allowList);
        }

        allow write: if request.resource.size < 5 * 1024 * 1024
                    && isAllowedFile(request)
                    && request.resource.contentType == resource.contentType
                    && imageId.size() < 32
      }
    }
  }
}
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/71677481

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档