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



我目前正在使用 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" }

在 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

最新更新