Microsoft Azure File DataLake Storage Client Library for Python
Azure DataLake service client library for Python
This preview package for Python includes ADLS Gen2 specific API support made available in Storage SDK. This includes:
- New directory level operations (Create, Rename, Delete) for hierarchical namespace enabled (HNS) storage account. For HNS enabled accounts, the rename/move operations are atomic.
- Permission related operations (Get/Set ACLs) for hierarchical namespace enabled (HNS) accounts.
- Python 2.7, or 3.5 or later is required to use this package.
- You must have an Azure subscription and an Azure storage account to use this package.
Install the package
Install the Azure DataLake Storage client library for Python with pip:
pip install azure-storage-file-datalake --pre
Create a storage account
# Create a new resource group to hold the storage account - # if using an existing resource group, skip this step az group create --name my-resource-group --location westus2 # Install the extension 'Storage-Preview' az extension add --name storage-preview # Create the storage account az storage account create --name my-storage-account-name --resource-group my-resource-group --sku Standard_LRS --kind StorageV2 --hierarchical-namespace true
Authenticate the client
Interaction with DataLake Storage starts with an instance of the DataLakeServiceClient class. You need an existing storage account, its URL, and a credential to instantiate the client object.
To authenticate the client you have a few options:
- Use a SAS token string
- Use an account shared access key
- Use a token credential from azure.identity
Alternatively, you can authenticate with a storage connection string using the
from_connection_string method. See example: Client creation with a connection string.
You can omit the credential if your account URL already has a SAS token.
Once you have your account URL and credentials ready, you can create the DataLakeServiceClient:
from azure.storage.filedatalake import DataLakeServiceClient service = DataLakeServiceClient(account_url="https://<my-storage-account-name>.dfs.core.windows.net/", credential=credential)
DataLake storage offers four types of resources:
- The storage account
- A file system in the storage account
- A directory under the file system
- A file in a the file system or under directory
The DataLake Storage SDK provides four different clients to interact with the DataLake Service:
- DataLakeServiceClient - this client interacts with the DataLake Service at the account level.
It provides operations to retrieve and configure the account properties
as well as list, create, and delete file systems within the account.
For operations relating to a specific file system, directory or file, clients for those entities
can also be retrieved using the
- FileSystemClient - this client represents interaction with a specific
file system, even if that file system does not exist yet. It provides operations to create, delete, or
configure file systems and includes operations to list paths under file system, upload, and delete file or
directory in the file system.
For operations relating to a specific file, the client can also be retrieved using the
For operations relating to a specific directory, the client can be retrieved using the
- DataLakeDirectoryClient - this client represents interaction with a specific directory, even if that directory does not exist yet. It provides directory operations create, delete, rename, get properties and set properties operations.
- DataLakeFileClient - this client represents interaction with a specific file, even if that file does not exist yet. It provides file operations to append data, flush data, delete, create, and read file.
- DataLakeLeaseClient - this client represents lease interactions with a FileSystemClient, DataLakeDirectoryClient or DataLakeFileClient. It provides operations to acquire, renew, release, change, and break leases on the resources.
The following sections provide several code snippets covering some of the most common Storage DataLake tasks, including:
Client creation with a connection string
Create the DataLakeServiceClient using the connection string to your Azure Storage account.
from azure.storage.filedatalake import DataLakeServiceClient service = DataLakeServiceClient.from_connection_string(conn_str="my_connection_string")
Uploading a file
Upload a file to your file system.
from azure.storage.filedatalake import DataLakeFileClient data = b"abc" file = DataLakeFileClient.from_connection_string("my_connection_string", file_system_name="myfilesystem", file_path="myfile") file.append_data(data, offset=0, length=len(data)) file.flush_data(len(data))
Downloading a file
Download a file from your file system.
from azure.storage.filedatalake import DataLakeFileClient file = DataLakeFileClient.from_connection_string("my_connection_string", file_system_name="myfilesystem", file_path="myfile") with open("./BlockDestination.txt", "wb") as my_file: download = file.download_file() download.readinto(my_file)
List the paths in your file system.
from azure.storage.filedatalake import FileSystemClient file_system = FileSystemClient.from_connection_string("my_connection_string", file_system_name="myfilesystem") paths = file_system.get_paths() for path in paths: print(path.name + '\n')
DataLake Storage clients raise exceptions defined in Azure Core.
All DataLake service operations will throw a StorageErrorException on failure with helpful error codes.
This library uses the standard logging library for logging. Basic information about HTTP sessions (URLs, headers, etc.) is logged at INFO level.
Detailed DEBUG level logging, including request/response bodies and unredacted
headers, can be enabled on a client with the
import sys import logging from azure.storage.filedatalake import DataLakeServiceClient # Create a logger for the 'azure.storage.filedatalake' SDK logger = logging.getLogger('azure.storage') logger.setLevel(logging.DEBUG) # Configure a console output handler = logging.StreamHandler(stream=sys.stdout) logger.addHandler(handler) # This client will log detailed information about its HTTP sessions, at DEBUG level service_client = DataLakeServiceClient.from_connection_string("your_connection_string", logging_enable=True)
logging_enable can enable detailed logging for a single operation,
even when it isn't enabled for the client:
More sample code
Get started with our Azure DataLake samples.
Several DataLake Storage Python SDK samples are available to you in the SDK's GitHub repository. These samples provide example code for additional scenarios commonly encountered while working with DataLake Storage:
datalake_samples_access_control.py- Examples for common DataLake Storage tasks:
- Set up a file system
- Create a directory
- Set/Get access control for the directory
- Create files under the directory
- Set/Get access control for each file
- Delete file system
datalake_samples_upload_download.py- Examples for common DataLake Storage tasks:
- Set up a file system
- Create file
- Append data to the file
- Flush data to the file
- Download the uploaded data
- Delete file system
This project welcomes contributions and suggestions. Most contributions require you to agree to a Contributor License Agreement (CLA) declaring that you have the right to, and actually do, grant us the rights to use your contribution. For details, visit https://cla.microsoft.com.
When you submit a pull request, a CLA-bot will automatically determine whether you need to provide a CLA and decorate the PR appropriately (e.g., label, comment). Simply follow the instructions provided by the bot. You will only need to do this once across all repos using our CLA.
Release history Release notifications | RSS feed
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
|Filename, size||File type||Python version||Upload date||Hashes|
|Filename, size azure_storage_file_datalake-12.1.2-py2.py3-none-any.whl (175.4 kB)||File type Wheel||Python version py2.py3||Upload date||Hashes View|
|Filename, size azure-storage-file-datalake-12.1.2.zip (251.2 kB)||File type Source||Python version None||Upload date||Hashes View|
Hashes for azure_storage_file_datalake-12.1.2-py2.py3-none-any.whl
Hashes for azure-storage-file-datalake-12.1.2.zip