我已经在应用程序上设置了Firebase身份验证和云存储。但是,每当我尝试上传文件时,都会出现错误,找不到任何相关信息。错误为400:错误请求,并显示以下消息
Listing objects in a bucket is disallowed for rules_version = "1". Please update storage security rules to rules_verison = "2" to use list.
我似乎找不到任何有关更新安全性rules_version的信息。请注意,当我查看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 }
}
我称之为
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()
}
}
}
}
我的firebase规则是默认规则:
service firebase.storage {
match /b/{bucket}/o {
match /{allPaths=**} {
allow read, write: if request.auth != null;
}
}
}
我还成功通过Facebook登录名进行了身份验证
override fun onSuccess(loginResult: LoginResult) {
val credential = FacebookAuthProvider.getCredential(loginResult.accessToken.token)
auth.signInWithCredential(credential)
}
(它现在缺少成功的侦听器,但是我知道它是有效的,因为当我不使用它时,会收到未授权的错误,并且该文件实际上并未上传)
答案 0 :(得分: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 }}
到
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 }}
答案 1 :(得分:0)
您需要在Firebase存储规则之前添加它:
rules_version = '2';
一个例子是这样的:
rules_version = '2';
service firebase.storage {
match /b/{bucket}/o {
match /{allPaths=**} {
allow read, write: if request.auth != null;
}
}
}
我怀疑这是一条不太有用的错误消息。
答案 2 :(得分:0)
我通过以下步骤解决了