【问题标题】:Firebase Storage putBytes throwing StorageException: Cannot upload to getRootFirebase Storage putBytes 抛出 StorageException:无法上传到 getRoot
【发布时间】: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


    【解决方案1】:

    您正在对根引用调用putBytes() 方法。这是不允许的。使用作为 val reference = storageReference.child("users/${auth.currentUser!!.uid}/profile_picture.jpg") 的根引用创建一个子引用,并在其上调用putBytes() 方法(reference.putBytes(profilePicture))。

    修改后的代码将如下所示。

    private val storageReference = Firebase.storage.reference
    
    private suspend fun uploadProfilePicture(profilePicture: ByteArray) {
        val completableDeferred = CompletableDeferred<Task<UploadTask.TaskSnapshot>>()
        val reference = storageReference.child("users/${auth.currentUser!!.uid}/profile_picture.jpg")
        reference.putBytes(profilePicture)
            .addOnCompleteListener { task -> completableDeferred.complete(task) }
        val uploadResult = completableDeferred.await()
        if (!uploadResult.isSuccessful) 
            throw UploadException(uploadResult.exception?.message ?: "Profile picture was not uploaded")
    }
    

    【讨论】:

      猜你喜欢
      • 2019-10-05
      • 1970-01-01
      • 2018-12-26
      • 2018-05-30
      • 2021-10-14
      • 1970-01-01
      • 2016-09-29
      • 2018-05-11
      • 1970-01-01
      相关资源
      最近更新 更多