Swift Mercury
Swift Mercury

Reputation: 89

List only blobs/files in a container and not directories, sub-directories, sub-files

Let's say that I have the following file structure in my azure storage account.

container-1
    directory-1
        file-1-1.csv
        file-1-2.json
        file-1-3.jpeg
    directory-2
        file-2-1.csv
    file-1.csv
    file-2.csv
container-2
    directory-1
        directory-1-1
            file1-1-1.csv

I want to list only files (file-1.csv, file-2.csv) inside container-1 and not any other results is there a way to make such a request.

Currently, the URL which I am using is this:

https://${account}.blob.core.windows.net/container-1?comp=list&restype=container&prefix=/

which returns:

directory-1
directory-1/file-1-1.csv
directory-1/file-1-2.json
directory-1/file-1-3.jpeg
directory-2
directory-2/file-2-1.csv
file-1.csv
file-2.csv

while I only need:

file-1.csv
file-2.csv

Upvotes: 3

Views: 3311

Answers (3)

Eli m
Eli m

Reputation: 83

You don't have a straight forward way to do so. I use python as follows:

    blob_list = container_client.list_blobs()
    folder_path = "folder/path/"

    for blob in blob_list:
        is_folder = True if blob.content_settings["content_md5"] is None else False
        if not is_folder and blob.name.count('/') == folder_path.count('/'):
            print(blob.name)

Upvotes: 0

NotFound
NotFound

Reputation: 6192

You can make a List Blobs request to the Azure Blob REST API. In that request you can specify a delimiter in the querystring. Use the delimiter /.

That'll cause the result to list all your blobs that are in the root of your request to use the XML <Blob> element. All blobs that are not in the root of your search will appear grouped under a <BlobPrefix> element with the foldername. Discard all <BlobPrefix> elements and you're left with only the blobs that are not contained in (another) 'folder'.

Upvotes: 0

Gaurav Mantri
Gaurav Mantri

Reputation: 136346

It is not possible to list just blobs using Blob Storage REST API. There is very limited server-side filtering support available there and listing just blobs is not one of them.

One "hacky" way would be to filter blobs by prefix. Assuming all files at the container level start with some pre-defined characters (file for example) and no virtual directories at that level start with those characters, then you can specify file as prefix and then REST API will only return the blobs names of which start with those characters.

Upvotes: 0

Related Questions