android - 上传图像,Firebase 显示 "User does not have permission to access this object",即使存储设置是公开的

标签 android firebase kotlin firebase-storage firebase-security

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

Failed to Upload Image User does not have permission to access this object.

我检查了每个具有相同主题的 stack-overflow/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}")
            }
    }

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

Failed to Upload Image User does not have permission to access this object.

即使规则设置为公开,并且我的代码看起来也完全没问题。 昨天正确上传图像,但现在出现此错误。

最佳答案

对存储桶名称进行硬编码并不是一个好主意。使用变量占位符作为存储桶名称,请参阅 in the documentation .

Storage Security Rules must first specify the service (in our case firebase.storage), and the Cloud Storage bucket (via match /b/{bucket}/o) which rules are evaluated against. The default rules require Firebase Authentication, but here are some examples of other common rules with different access control.

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

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

enter image description here

关于android - 上传图像,Firebase 显示 "User does not have permission to access this object",即使存储设置是公开的,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55839832/

相关文章:

firebase - 箭头键不适用于 firebase init 命令

error-handling - 在RxJava/RxKotlin中,返回Completable.error(Exception())与抛出之间有什么区别?

java - Google Drive REST Api 通过 fileId 下载文件

java - 从手机摄像头流式传输 JPEG

android - Android 上的 Jgit 克隆

android - 通过取消选中 Play 商店中的自动更新来避免数据费用的最佳方法

Android:带进度条的 EditText?

android - 有人可以帮助我了解 firebase on success listener 的逻辑吗

javascript - 在 Bootstrap 模式中输入 Firebase 数据

java - 在 Kotlin 中将嵌套 Map 复制到 MutableMap