Search code examples
pythonazure-data-lake

Generating SAS URL for Azure Data Lake Storage


I would like to reach my files which located under Data lake storage folder filesystem from my computer using python. I have generated sas token and created sas url with this piece of code

sas_file = generate_file_sas(account_name=account_name,
                  file_system_name = file_system_name ,
                  directory_name = directory_name ,
                  file_name = file_name,
                  credential=account_key,
                  expiry=datetime.utcnow() + timedelta(hours=1)
                  )
sas_url = 'https://' + account_name+'.blob.core.windows.net/' + container_name + '/' + directory_name + '/' + file_name + '?'+  sas_file

but i can't download any file from this script. Where am I wrong?


Solution

  • I have one file named logo.jpg in directory of my Data lake storage folder filesystem like this:

    enter image description here

    I used below python code to generate SAS URL and download file to local folder like this:

    from azure.storage.filedatalake import generate_file_system_sas,FileSystemSasPermissions
    from datetime import datetime, timedelta
    import requests
    
    account_name="deviadlsgen2acc"
    file_system_name="sri"
    directory_name="dir1"
    account_key="xxxxxxxxxxxxxxxxx"
    file_name="logo.jpg"
    file_path="C:\\Users\\xxxxx\\Documents\\folder1\\test.jpg"
    
    sas_token = generate_file_system_sas(
        account_name=account_name,
        file_system_name=file_system_name,
        credential=account_key,
        permission=FileSystemSasPermissions(read=True),
        expiry=datetime.utcnow() + timedelta(hours=1)
    )
    sas_url = 'https://' + account_name+'.dfs.core.windows.net/' + file_system_name + '/' + directory_name + '/' + file_name + '?'+  sas_token
    print(sas_url)   
    
    response = requests.get(sas_url)
    with open(file_path, "wb") as f:
         f.write(response.content)
         print("File downloaded successfully!!!!)
    

    Response:

    enter image description here

    When I checked that file path, file downloaded successfully to local folder like below:

    enter image description here

    Reference: azure.storage.filedatalake package | Microsoft