将文件上传到 Firebase 云存储时出错:“对于 rules_version = 1,不允许在存储桶中列出对象”

Sam*_*man 8 firebase firebase-security firebase-authentication firebase-storage

我已经在我的应用程序上设置了 Firebase 身份验证和云存储。但是,每当我尝试上传文件时,都会收到一个错误,我找不到任何相关信息。错误是 400: Bad Request 带有以下消息

Listing objects in a bucket is disallowed for rules_version = "1". Please update storage security rules to rules_verison = "2" to use list.

我似乎找不到有关更新安全规则版本的任何信息。请注意,当我查看 firebase 控制台时,实际上上传成功通过,但 HTTP 返回仍然是上述错误。列出对象是什么意思,我该如何更新我的安全规则?

有关更多信息,我的上传代码(在 Kotlin 中)是

 fun uploadImage(uri: Uri, path: String): Task<Uri> {
            val storageRef = FirebaseStorage.getInstance().reference
            val storagePath = storageRef.child(path)
            return storagePath.putFile(uri).continueWithTask{ storageRef.downloadUrl }
 }
Run Code Online (Sandbox Code Playgroud)

我用

public override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        if (requestCode in 0..2 && resultCode == Activity.RESULT_OK) {
            val cropImageUri = CropImage.getActivityResult(data).uri
            val systemTime = System.currentTimeMillis()
            val path = "$userId/$systemTime"
            //IMAGE UPLOAD HERE:
            FirebaseImageResource.uploadImage(cropImageUri, path)
                    .addOnCompleteListener {
                if (it.isSuccessful) {
                    GlideApp.with(this)
                            .load(cropImageUri)
                            .into(imageViewForPosition(requestCode)!!)
                    imageUris[requestCode] = it.result.toString()
                }
            }
        }
    }
Run Code Online (Sandbox Code Playgroud)

我的 firebase 规则是默认的:

service firebase.storage {
  match /b/{bucket}/o {
    match /{allPaths=**} {
      allow read, write: if request.auth != null;
    }
  }
}
Run Code Online (Sandbox Code Playgroud)

我也成功通过 Facebook 登录进行身份验证

override fun onSuccess(loginResult: LoginResult) {
                val credential = FacebookAuthProvider.getCredential(loginResult.accessToken.token)
                auth.signInWithCredential(credential)
            }
Run Code Online (Sandbox Code Playgroud)

(它现在缺少成功侦听器,但我知道它正在工作,因为当我不使用它时,我会收到未经授权的错误,并且文件实际上并未上传)

Mof*_*fee 18

您需要将其添加到 Firebase 存储规则中:

rules_version = '2';
Run Code Online (Sandbox Code Playgroud)

一个例子是这样的:

rules_version = '2';
service firebase.storage {
  match /b/{bucket}/o {
    match /{allPaths=**} {
      allow read, write: if request.auth != null;
    }
  }
}
Run Code Online (Sandbox Code Playgroud)

我怀疑这是一个不太有用的错误消息的情况。


Amo*_*age 2

只需替换您的上传代码即可

fun uploadImage(uri: Uri, path: String): Task<Uri> {
        val storageRef = FirebaseStorage.getInstance().reference
        val storagePath = storageRef.child(path)
        return storagePath.putFile(uri).continueWithTask{ storageRef.downloadUrl }} 
Run Code Online (Sandbox Code Playgroud)

fun uploadImage(uri: Uri, path: String): Task<Uri> {
        val storageRef = FirebaseStorage.getInstance().reference
        val storagePath = storageRef.child(path)
        return storagePath.putFile(uri).continueWithTask{ storagePath.downloadUrl }}
Run Code Online (Sandbox Code Playgroud)