Search code examples
pythonazureautomationsftp

Automate SFTP activation on ADLS


I have an Azure datalake storage account. On the Azure web interface, I can activate or deactivate the SFTP : Azure screen

I there a possibility to activate or deactivate SFTP using python ?

I found something using bard but that's not working :

from azure.storage.filedatalake import ADLSFileSystemClient

client = ADLSFileSystemClient.from_connection_string("connection_string")

client.set_sftp_access("my-filesystem", True)

Error :

ImportError: cannot import name 'ADLSFileSystemClient' from 'azure.storage.filedatalake' (lib/python3.8/site-packages/azure/storage/filedatalake/init.py)


Solution

  • Is there a possibility to activate or deactivate SFTP using Python?

    You can use the code below to Enable and disable SFTP using Azure Python SDK.

    Code:(Enable)

    from azure.mgmt.storage import StorageManagementClient
    from azure.identity import DefaultAzureCredential
    
    credential = DefaultAzureCredential()
    subscription_id="Your-subscription-id"
    storage_client = StorageManagementClient(credential, subscription_id)
    
    resource_group_name = "your-resource-grp-name"
    storage_account_name = "your-storage-account-name"
    
    credential = DefaultAzureCredential()
    storage_client = StorageManagementClient(credential, subscription_id)
    
    # Get the current properties of the storage account
    storage_account = storage_client.storage_accounts.get_properties(
        resource_group_name, storage_account_name
    )
    
    # Enable SFTP for the storage account
    storage_account.is_sftp_enabled=True
    updated_storage_account=storage_client.storage_accounts.update(
        resource_group_name, storage_account_name, storage_account
    )
    print("Updated Storage Account Properties:")
    print(f"Name: {updated_storage_account.name}")
    print(f"Is SFTP Enabled: {updated_storage_account.is_sftp_enabled}")
    

    Output:

    Updated Storage Account Properties:
    Name: venkat098
    Is SFTP Enabled: True
    

    enter image description here

    enter image description here

    Similarly, In the above code if you change storage_account.is_sftp_enabled=False it will disable the SFTP in the Azure storage account.

    Reference:

    Python Examples of azure.mgmt.storage.StorageManagementClient (programcreek.com)