Account management operations on Azure Data Lake Storage Gen1 using Python
Learn how to use the Python SDK for Azure Data Lake Storage Gen1 to perform basic account management operations such as create a Data Lake Storage Gen1 account, list the Data Lake Storage Gen1 accounts, etc. For instructions on how to perform filesystem operations on Data Lake Storage Gen1 using Python, see Filesystem operations on Data Lake Storage Gen1 using Python.
Prerequisites
Python. You can download Python from here. This article uses Python 3.6.2.
An Azure subscription. See Get Azure free trial.
An Azure resource group. For instructions, see Create an Azure resource group.
Install the modules
To work with Data Lake Storage Gen1 using Python, you need to install three modules.
- The
azure-mgmt-resource
module, which includes Azure modules for Active Directory, etc. - The
azure-mgmt-datalake-store
module, which includes the Azure Data Lake Storage Gen1 account management operations. For more information on this module, see Azure Data Lake Storage Gen1 Management module reference. - The
azure-datalake-store
module, which includes the Azure Data Lake Storage Gen1 filesystem operations. For more information on this module, see azure-datalake-store filesystem module reference.
Use the following commands to install the modules.
pip install azure-identity
pip install azure-mgmt-resource
pip install azure-mgmt-datalake-store
pip install azure-datalake-store
Create a new Python application
In the IDE of your choice create a new Python application, for example, mysample.py.
Add the following snippet to import the required modules:
# Acquire a credential object for the app identity. When running in the cloud, # DefaultAzureCredential uses the app's managed identity (MSI) or user-assigned service principal. # When run locally, DefaultAzureCredential relies on environment variables named # AZURE_CLIENT_ID, AZURE_CLIENT_SECRET, and AZURE_TENANT_ID. from azure.identity import DefaultAzureCredential ## Required for Data Lake Storage Gen1 account management from azure.mgmt.datalake.store import DataLakeStoreAccountManagementClient from azure.mgmt.datalake.store.models import CreateDataLakeStoreAccountParameters ## Required for Data Lake Storage Gen1 filesystem management from azure.datalake.store import core, lib, multithread # Common Azure imports import adal from azure.mgmt.resource.resources import ResourceManagementClient from azure.mgmt.resource.resources.models import ResourceGroup # Use these as needed for your application import logging, getpass, pprint, uuid, time
Save changes to mysample.py.
Authentication
In this section, we talk about the different ways to authenticate with Microsoft Entra ID. The options available are:
- For end-user authentication for your application, see End-user authentication with Data Lake Storage Gen1 using Python.
- For service-to-service authentication for your application, see Service-to-service authentication with Data Lake Storage Gen1 using Python.
Create client and Data Lake Storage Gen1 account
The following snippet first creates the Data Lake Storage Gen1 account client. It uses the client object to create a Data Lake Storage Gen1 account. Finally, the snippet creates a filesystem client object.
## Declare variables
subscriptionId = 'FILL-IN-HERE'
adlsAccountName = 'FILL-IN-HERE'
resourceGroup = 'FILL-IN-HERE'
location = 'eastus2'
credential = DefaultAzureCredential()
## Create Data Lake Storage Gen1 account management client object
adlsAcctClient = DataLakeStoreAccountManagementClient(credential, subscription_id=subscriptionId)
## Create a Data Lake Storage Gen1 account
adlsAcctResult = adlsAcctClient.accounts.begin_create(
resourceGroup,
adlsAccountName,
CreateDataLakeStoreAccountParameters(
location=location
)
)
List the Data Lake Storage Gen1 accounts
## List the existing Data Lake Storage Gen1 accounts
result_list_response = adlsAcctClient.accounts.list()
result_list = list(result_list_response)
for items in result_list:
print(items)
Delete the Data Lake Storage Gen1 account
## Delete an existing Data Lake Storage Gen1 account
adlsAcctClient.accounts.begin_delete(resourceGroup, adlsAccountName)