skip to Main Content

I would like to list all the files and folders in a specific directory inside firebase cloud storage. I have tried using files = storage.list_files() from other stack overflow answers. This lists all the files and folders. I have tried using files = storage.list_files(prefix='images/') but it does not work.

I am using pyrebase4 to initialize the app and I have even provided serviceAccount but it did not work.

bucket = storage.bucket()
bucket.list_blobs(prefix="files/")

Please respond with the libraries you are importing, the syntax of initializing firebase app and finally the code to fetch list of all files from a specific directory in firebase cloud storage.

If the above is not possible, would it be very resource demanding (time & cost) to do a storage.list_files().includes(file_to_be_searched) every time? for let’s say, 10k total files.

2

Answers


  1. Chosen as BEST ANSWER

    For posterity, the list_files() will always list all the files because it is deprecated. https://github.com/thisbejim/Pyrebase/issues/414#issuecomment-1100009426

    This code should work to list all the files and folders in a specific directory (copying from the link mentioned above).

    $ pip install pyrebase
    $ pip install pyrebase4
    
    import pyrebase
    
    firebase = pyrebase.initialize_app(config)
    storage = firebase.storage()
    
    path = "images/"
    files = storage.bucket.list_blobs(prefix=path)
    
    # print file names
    for file in files:
        print(file.name)
    

  2. The example provided is using the firebase admin sdk for python.

    Given that you said you are using pyrebase you’d have to use pyrebase

    import pyrebase
    
    # configure firebase app
    config = {
      "apiKey": "apiKey",
      "authDomain": "projectId.firebaseapp.com",
      "databaseURL": "https://projectId.firebaseio.com",
      "storageBucket": "projectId.appspot.com",
    # DO NOT FORGET SERVICE ACCOUNT
    "serviceAccount":"path to service account"
    }
    
    firebase = pyrebase.initialize_app(config)
    storage = firebase.storage()
    
    # specify directory path
    directory_path = "path/to/directory"
    
    # list files in directory
    files = storage.list_files(directory_path)
    
    # print file names
    for file in files:
        print(file.name)
    

    would it be very resource demanding (time & cost)

    Yes it will to both.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search