4

As the title states, I want to return a list of files in my Firebase Storage under path: images/userUID/ where userUID is supplied by the function. I am doing this so I can figure out which is the highest numbered photo so the next photo upload can take value highestPreviousNumber + 1.

According to the top answer here from 1 year 2 months ago, "Currently there is no API in Firebase Storage to list all files in a folder." But I don't believe that as the top answer to this question states that "the Firebase SDK for Cloud Storage now supports listing all objects from a bucket" and seems to give instructions. (I also don't see why I wouldn't be able to do it given that this official documentation seems to almost have an answer.)

But, the instructions do not work for me: I think I misunderstand what to fill into .ref(). I'll show you what I've tried.

Specifically, I am attempting to get the name of a file with this path:

image/userUID/profilePic-0 where userUID comes from the authUser's uid.

And so I've tried writing:

getHighestPhotoNumByUser = (userUID) => {
        // function retrieves the highest numbered photo from Storage so newly uploaded photos can have value "highestNum + 1"
        let highestNumber;

        const storageRef = this.storage.ref()
        // current attempt
        const imagesRef = storageRef.child(`images`)

        // plugging in this storageRef var into .listAll() returned a result with 0 items
        // const storageRef = this.storage.ref(`images/${userUID}`)

        // plugging the listRef var into .listAll() also returned 0 items
        // const listRef = storageRef.child(`images/${userUID}`).child()

            imagesRef.listAll().then(function (result) {
                console.log("result:", result)
                if (result.items.length == 0) { // base case where user has yet to upload a photo
                    return null
                }
                result.prefixes.forEach(function (something) {
                    // trying to sort thru the names of files stored by this user and get the highest file #
                    return highestNumber
                })
            }).catch(err => {
                console.log(err) 
                // will do something with the error later!
            })
    }

So, in summary, I believe the following code works since I found it on S.O.:

 var storageRef = firebase.storage().ref("your_folder");

    storageRef.listAll().then(function(result) {
        // do stuff
    }

But my three attempts to plug something useful into .listAll() have failed. Again those attempts are:

(1) const imagesRef = storageRef.child(`images`)
(2) const storageRef = this.storage.ref(`images/${userUID}`)
(3) const listRef = storageRef.child(`images/${userUID}`).child()

All returned a result with 0 items. Also in case anyone is wondering, I do indeed have a file in my Firebase Storage under images/userUID, so there is something to retrieve.

If it is impossible to retrieve items specifically from a subfolder, someone please tell me. Perhaps I understand what "listing all objects from a bucket" means? Isn't a bucket just another word for a folder?

Roly Poly
  • 489
  • 1
  • 9
  • 19
  • Your use of "new Promise" wrapping an API that already returns a promise is confusing to me. There is no need to introduce a new Promise object with the API already returns one. I will also point out that you're doing nothing to handle errors here by using catch to detect a rejected promise that should have an error message in it. – Doug Stevenson May 27 '20 at 01:10
  • Ok, I've edited out the promise. I thought it was necessary but I was wrong. – Roly Poly May 27 '20 at 01:23
  • Now, the function is returning nothing at all. The returns on the inner callabacks are not being propagated to the larger function. And there's still no error checking. – Doug Stevenson May 27 '20 at 01:29
  • my main concern in this post is getting ```something.listAll().then(function (result) {})``` to give me the contents of my ```images/userUID/``` folder. Presently it returns empty results when I am expecting something like ```"profilePic-0.jpg"``` – Roly Poly May 27 '20 at 01:37
  • I suggest removing all variables and hard coding a path, in order to remove all uncertainty. – Doug Stevenson May 27 '20 at 01:40
  • 1
    I am having the same issue was this ever resolved? Thanks! – Matt Laszcz Sep 29 '21 at 13:32

0 Answers0