How to Get the Download Url from Firebase Storage

How to get URL from Firebase Storage getDownloadURL

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<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).

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

How to get download url for firebase storage and update firestore document?

Determining the download URL requires a call to the servers, which means it happens asynchronously. For this reason, any code that needs the download URL needs to be inside the onSuccess of getDownloadURL() or be called from there.

So:

private void getItemImageUrl(StorageReference reference) {
reference.getDownloadUrl()
.addOnSuccessListener(new OnSuccessListener<Uri>() {
@Override
public void onSuccess(Uri uri) {
itemImageUrl = uri.toString();

... here you can write itemImageUrl to the database
}
});

}

Also see:

  • How to get the download url from Firebase Storage?

Cant get the download url from firebase storage

You don't need to use an UploadTask to get the download URL. A simpler version of your code is:

Reference ref = FirebaseStorage.instance .ref()
.child('user_image')
.child(authResult.user.uid+'.jpg')
try {
await ref.putFile(image);
String url = await ref.getDownloadURL();
print('Url' + url);
} on FirebaseException catch (e) {
print(e);
}

What I've done above:

  1. Create a variable for the StorageReference, so that you only have to calculate it once and then both upload and get the download URL from that variable.
  2. Move the exception handling to also cover the getDownloadURL() call, as you probably don't want to try and get the download URL when the upload fails.

With these two changes, it's pretty idiomatic and really close to the FlutterFire documentation examples on uploading files and getting download URLs.

How to get download url for a Image from Firebase Storage?

Instead of passing storage to getDownloadURL(), pass snapshot.ref.

const response = await fetch(selectedImage.uri);
const file = await response.blob();

const storageRef = ref(storage, `profile/${currentUser.email}`);
uploadBytes(storageRef, file).then( (snapshot) => {
console.log('uploaded');
getDownloadURL(snapshot.ref).then( url => console.log(url));
});

Getting download URLs from FirebaseStorage

In order to successfully return the downloadURL the client has to ping the storage bucket. Firebase then has to check the security rules to ensure that the operation is allowed, then verify that the object exists, and then return it's download URL to you. Unfortunately there is currently no way of doing this process in bulk.

It's quite common to store the downloadURL in a Firestore document so you can avoid the additional overhead and retrieve the downloadURL directly. But remember, downloadURLs are public and do not adhere to security rules. So make sure the document you're storing them in has proper rules placed on it.

Remember to perform a clean up operation and remove the reference in Firestore if you every delete the file.



Related Topics



Leave a reply



Submit