【问题标题】:Google Storage - only auth users and no delete / rewrite谷歌存储 - 只有认证用户,没有删除/重写
【发布时间】:2020-09-08 00:52:50
【问题描述】:

我有一个社交应用程序,用户可以将图像发送到谷歌云存储。

所以我需要只有授权用户可以发送图像,并且一个用户不能删除另一个用户图像或覆盖另一个用户图像。

我有这个规则:

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 (if it exists)
     // 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/.*')
                    && (resource == null || request.resource.contentType == resource.contentType)
                    && imageId.size() < 32
     }
   }
 }
}

那么我该如何添加只有授权用户可以发送图像并且他们在发送后不能删除/覆盖图像?

谢谢!

【问题讨论】:

  • 用户是否有谷歌账号?
  • @guillaumeblaquiere 我使用谷歌身份验证,但仅在我的网站中使用电子邮件/密码帐户。
  • 您可以让每个用户在文件的元数据中写入他们的 uid,然后在安全规则中使用该元数据来确定用户是否应该能够删除他们自己的内容。
  • @DougStevenson 哦,谢谢你的回答!我在文档中没有找到关于删除的任何内容,你知道它是否默认拒绝?例如,在我的实际情况下,如果我不更改任何内容,每个用户都将图像发送到 /images/ 任何用户都可以删除/覆盖此文件夹中的图像吗?还是默认拒绝所有人?再次感谢!
  • 在规则允许之前,一切都会被默认拒绝。

标签: firebase google-cloud-storage firebase-storage firebase-security


【解决方案1】:

使用 Google Cloud Storage,您可以在文件上定义 ACL。但是这个 ACL 需要一个 Google 帐户,这不是你的情况。

因此,您必须手动实现过滤器和查看/删除/写入文件的过程。您有几种解决方案

  • 您可以为每个用户创建一个目录(/username-at-provider-com -> 删除电子邮件和 url 格式,您会遇到问题)。
  • 仅使用一个存储桶和目录,并在您的文件中添加custom metadata 以及电子邮件值。

在这两种情况下,请根据用户想要执行的操作检查您的用户电子邮件。只有执行这些检查的后端服务帐户才能访问 GCS。所有的用户请求都必须被这个后端过滤。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-09-11
    • 1970-01-01
    • 1970-01-01
    • 2018-08-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多