How to get URL from Firebase Storage getDownloadURL

后端 未结 10 1984
小蘑菇
小蘑菇 2020-11-21 04:54

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 fireb         


        
10条回答
  •  有刺的猬
    2020-11-21 05:34

    Please refer to the documentation for getting a download URL.

    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()
        {
            @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).

    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();
    }
    

提交回复
热议问题