上传图片和Firebase说“用户无权访问此对象”,即使存储设置是公开的

问题描述 投票:1回答:1

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

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

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

我的规则是公开的

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-rules
1个回答
1
投票

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

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

将存储规则更改为以下规则以实现您的目标。

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

enter image description here

© www.soinside.com 2019 - 2024. All rights reserved.