4

The recently introduced firebase extension 'Image resize' produces a thumbnail once a picture is uploaded to a storage bucket.

How do I obtain the download url of the image of this thumbnail, after the extension completes?

final StorageReference storageRef =
        FirebaseStorage.instance.ref().child(fileName);

    final StorageUploadTask uploadTask = storageRef.putFile(
      File(path),
    );

    final StorageTaskSnapshot downloadUrl = (await uploadTask.onComplete);

    final String url = (await downloadUrl.ref.getDownloadURL()); //This will give me the download url of file before resize

// ??How do I the download url of resized image that gets stored in fileName/thumbnails folder

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807

1 Answers1

0

When you upload an image file to your specified Cloud Storage bucket, this extension:

  1. Creates a resized image with your specified dimensions.
  2. Stores the resized image in the same Storage bucket as the original uploaded image.
  3. Names the resized image using the same name as the original uploaded image, but suffixed with your specified width and height.

For example, if you specify a path here of thumbs and you upload an image to /images/original.jpg, then the resized image is stored at /images/thumbs/original_200x200.jpg

So your file's url will be-

 String name = url.substring(url.lastIndexOf("/")+1,url.indexOf("."));
 String urlStr = "thumbnails/"+name+"_"+width+"x"+height+url.substring(url.indexOf("."),url.length());
 storageRef.child(url.replace(name,urlStr)).getDownloadUrl()
     .addOnSuccessListener(new OnSuccessListener<Uri>() {
        @Override
        public void onSuccess(Uri uri) {
           // Got the download URL for 'users/me/profile.png'
     }})  
Krishna Vyas
  • 1,009
  • 8
  • 25
  • thanks for this. However, I do not see 'addOnSuccessListener' for getDownloadURL. – Vinayakaram Nagarajan Oct 08 '19 at 17:54
  • Sorry this code is in Java, In Flutter Just use: var ref = storageRef.child(url.replace(name,urlStr)); var downloadURL = await ref.getDownloadURL(); – Krishna Vyas Oct 09 '19 at 05:24
  • 4
    Thanks. This does not seem to be the right method to get downloadUrl though. There is a cloud function that gets triggered to create the thumbnail. Client side code does not know when the cloud function completes. So, when `downloadURL = await ref.getDownloadURL()` is called the file would not exist. Need the download URL AFTER the creation of thumbnail. Guess we need another cloud function to achieve this. – Vinayakaram Nagarajan Oct 09 '19 at 10:42
  • that's why await keyword is used. – Krishna Vyas Oct 09 '19 at 11:05
  • await waits for String download url if the storageref is present. I have tried code above. Does not wait for a file to be created and then return its download url – Vinayakaram Nagarajan Oct 09 '19 at 13:16
  • 1
    as Vinayakaram points out, this does not give the url after the EXTENSION has run. I've been looking for an answer too, but the only thing I can come up with is to store the new bucket location (because that you can know) and then on the client use ref.getDownloadURL() as pointed out here https://stackoverflow.com/questions/50877398/flutter-load-image-from-firebase-storage. But that, imo, kinda puts a dent in the point of having a thumbnail in the first place. It does indeed look like another cloud function is necessary (which renders the extension pointless). – Jason Simpson Oct 28 '19 at 05:33
  • 5
    With no way to derive the public urls of the resized images, the extension is useless to anyone using public urls to load images – Keith Coughtrey Oct 30 '19 at 04:25
  • 2
    The function is still useless... April 2020 – luke cross Apr 28 '20 at 14:13
  • the function is still useless... July 2021 – Luka Momcilovic Jul 10 '21 at 01:08
  • o5-2022 still no answer:( – awariat May 06 '22 at 21:40