【问题标题】:Uploading an Image and Firebase says "User does not have permission to access this object", even when storage settings are public上传图片和 Firebase 显示“用户无权访问此对象”,即使存储设置是公开的
【发布时间】:2019-09-14 07:52:31
【问题描述】:

我正在尝试上传图像,然后将所述图像的下载位置存储在 val.昨天它可以 100% 完美运行,没有任何问题。我没有更改任何代码,我现在无法上传图片,现在它说

上传图片失败 用户无权访问该对象。

我检查了每个堆栈溢出/Reddit 帖子的相同主题,他们都说要将我的规则的权限更改为公开。

我的规则设置为公开

service firebase.storage {
  match /b/savephoto-a1cc3.appspot.com/o {
    match /{allPaths=**} {
      allow read, write;
    }
  }
}

这是上传图片的实现。

      val ref = FirebaseStorage.getInstance().getReference("/images/profilePictures/$filename")

        //Stores File
        ref.putFile(selectedPhoto!!)
            .addOnSuccessListener {
                Log.d("main", "Succesfully Uploaded Image ${it.metadata?.path}")

                //Gets Download Link for Image
                ref.downloadUrl.addOnSuccessListener {
                    val downloadImg = it.toString()
                    Log.d("main", "Profile Picture Location $it")
                    //Create User
                    createUser(downloadImg, age, gender, firstName, lastName)
                }
            }
            .addOnFailureListener {
                Log.d("main", "Failed to Upload Image ${it.message}")
            }
    }

仍然不确定我为什么会得到一个

上传图片失败 用户无权访问该对象。

即使规则设置为公开并且我的代码看起来非常好。 昨天可以正常上传图片,但现在我收到了这个错误。

【问题讨论】:

标签: android firebase kotlin firebase-storage firebase-security


【解决方案1】:

硬编码存储桶名称不是一个好主意。使用变量占位符代替存储桶名称,参见in the documentation

存储安全规则必须首先指定服务(在我们的例子中为 firebase.storage)和 Cloud Storage 存储桶(通过匹配 /b/{bucket}/o) 评估规则。默认规则需要 Firebase 身份验证,但以下是具有不同访问控制的其他常见规则的一些示例。

将您的存储规则更改为以下规则以实现您想要的。

service firebase.storage {
  match /b/{bucket}/o {
    match /{allPaths=**} {
      allow read, write;
    }
  }
}

【讨论】:

    猜你喜欢
    • 2016-12-04
    • 2020-01-30
    • 2020-08-09
    • 2018-05-30
    • 1970-01-01
    • 1970-01-01
    • 2018-06-05
    • 2020-10-05
    • 2021-08-18
    相关资源
    最近更新 更多