user523234 user523234 - 4 months ago 24
iOS Question

Get Firebase database file donwloadURL manually - iOS platform

I know if I upload an image file to Firebase Database via

putData
method. In return, I can get the
downloadURL
from
metadata
.

But what if I do not want to store this
downloadURL
. And later, I want to download this image.

Question: Can I get this downloadURL if I know the image's file name?

Answer

If you know the file name (or really: the file path) of the image in Firebase Storage, you can get the download URL using:

// Create a reference to the file you want to download
let starsRef = storageRef.child("images/stars.jpg")
// Fetch the download URL
starsRef.downloadURLWithCompletion { (URL, error) -> Void in
  if (error != nil) {
    // Handle any errors
  } else {
    // Get the download URL for 'images/stars.jpg'
  }
}

See the Firebase Storage documentation on downloading files from where I got the above example.