Search code examples
javascriptfirebase-storage

Retrieve all media from Firebase storage in 2023


Is there a way to retrieve every img download URL from firebase storage i tried like this:

const mediaRef = ref(storage, 'media'); // Reference to your media folder

    const urls = await Promise.all(mediaRef.items.map(async (item) => {
      const url = await getDownloadURL(item);
      return url;

and like this:

    const mediaRef = ref(storage, 'media'); // Reference to your media folder
    const result = await storage.listAll(mediaRef);

    const urls = await Promise.all(result.items.map(async (item) => {
      const url = await getDownloadURL(item);
      return url;

but noting seems to work. If there is no such a functionality then what is the workaround? I saw a question from 6 years ago that there was no functionality for that then its still true?


Solution

  • You can try it this way, it worked for me:

    const FIREBASE_APP = initializeApp(firebaseConfig);
    const FIREBASE_STORAGE = getStorage(FIREBASE_APP);
    
    const listRef = ref(FIREBASE_STORAGE);
    
    listAll(listRef).then(res =>
      res.items.forEach(imageRef =>
        getDownloadURL(imageRef).then(res2 => console.log(res2)),
      ),
    );