Azure Storage File Share

12.24.0 · active · verified Sun Mar 29

Microsoft Azure Azure File Share Storage Client Library for Python version 12.24.0. This library provides fully managed file shares in the cloud, accessible via the industry-standard Server Message Block (SMB) protocol. It is part of the extensive Azure SDK for Python, which typically adheres to a continuous release cadence, delivering frequent updates and bug fixes across its various client libraries.

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to initialize a `ShareServiceClient` using a storage account connection string and then list all existing file shares in the account. Ensure the `STORAGE_CONNECTION_STRING` environment variable is set with your Azure Storage account connection string.

import os
from azure.storage.fileshare import ShareServiceClient
from azure.core.exceptions import ResourceNotFoundError

# Retrieve the connection string from an environment variable
# Go to Azure Portal -> Storage Accounts -> your_storage_account -> Access keys
# Copy the 'Connection string' value
connection_string = os.environ.get("STORAGE_CONNECTION_STRING", "DefaultEndpointsProtocol=https;AccountName=YOUR_ACCOUNT_NAME;AccountKey=YOUR_ACCOUNT_KEY;EndpointSuffix=core.windows.net")

if "YOUR_ACCOUNT_NAME" in connection_string or "YOUR_ACCOUNT_KEY" in connection_string:
    print("Please set the STORAGE_CONNECTION_STRING environment variable or replace placeholder values in the quickstart code.")
    exit(1)

try:
    # Create the ShareServiceClient object from a connection string
    service_client = ShareServiceClient.from_connection_string(connection_string)

    # List shares in the storage account
    print("Listing shares in the storage account:")
    for share in service_client.list_shares():
        print(f"- {share.name}")

    print("\nSuccessfully listed file shares.")

except Exception as e:
    print(f"An error occurred: {e}")

view raw JSON →