📖 Scenario: You are building a simple web app that stores images in Firebase Storage. You want to get the download URLs for these images so users can view them.
🎯 Goal: Learn how to create a list of image file names, configure Firebase Storage, generate download URLs for each image, and complete the setup to access these URLs.
📋 What You'll Learn
Create a list of image file names
Initialize Firebase Storage reference
Generate download URLs for each image file
Store the URLs in a list for later use
💡 Why This Matters
🌍 Real World
Getting download URLs is essential to display or share files stored in Firebase Storage in web or mobile apps.
💼 Career
Understanding how to generate and manage download URLs is a common task for cloud developers working with Firebase or similar cloud storage services.
Progress0 / 4 steps
1
Create a list of image file names
Create a list called imageFiles with these exact strings: "photo1.jpg", "photo2.jpg", "photo3.jpg".
Firebase
Hint
Use square brackets to create a list (array) and include the file names as strings.
2
Initialize Firebase Storage reference
Create a constant called storage and set it to getStorage() from Firebase. Then create a constant called storageRef and set it to ref(storage).
Firebase
Hint
Use getStorage() to get the storage service and ref(storage) to get the root reference.
3
Generate download URLs for each image file
Create an empty array called downloadURLs. Use a for loop with variable fileName to iterate over imageFiles. Inside the loop, create a reference called fileRef using ref(storageRef, fileName). Then use getDownloadURL(fileRef) and push the resulting promise into downloadURLs.
Firebase
Hint
Use a for...of loop to go through each file name and create a reference for it.
4
Complete setup to access download URLs
Use Promise.all(downloadURLs) and assign it to a constant called allURLs. This will wait for all download URL promises to resolve.
Firebase
Hint
Use Promise.all() to handle multiple promises and get all URLs together.
Practice
(1/5)
1. What does the getDownloadURL() method in Firebase Storage do?
easy
A. It deletes a file from Firebase Storage.
B. It returns a public URL to access the stored file.
C. It uploads a file to Firebase Storage.
D. It lists all files in a storage bucket.
Solution
Step 1: Understand the purpose of getDownloadURL()
This method is used to get a URL that anyone can use to download or view the file stored in Firebase Storage.
Step 2: Compare with other storage actions
Uploading, deleting, or listing files are different methods. getDownloadURL() specifically returns the file's public URL.
Final Answer:
It returns a public URL to access the stored file. -> Option B
Quick Check:
getDownloadURL() = public file URL [OK]
Hint: Remember: getDownloadURL() gives you the file's public link [OK]
Common Mistakes:
Confusing getDownloadURL() with upload or delete methods
Thinking it lists files instead of returning a URL
Assuming it modifies the file instead of providing access
2. Which of the following is the correct syntax to get a download URL for a file reference fileRef in Firebase Storage?
easy
A. const url = fileRef.getDownloadURL();
B. const url = getDownloadURL(fileRef);
C. const url = await getDownloadURL(fileRef);
D. const url = fileRef.downloadURL();
Solution
Step 1: Recognize that getDownloadURL returns a Promise
Since getDownloadURL() is asynchronous, you must use await or .then() to get the URL.
Step 2: Check correct usage with file reference
The correct usage is calling getDownloadURL(fileRef) as a function with the file reference passed in, and awaiting the result.
Final Answer:
const url = await getDownloadURL(fileRef); -> Option C
Quick Check:
Use await with getDownloadURL(fileRef) [OK]
Hint: Use await with getDownloadURL(fileRef) to get URL [OK]
Common Mistakes:
Calling getDownloadURL as a method on fileRef without await
Forgetting to await the Promise
Using incorrect method names like downloadURL()
3. What will the following code output if the file exists and permissions allow access?
A. getDownloadURL returns a Promise, so url is not the actual URL string.
B. fileRef is not defined.
C. getDownloadURL is not imported.
D. console.log cannot print URLs.
Solution
Step 1: Identify getDownloadURL return type
getDownloadURL() returns a Promise, not the URL string directly.
Step 2: Understand why console.log prints a Promise
Without awaiting or using .then(), url holds a Promise object, so console.log prints the Promise, not the URL.
Final Answer:
getDownloadURL returns a Promise, so url is not the actual URL string. -> Option A
Quick Check:
Missing await means url is Promise, not string [OK]
Hint: Always await getDownloadURL to get the URL string [OK]
Common Mistakes:
Forgetting that getDownloadURL returns a Promise
Assuming console.log prints resolved Promise value
Ignoring async/await syntax requirements
5. You want to display an image from Firebase Storage in your web app using its download URL. Which approach correctly handles errors if the file is missing or access is denied?