Azure Documentation

Using Azure File Storage with Python

This document provides a comprehensive guide on how to interact with Azure File Shares using the Python SDK. Azure Files offers fully managed cloud file shares that are accessible via the industry-standard Server Message Block (SMB) protocol.

Prerequisites

  • An Azure subscription.
  • An Azure Storage Account.
  • A file share within your storage account.
  • Python 3.7 or later installed.
  • The Azure Storage File Share SDK for Python installed. You can install it using pip:
    pip install azure-storage-file-share

Connecting to Your File Share

To connect to your file share, you'll need your storage account name and its access key. You can find these in the Azure portal under your storage account's "Access keys" section.

Here's how to get the connection string:


from azure.storage.file_share import ShareServiceClient

# Replace with your actual storage account name and key
account_name = "your_storage_account_name"
account_key = "your_storage_account_access_key"

# Create a ShareServiceClient
share_service_client = ShareServiceClient(account_url=f"https://{account_name}.file.core.windows.net", credential=account_key)
                

Security Note: Storing access keys directly in code is not recommended for production environments. Consider using Azure Key Vault or managed identities for secure credential management.

Working with File Shares

Creating a File Share

You can create a new file share programmatically:


# Replace 'myshare' with your desired share name
share_name = "myshare"
try:
    share_client = share_service_client.create_share(share_name)
    print(f"Share '{share_name}' created successfully.")
except Exception as e:
    print(f"Error creating share: {e}")
                

Listing File Shares

To list all the file shares in your storage account:


print("Listing shares:")
for share in share_service_client.list_shares():
    print(f"- {share.name}")
                

Working with Directories

Creating a Directory

File shares are organized into directories. You can create them like this:


# Get a ShareClient for an existing share
share_client = share_service_client.get_share_client(share_name)

# Replace 'mydirectory' with your desired directory name
directory_name = "mydirectory"
directory_client = share_client.get_directory_client(directory_name)
try:
    directory_client.create_directory()
    print(f"Directory '{directory_name}' created successfully.")
except Exception as e:
    print(f"Error creating directory: {e}")
                

Listing Directories

To list directories within a share or another directory:


print(f"Listing directories in share '{share_name}':")
for directory in share_client.list_directories_and_files():
    if directory.is_directory:
        print(f"- {directory.name}")
                

Working with Files

Uploading a File

To upload a file, you first need to create a file client and then upload the content. Let's assume you have a local file named local_file.txt.


from pathlib import Path

# Create a FileClient for a file within a directory
file_path = Path(directory_name) / "my_uploaded_file.txt"
file_client = share_client.get_file_client(file_path)

local_file_path = "local_file.txt"
file_content = "This is the content of my uploaded file."

with open(local_file_path, "w") as f:
    f.write(file_content)

with open(local_file_path, "rb") as data:
    try:
        file_client.upload_file(data)
        print(f"File '{file_path}' uploaded successfully.")
    except Exception as e:
        print(f"Error uploading file: {e}")
                

Downloading a File

To download a file from the share:


download_path = "downloaded_file.txt"
print(f"Downloading file '{file_path}' to '{download_path}'...")

try:
    with open(download_path, "wb") as download_file:
        download_stream = file_client.download_file()
        download_file.write(download_stream.readall())
    print(f"File downloaded successfully.")
except Exception as e:
    print(f"Error downloading file: {e}")
                

Listing Files

To list files within a directory:


print(f"Listing files in directory '{directory_name}':")
for item in share_client.list_directories_and_files(directory_name):
    if not item.is_directory:
        print(f"- {item.name}")
                

Deleting a File

To delete a file:


try:
    file_client.delete_file()
    print(f"File '{file_path}' deleted successfully.")
except Exception as e:
    print(f"Error deleting file: {e}")
                

Conclusion

Azure File Storage provides a robust and scalable solution for cloud-based file storage. The Azure Storage File Share SDK for Python makes it easy to integrate these capabilities into your Python applications. Remember to handle credentials securely and refer to the official Azure documentation for advanced features and best practices.

Tip: Explore features like setting file metadata, managing file properties, and handling concurrent access for more complex scenarios.