问题描述
我正在尝试为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 likecom.google.android.gms.tasks.zzh@xxx
问题1。我可以从此获取类似的下载链接:
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错误,并且应用程序崩溃了。 consol告诉我这是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。如何解决此问题?
推荐答案
当您调用 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
}
});
}
这将返回一个公开的无法下载的网址。如果你刚刚上传了一个文件,那么这个公共网址将在上传的成功回调中(你上传后不需要调用另一个异步方法)。
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获取网址的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!