使用Kotlin将图像上传到Firebase存储不起作用

时间:2020-06-08 16:47:51

标签: android firebase kotlin firebase-storage

我目前正在使用Kotlin上传图像。我已按照文档进行操作,目前已以经过身份验证的用户身份登录。

这是我的代码:

storage = Firebase.storage
private fun uploadImageTest() {
    val storageRef = storage.reference
    val imagesRef = storageRef.child("images/${UUID.randomUUID()}")
    val uploadTask = imagesRef.putFile(imageURI)

    uploadTask.addOnFailureListener {

    }.addOnSuccessListener {

    }
}

但是当我运行代码时,我得到的错误是:

D/NetworkSecurityConfig: No Network Security Config specified, using platform default
W/NetworkRequest: error sending network request POST https://firebasestorage.googleapis.com/v0/b/drop-calendar.appspot.com/o
    java.net.SocketException: socket failed: EPERM (Operation not permitted)

我已经尝试将规则更改为无需身份验证,甚至尝试了我的朋友的代码,该代码确实有效,但在我的身上却无效。当我打开提供的链接时,我得到了这个json:

error: { code: 403, message: "permission denied. could not perform this operation" }

1 个答案:

答案 0 :(得分:0)

在firebase控制台->“存储”->“规则”中转到您的应用程序,然后在此处检查您的设置。如果包含以下内容:

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

这意味着您的用户必须有权执行某些操作。您可以向应用程序添加身份验证(选中https://firebase.google.com/docs/auth),也可以只允许对任何请求的存储进行读取访问:

allow read;
allow write: if request.auth.uid == userId;

有关安全规则的更多信息,您可以在这里查看:https://firebase.google.com/docs/storage/security