| name | azure-storage-file-share-py |
| description | Azure Storage File Share SDK for Python. Use for SMB file shares, directories, and file operations in the cloud.
Triggers: "azure-storage-file-share", "ShareServiceClient", "ShareClient", "file share", "SMB". |
| license | MIT |
| metadata | {"author":"Microsoft","version":"1.0.0"} |
Azure Storage File Share SDK for Python
Manage SMB file shares for cloud-native and lift-and-shift scenarios.
Installation
pip install azure-storage-file-share
Environment Variables
AZURE_STORAGE_ACCOUNT_URL=https://<account>.file.core.windows.net
AZURE_TOKEN_CREDENTIALS=prod
Authentication & Lifecycle
🔑 Two rules apply to every code sample below:
- Prefer
DefaultAzureCredential. It works locally (Azure CLI / VS Code / Developer CLI) and in Azure (managed identity, workload identity) with no code change. Avoid connection strings, account/API keys — they bypass Entra audit and rotation.
- Local dev:
DefaultAzureCredential works as-is.
- Production: set
AZURE_TOKEN_CREDENTIALS=prod (or AZURE_TOKEN_CREDENTIALS=<specific_credential>) to constrain the credential chain to production-safe credentials.
- Wrap every client in a context manager so HTTP transports, sockets, and token caches are released deterministically:
- Sync:
with <Client>(...) as client:
- Async:
async with <Client>(...) as client: and async with DefaultAzureCredential() as credential: (from azure.identity.aio)
Snippets may abbreviate this setup, but production code should always follow both rules.
from azure.storage.fileshare import ShareServiceClient
from azure.identity import DefaultAzureCredential, ManagedIdentityCredential
credential = DefaultAzureCredential(require_envvar=True)
with ShareServiceClient(
account_url=os.environ["AZURE_STORAGE_ACCOUNT_URL"],
credential=credential
) as service:
...
Share Operations
Create Share
share = service.create_share("my-share")
List Shares
for share in service.list_shares():
print(f"{share.name}: {share.quota} GB")
Get Share Client
share_client = service.get_share_client("my-share")
Delete Share
service.delete_share("my-share")
Directory Operations
Create Directory
share_client = service.get_share_client("my-share")
share_client.create_directory("my-directory")
share_client.create_directory("my-directory/sub-directory")
List Directories and Files
directory_client = share_client.get_directory_client("my-directory")
for item in directory_client.list_directories_and_files():
if item["is_directory"]:
print(f"[DIR] {item['name']}")
else:
print(f"[FILE] {item['name']} ({item['size']} bytes)")
Delete Directory
share_client.delete_directory("my-directory")
File Operations
Upload File
file_client = share_client.get_file_client("my-directory/file.txt")
file_client.upload_file("Hello, World!")
with open("local-file.txt", "rb") as f:
file_client.upload_file(f)
file_client.upload_file(b"Binary content")
Download File
file_client = share_client.get_file_client("my-directory/file.txt")
data = file_client.download_file().readall()
with open("downloaded.txt", "wb") as f:
data = file_client.download_file()
data.readinto(f)
download = file_client.download_file()
for chunk in download.chunks():
process(chunk)
Get File Properties
properties = file_client.get_file_properties()
print(f"Size: {properties.size}")
print(f"Content type: {properties.content_settings.content_type}")
print(f"Last modified: {properties.last_modified}")
Delete File
file_client.delete_file()
Copy File
source_url = "https://account.file.core.windows.net/share/source.txt"
dest_client = share_client.get_file_client("destination.txt")
dest_client.start_copy_from_url(source_url)
Range Operations
Upload Range
file_client.upload_range(data=b"content", offset=0, length=7)
Download Range
download = file_client.download_file(offset=0, length=100)
data = download.readall()
Snapshot Operations
Create Snapshot
snapshot = share_client.create_snapshot()
print(f"Snapshot: {snapshot['snapshot']}")
Access Snapshot
snapshot_client = service.get_share_client(
"my-share",
snapshot=snapshot["snapshot"]
)
Async Client
from azure.storage.fileshare.aio import ShareServiceClient
from azure.identity.aio import DefaultAzureCredential
async def upload_file():
async with DefaultAzureCredential() as credential:
async with ShareServiceClient(account_url, credential=credential) as service:
share = service.get_share_client("my-share")
file_client = share.get_file_client("test.txt")
await file_client.upload_file("Hello!")
Client Types
| Client | Purpose |
|---|
ShareServiceClient | Account-level operations |
ShareClient | Share operations |
ShareDirectoryClient | Directory operations |
ShareFileClient | File operations |
Best Practices
- Pick sync OR async and stay consistent. Do not mix
azure.storage.fileshare sync clients with azure.storage.fileshare.aio async clients in the same call path. Choose one mode per module.
- Always use context managers for clients and async credentials. Wrap every client in
with ShareServiceClient(...) as client: (sync) or async with ShareServiceClient(...) as client: (async). For async DefaultAzureCredential from azure.identity.aio, also use async with credential: so tokens and transports are cleaned up.
- Use
DefaultAzureCredential for portable auth across local dev and Azure (avoid connection strings / API keys when possible).
- Use Microsoft Entra ID for production with RBAC
- Stream large files using chunks() to avoid memory issues
- Create snapshots before major changes
- Set quotas to prevent unexpected storage costs
- Use ranges for partial file updates
Reference Files