【发布时间】:2021-01-26 18:13:38
【问题描述】:
Firebase 存储中是否有办法生成指向任何内容的下载 url,以便稍后将文件上传到该 url?类似的东西(在 Kotlin 中):
fun generateItemPhotoUrl(id: String) =
storageRef.child("$Id/${generateUniqueName()}.${COMPRESS_FORMAT.name}").downloadUrl
此代码返回失败的任务...
我想要这个,所以我的上传过程看起来像这样:
// Case: old photo is null but new one is not - upload new photo to a new uri
generateItemPhotoUrl(itemId).continueWithTask { generateTask ->
if (generateTask.isSuccessful) {
val destUrl = generateTask.result.toString()
// Uploading may take time, so first update document to hold a uri, so consecutive
// calls will result in updating instead of uploading a new file
updateItemPhoto(itemId, destUrl).continueWithTask { updateTask ->
if (updateTask.isSuccessful)
uploadFileToDest(destUrl, newImage).continueWithTask { uploadTask ->
if (!uploadTask.isSuccessful) updateItemPhoto(itemId, null)
}
}
}
}
如代码中所述,我需要这样做以防止连续两次更新项目照片的速度太快,以至于第一个无法完成上传。我最终得到了 2 个文件 - 其中一个没有从任何地方引用。如果我能做这样的事情,第二次上传将转到我的“更新”案例(而不是此处显示的“新照片”案例)——文件将被正确切换。
【问题讨论】:
标签: firebase kotlin google-cloud-firestore firebase-storage