Microsoft Azure File DataLake Storage Client Library for Python
Overview
This preview package for Python includes ADLS Gen2 specific API support made available in Storage SDK. This includes:
Source code | Package (PyPi) | Package (Conda) | API reference documentation | Product documentation | Samples
Install the Azure DataLake Storage client library for Python with pip:
pip install azure-storage-file-datalake --pre
If you wish to create a new storage account, you can use the Azure Portal, Azure PowerShell, or Azure CLI:
# 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
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:
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:
This library includes a complete async API supported on Python 3.5+. To use it, you must first install an async transport, such as aiohttp. See azure-core documentation for more information.
Async clients and credentials should be closed when they're no longer needed. These
objects are async context managers and define async close
methods.
The DataLake Storage SDK provides four different clients to interact with the DataLake Service:
get_file_client
, get_directory_client
or get_file_system_client
functions.get_file_client
function.
For operations relating to a specific directory, the client can be retrieved using
the get_directory_client
function.The following sections provide several code snippets covering some of the most common Storage DataLake tasks, including:
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")
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.create_file ()
file.append_data(data, offset=0, length=len(data))
file.flush_data(len(data))
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')
Optional keyword arguments that can be passed in at the client and per-operation level.
Use the following keyword arguments when instantiating a client to configure the retry policy:
retry_total=0
if you do not want to retry on requests. Defaults to 10.False
.Other optional configuration keyword arguments that can be specified on the client or per-operation.
Client keyword arguments:
Per-operation keyword arguments:
headers={'CustomValue': value}
DataLake Storage clients raise exceptions defined in Azure Core.
This list can be used for reference to catch thrown exceptions. To get the specific error code of the exception, use the error_code
attribute, i.e, exception.error_code
.
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 logging_enable
argument:
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)
Similarly, logging_enable
can enable detailed logging for a single operation,
even when it isn't enabled for the client:
service_client.list_file_systems(logging_enable=True)
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:
datalake_samples_upload_download.py
- Examples for common DataLake Storage tasks:
Table for ADLS Gen1 to ADLS Gen2 API Mapping For more extensive REST documentation on Data Lake Storage Gen2, see the Data Lake Storage Gen2 documentation on docs.microsoft.com.
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.
This project has adopted the Microsoft Open Source Code of Conduct. For more information see the Code of Conduct FAQ or contact opencode@microsoft.com with any additional questions or comments.