如何从使用Firebase上传的文件中获取下载URL

huangapple go评论86阅读模式
英文:

How to get download url from uploaded files using Firebase

问题

阅读文档后,我发现有一个部分介绍了如何上传文件并获取其下载链接。
获取下载链接的代码如下:

Kotlin

val ref = storageRef.child("images/mountains.jpg")
uploadTask = ref.putFile(file)

val urlTask = uploadTask.continueWithTask { task ->
    if (!task.isSuccessful) {
        task.exception?.let {
            throw it
        }
    }
    ref.downloadUrl
}.addOnCompleteListener { task ->
    if (task.isSuccessful) {
        val downloadUri = task.result
    } else {
        // 处理失败情况
        // ...
    }
}

Java

final StorageReference ref = storageRef.child("images/mountains.jpg");
uploadTask = ref.putFile(file);

Task<Uri> urlTask = uploadTask.continueWithTask(new Continuation<UploadTask.TaskSnapshot, Task<Uri>>() {
    @Override
    public Task<Uri> then(@NonNull Task<UploadTask.TaskSnapshot> task) throws Exception {
        if (!task.isSuccessful()) {
            throw task.getException();
        }

        // 继续任务以获取下载链接
        return ref.getDownloadUrl();
    }
}).addOnCompleteListener(new OnCompleteListener<Uri>() {
    @Override
    public void onComplete(@NonNull Task<Uri> task) {
        if (task.isSuccessful()) {
            Uri downloadUri = task.getResult();
        } else {
            // 处理失败情况
            // ...
        }
    }
});

但是,哪一行代码用于获取下载链接?
是 "return ref.getDownloadUrl();" / "ref.downloadUrl" 吗?
还是 "Uri downloadUri = task.getResult();" / "val downloadUri = task.result;"?

英文:

Reading the documentation, I found a section talking about how to upload files and get its download link.
The code to get download link is:

Kotlin

val ref = storageRef.child(&quot;images/mountains.jpg&quot;)
uploadTask = ref.putFile(file)

val urlTask = uploadTask.continueWithTask { task -&gt;
    if (!task.isSuccessful) {
        task.exception?.let {
            throw it
        }
    }
    ref.downloadUrl
}.addOnCompleteListener { task -&gt;
    if (task.isSuccessful) {
        val downloadUri = task.result
    } else {
        // Handle failures
        // ...
    }
}

Java

final StorageReference ref = storageRef.child(&quot;images/mountains.jpg&quot;);
uploadTask = ref.putFile(file);

Task&lt;Uri&gt; urlTask = uploadTask.continueWithTask(new Continuation&lt;UploadTask.TaskSnapshot, Task&lt;Uri&gt;&gt;() {
    @Override
    public Task&lt;Uri&gt; then(@NonNull Task&lt;UploadTask.TaskSnapshot&gt; task) throws Exception {
        if (!task.isSuccessful()) {
            throw task.getException();
        }

        // Continue with the task to get the download URL
        return ref.getDownloadUrl();
    }
}).addOnCompleteListener(new OnCompleteListener&lt;Uri&gt;() {
    @Override
    public void onComplete(@NonNull Task&lt;Uri&gt; task) {
        if (task.isSuccessful()) {
            Uri downloadUri = task.getResult();
        } else {
            // Handle failures
            // ...
        }
    }
});

But what is the line I get the download URL?
Is it the "return ref.getDownloadUrl();"/"ref.downloadUrl"?
Or the "Uri downloadUri = task.getResult();"/"val downloadUri = task.result"?

答案1

得分: 1

在这两种情况下,名为 downloadUri 的变量是最终的下载URL。如果你愿意,你可以使用 downloadUri.toString() 将其转换为普通的字符串。

不是 ref.getDownloadUrl() 的返回值。这是一个非常常见的错误。getDownloadUrl() 是异步的,不会立即返回URL。这就是为什么你需要回调函数。

另请参阅:https://stackoverflow.com/questions/37374868/how-to-get-url-from-firebase-storage-getdownloadurl

英文:

In both cases, the variable called downloadUri is the final download URL. You can convert it to a plain old string, if you want, with downloadUri.toString().

It is not the return value of ref.getDownloadUrl(). That is a very common mistake. getDownloadUrl() is asynchronous and doesn't return the URL immediately. That's why you need the callback.

See also: https://stackoverflow.com/questions/37374868/how-to-get-url-from-firebase-storage-getdownloadurl

huangapple
  • 本文由 发表于 2020年3月15日 10:26:32
  • 转载请务必保留本文链接:https://go.coder-hub.com/60689182.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定