【问题标题】:firebase storage can not upload any filefirebase 存储无法上传任何文件
【发布时间】:2018-10-23 08:06:14
【问题描述】:

当我尝试将任何图像文件上传到 Firebase 存储时,我收到一条错误消息。

Object { code_: "storage/unauthorized", message_: "Firebase Storage: User does not have permission to access 'img/product/1540281542536/58916.jpg'.", serverResponse_: "{\n  \"error\": {\n    \"code\": 403,\n    \"message\": \"Permission denied. Could not perform this operation\"\n  }\n}", name_: "FirebaseError" }

不知道为什么不行

这是我的存储规则

service firebase.storage {
  match /b/{bucket}/o {
    match /{allPaths=**} {
      match /img {
        match /{allImages=**} {
            allow read
        }
        match /product/{productID}/{imageId} {
            allow create, write, delete: if request.auth != null
        }
      }
      match /user/{userId} {
        allow read, write: if request.auth.uid == userId;
      }
    }
  }
}

这是我的上传代码

$formImgUpload.on('change', function (e) {
  let file = this.files[0]
  let rootRef = firebase.storage().ref()
  let fileName = 'img/product/' + id + '/' + file.name
  let fileRef = rootRef.child(fileName)

  fileRef.put(file).then(function (result) {
    console.log('"' + fileName + '" was uploaded successfully.')

    result.ref.getDownloadURL().then(function (url) {
      $img.attr('src', url)
      $fileUrl.text(url)
    })
  }).catch(function (err) {
    console.error(err)
  })
})

谁能帮我写出正确的规则?

【问题讨论】:

    标签: firebase storage


    【解决方案1】:

    我也有这个问题。你是从你的客户端上传的吗?如果是这样,目前您没有为 request.auth 发送任何内容,因此它将始终为空。

    要解决此问题,请发送自定义令牌 --> https://firebase.google.com/docs/auth/admin/create-custom-tokens

    使用 Firebase Admin SDK 创建自定义令牌是一种非常简单的方法,使用您的 UID 从服务器发送令牌,然后在客户端对其进行身份验证。然后,当您将数据发送到 Storage 时,request.auth 随之而来,然后数据库规则将能够处理这种情况。

    希望这会有所帮助。

    一切顺利。

    【讨论】:

      猜你喜欢
      • 2019-11-06
      • 2021-09-19
      • 2021-04-05
      • 1970-01-01
      • 2018-10-13
      • 1970-01-01
      • 2017-02-03
      • 2021-12-31
      • 2019-03-28
      相关资源
      最近更新 更多