Search code examples
djangoazure-storageazure-blob-storagedjango-storage

Is it possible to list files in a dir with Azure storages


I'm using Django and django-storages[azure] as backend. But i can't to find out to list dir the files instead I have to use snippets like this:

block_blob_service.list_blobs(container_name='media', prefix=folderPath)]

this is not working:

listdir(absoluteFolderPath)

In storages/backend/azure_storage.py I found this part:

def listdir(self, path=''):
        """
        Return directories and files for a given path.
        Leave the path empty to list the root.
        Order of dirs and files is undefined.
        """
        files = []
        dirs = set()
        for name in self.list_all(path):
            n = name[len(path):]
            if '/' in n:
                dirs.add(n.split('/', 1)[0])
            else:
                files.append(n)
        return list(dirs), files

But how can I use it?

regards Christopher.


Solution

  • Assuming you have set DEFAULT_FILE_STORAGE you could access it through storage

    from django.core.files.storage import default_storage
    dirs = default_storage.listdir(absoluteFolderPath)