如何从 Firebase 存储 getDownloadURL 获取 URL

How to get URL from Firebase Storage getDownloadURL(如何从 Firebase 存储 getDownloadURL 获取 URL)

本文介绍了如何从 Firebase 存储 getDownloadURL 获取 URL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试获取指向 Firebase 存储桶中文件的长期持久下载链接".我已将其权限更改为

I'm trying to get the "long term persistent download link" to files in our Firebase storage bucket. I've changed the permissions of this to

service firebase.storage {
  match /b/project-xxx.appspot.com/o {
    match /{allPaths=**} {
      allow read, write;
    }
  }
}

我的 javacode 看起来像这样:

And my javacode looks like this:

private String niceLink (String date){
    String link;
    // Points to the root reference
    StorageReference storageRef = FirebaseStorage.getInstance().getReference();
    StorageReference dateRef = storageRef.child("/" + date+ ".csv");
    link = dateRef.getDownloadUrl().toString();
    return link;
}

当我运行它时,我得到的 uri 链接看起来像com.google.android.gms.tasks.zzh@xxx

When I run this I get the uri link that looks something like com.google.android.gms.tasks.zzh@xxx

问题 1. 我可以从中获得类似于以下内容的下载链接:https://firebasestorage.googleapis.com/v0/b/project-xxxx.appspot.com/o/20-5-2016.csv?alt=media&token=b5d45a7f-3ab7-4f9b-b661-3a2187adxxxx

Question 1. Can I from this get the download link similar to: https://firebasestorage.googleapis.com/v0/b/project-xxxx.appspot.com/o/20-5-2016.csv?alt=media&token=b5d45a7f-3ab7-4f9b-b661-3a2187adxxxx

在尝试获取上面的链接时,我更改了返回前的最后一行,如下所示:

When trying to get the link above I changed the last row before my return, like this:

private String niceLink (String date){
    String link;
    // Points to the root reference
    StorageReference storageRef = FirebaseStorage.getInstance().getReference();
    StorageReference dateRef = storageRef.child("/" + date+ ".csv");
    link = dateRef.getDownloadUrl().getResult().toString();
    return link;
}

但是当我这样做时,我得到一个 403 错误,并且应用程序崩溃了.控制台告诉我这是 bc 用户未登录/auth."请先登录再索取令牌"

But when doing this i get a 403 error, and the app crashing. The consol tells me this is bc user is not logged in /auth. "Please sign in before asking for token"

问题 2.我该如何解决这个问题?

Question 2. How do I fix this?

推荐答案

请参考获取下载 URL 的文档.

当您调用 getDownloadUrl() 时,调用是异步的,您必须订阅成功回调才能获得结果:

When you call getDownloadUrl(), the call is asynchronous and you must subscribe on a success callback to obtain the results:

// Calls the server to securely obtain an unguessable download Url
private void getUrlAsync (String date){
    // Points to the root reference
    StorageReference storageRef = FirebaseStorage.getInstance().getReference();
    StorageReference dateRef = storageRef.child("/" + date+ ".csv");
    dateRef.getDownloadUrl().addOnSuccessListener(new OnSuccessListener<Uri>()
    {
        @Override
        public void onSuccess(Uri downloadUrl) 
        {                
           //do something with downloadurl
        } 
    });
}

这将返回一个公开的不可猜测的下载 url.如果你刚刚上传了一个文件,这个公共的url会在上传成功回调中(上传后不需要调用其他异步方法).

This will return a public unguessable download url. If you just uploaded a file, this public url will be in the success callback of the upload (you do not need to call another async method after you've uploaded).

但是,如果您想要的只是引用的 String 表示,则只需调用 .toString()

However, if all you want is a String representation of the reference, you can just call .toString()

// Returns a Uri of the form gs://bucket/path that can be used
// in future calls to getReferenceFromUrl to perform additional
// actions
private String niceRefLink (String date){
    // Points to the root reference
    StorageReference storageRef = FirebaseStorage.getInstance().getReference();
    StorageReference dateRef = storageRef.child("/" + date+ ".csv");
    return dateRef.toString();
}

这篇关于如何从 Firebase 存储 getDownloadURL 获取 URL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:如何从 Firebase 存储 getDownloadURL 获取 URL

基础教程推荐