Search code examples
javascriptreactjsfirebasefirebase-storage

Get only one item from firebase storage


I am trying to build a website to develop my skills. I try to do something but I couldn't find a solution.

import { getStorage, ref, listAll } from "firebase/storage";

const storage = getStorage();

// Create a reference under which you want to list
const listRef = ref(storage, 'files/uid');

// Find all the prefixes and items.
listAll(listRef)
  .then((res) => {
    res.prefixes.forEach((folderRef) => {
      // All the prefixes under listRef.
      // You may call listAll() recursively on them.
    });
    res.items.forEach((itemRef) => {
      // All the items under listRef.
    });
  }).catch((error) => {
    // Uh-oh, an error occurred!
  });

on the Firebase Storage documentation, I can see how to get all prefixes and files. What I want to do is to get only one item from the folder. Is there a way to do this?


Solution

  • Note: listAll only retrieves the file names, not the data from those files.

    There is no way to get a list of only one filename in a folder through the API. I'd recommend either giving that first file a default name, like default.jpg so that you can use that name in your code, or only retrieving the first file that listAll gives you.