Share via


Azure File Share code samples using Python version 2 client libraries

This article shows code samples that use version 2 of the Azure File Share client library for Python.

On March 31, 2023, we retired support for Azure SDK libraries which do not conform to the current Azure SDK guidelines. The new Azure SDK libraries are updated regularly to drive consistent experiences and strengthen your security posture. It's recommended that you transition to the new Azure SDK libraries to take advantage of the new capabilities and critical security updates.

Although the older libraries can still be used beyond March 31, 2023, they'll no longer receive official support and updates from Microsoft. For more information, see the support retirement announcement.

Prerequisites

Install the following package using pip install:

pip install azure-storage-file

Add the following import statement:

from azure.storage.file import FileService

Create an Azure file share

Related article: Develop for Azure Files with Python

The following code example uses a FileService object to create the share if it doesn't exist.

file_service.create_share('myshare')

Create a directory

Related article: Develop for Azure Files with Python

You can organize storage by putting files inside subdirectories instead of having all of them in the root directory.

The code below will create a subdirectory named sampledir under the root directory.

file_service.create_directory('myshare', 'sampledir')

Upload a file

Related article: Develop for Azure Files with Python

In this section, you'll learn how to upload a file from local storage into Azure Files.

An Azure file share contains, at the least, a root directory where files can reside. To create a file and upload data, use any of the following methods:

These methods perform the necessary chunking when the size of the data exceeds 64 MiB.

create_file_from_path uploads the contents of a file from the specified path, and create_file_from_stream uploads the contents from an already opened file/stream. create_file_from_bytes uploads an array of bytes, and create_file_from_text uploads the specified text value using the specified encoding (defaults to UTF-8).

The following example uploads the contents of the sunset.png file into the myfile file.

from azure.storage.file import ContentSettings
file_service.create_file_from_path(
    'myshare',
    None,  # We want to create this file in the root directory, so we specify None for the directory_name
    'myfile',
    'sunset.png',
    content_settings=ContentSettings(content_type='image/png'))

Enumerate files and directories in an Azure file share

Related article: Develop for Azure Files with Python

To list the files and directories in a share, use the list_directories_and_files method. This method returns a generator. The following code outputs the name of each file and directory in a share to the console.

generator = file_service.list_directories_and_files('myshare')
for file_or_dir in generator:
    print(file_or_dir.name)

Download a file

Related article: Develop for Azure Files with Python

To download data from a file, use any of the following methods:

These methods perform the necessary chunking when the size of the data exceeds 64 MiB.

The following example demonstrates using get_file_to_path to download the contents of the myfile file and store it to the out-sunset.png file.

file_service.get_file_to_path('myshare', None, 'myfile', 'out-sunset.png')

Create a share snapshot

Related article: Develop for Azure Files with Python

You can create a point in time copy of your entire file share.

snapshot = file_service.snapshot_share(share_name)
snapshot_id = snapshot.snapshot

Create share snapshot with metadata

metadata = {"foo": "bar"}
snapshot = file_service.snapshot_share(share_name, metadata=metadata)

List shares and snapshots

Related article: Develop for Azure Files with Python

You can list all the snapshots for a particular share.

shares = list(file_service.list_shares(include_snapshots=True))

Browse share snapshot

Related article: Develop for Azure Files with Python

You can browse each share snapshot to retrieve files and directories from that point in time.

directories_and_files = list(
    file_service.list_directories_and_files(share_name, snapshot=snapshot_id))

Get file from share snapshot

Related article: Develop for Azure Files with Python

You can download a file from a share snapshot. This enables you to restore a previous version of a file.

with open(FILE_PATH, 'wb') as stream:
    file = file_service.get_file_to_stream(
        share_name, directory_name, file_name, stream, snapshot=snapshot_id)

Delete a single share snapshot

Related article: Develop for Azure Files with Python

You can delete a single share snapshot.

file_service.delete_share(share_name, snapshot=snapshot_id)

Delete a file

Related article: Develop for Azure Files with Python

To delete a file, call delete_file.

The following code example shows how to delete a file:

file_service.delete_file('myshare', None, 'myfile')

Delete share when share snapshots exist

Related article: Develop for Azure Files with Python

A share that contains snapshots cannot be deleted unless all the snapshots are deleted first.

The following code example shows how to delete a share:

file_service.delete_share(share_name, delete_snapshots=DeleteSnapshot.Include)