I got a SAS token which was created for a specific folder on my Azure Datalake Gen2. The goal is, to download the folder with all its contents.
I understand, that I can create a DataLakeServiceClient, a FileSystemClient, or a DataLakeDirectoryClient as follows:
# configuration
url = 'https://my-account.blob.core.windows.net'
sas_token = '<sas-token>'
file_system_name = 'file_system_1'
subfolder_path = 'subfolder_1'
# service client
data_lake_service_client = DataLakeServiceClient(account_url=url, credential=sas_token)
# directory client and file system client
file_system_client = data_lake_service_client.get_file_system_client(file_system=file_system_name)
data_lake_directory_client = data_lake_service_client.get_directory_client(file_system=file_system_name, directory=subfolder_path)
Now to download specific files, I need to know what files exist:
-
Unfortunately, the
DataLakeDirectoryClient
does not have a function to get all paths of the files inside that directory. -
On the other hand, the
FileSystemClient
has that function but is searching on the file system level, where my SAS token does not have access.
How do I list and download all files in my directory?
2
Answers
You can list files in a specific directory using the list_paths() method of FileSystemClient and then filter them by the directory’s prefix.
Here’s a sample code for listing and downloading all files in a directory:
I have reproduced in my environment and got expected results as below:
In Portal created SAS for subfolder2 and below are files in that folder:
Code
which worked for me :Output:
Downloaded Locally: