【发布时间】:2021-06-16 08:14:31
【问题描述】:
我有以下方法尝试将图像作为字节数组上传到 Kotlin 中的 Firebase 存储,但它抛出了 StorageException。
private val storageReference = Firebase.storage.reference
private suspend fun uploadProfilePicture(profilePicture: ByteArray) {
val completableDeferred = CompletableDeferred<Task<UploadTask.TaskSnapshot>>()
storageReference.child("users/${auth.currentUser!!.uid}/profile_picture.jpg")
storageReference.putBytes(profilePicture)
.addOnCompleteListener { task -> completableDeferred.complete(task) }
val uploadResult = completableDeferred.await()
if (!uploadResult.isSuccessful)
throw UploadException(uploadResult.exception?.message ?: "Profile picture was not uploaded")
}
logcat 错误:
An unknown error occurred, please check the HTTP result code and inner exception for server response.
Code: -13000 HttpResult: 0
2021-03-18 21:14:47.586 16532-16532/ E/StorageException: Cannot upload to getRoot. You should upload to a storage location such as .getReference('image.png').putFile...
java.lang.IllegalArgumentException: Cannot upload to getRoot. You should upload to a storage location such as .getReference('image.png').putFile...
at com.google.firebase.storage.UploadTask.run(UploadTask.java:205)
at com.google.firebase.storage.StorageTask.lambda$getRunnable$7(StorageTask.java:1072)
at com.google.firebase.storage.StorageTask$$Lambda$12.run(Unknown Source:2)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1167)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:641)
at java.lang.Thread.run(Thread.java:764)
2021-03-18 21:14:47.587 16532-16532/ E/Register upload: An unknown error occurred, please check the HTTP result code and inner exception for server response.
按照https://firebase.google.com/docs/storage/android/upload-files#kotlin+ktx 这似乎没问题,我不确定问题可能是什么。我存储的安全规则设置为允许任何人,只要他们目前获得授权:
rules_version = '2';
service firebase.storage {
match /b/{bucket}/o {
match /{allPaths=**} {
allow read, write: if request.auth != null;
}
}
}
【问题讨论】:
标签: android firebase kotlin firebase-storage