Home > Back-end >  List only blobs/files in a container and not directories, sub-directories, sub-files
List only blobs/files in a container and not directories, sub-directories, sub-files

Time:01-13

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

CodePudding user response:

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.

CodePudding user response:

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'.

  •  Tags:  
  • Related