Skip to content

Latest commit

 

History

History
149 lines (87 loc) · 7.61 KB

storage-python-how-to-use-file-storage.md

File metadata and controls

149 lines (87 loc) · 7.61 KB
title titleSuffix description author ms.service ms.topic ms.date ms.author ms.custom
Develop for Azure Files with Python
Azure Storage
Learn how to develop Python applications and services that use Azure Files to store file data. Create and delete files, file shares, and directories.
pauljewellmsft
azure-file-storage
how-to
05/13/2024
pauljewell
devx-track-python, py-fresh-zinc

Develop Python applications that use Azure Files

[!INCLUDE storage-selector-file-include]

Learn the basics of using Python to develop apps or services that use Azure Files to store file data. Create a console app and learn how to perform basic actions with Python and Azure Files:

  • Create Azure file shares
  • Create directories
  • Enumerate files and directories in an Azure file share
  • Upload, download, and delete a file
  • Create file share backups by using snapshots

Note

Because Azure Files may be accessed over SMB, it's possible to write simple applications that access the Azure file share using the standard Python I/O classes and functions. This article describes how to write apps that use the Azure Storage SDK for Python, which uses the Azure Files REST API to talk to Azure Files.

Applies to

File share type SMB NFS
Standard file shares (GPv2), LRS/ZRS Yes No
Standard file shares (GPv2), GRS/GZRS Yes No
Premium file shares (FileStorage), LRS/ZRS Yes No

Download and Install Azure Storage SDK for Python

Note

If you're upgrading from the Azure Storage SDK for Python version 0.36 or earlier, uninstall the older SDK using pip uninstall azure-storage before installing the latest package.

The Azure Files client library for Python requires Python 3.8+.

Install via PyPI

To install via the Python Package Index (PyPI), type:

pip install azure-storage-file-share

Set up your application to use Azure Files

Add the following code near the top of a Python source file to use the code snippets in this article.

:::code language="python" source="~/azure-storage-snippets/files/howto/python/python-v12/file_share_ops.py" id="Snippet_Imports":::

Set up a connection to Azure Files

ShareServiceClient lets you work with shares, directories, and files. This code creates a ShareServiceClient object using the storage account connection string:

:::code language="python" source="~/azure-storage-snippets/files/howto/python/python-v12/file_share_ops.py" id="Snippet_CreateShareServiceClient":::

Create an Azure file share

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

:::code language="python" source="~/azure-storage-snippets/files/howto/python/python-v12/file_share_ops.py" id="Snippet_CreateFileShare":::

Create a directory

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

The following method creates a directory in the root of the specified file share by using a ShareDirectoryClient object.

:::code language="python" source="~/azure-storage-snippets/files/howto/python/python-v12/file_share_ops.py" id="Snippet_CreateDirectory":::

Upload a file

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

The following method uploads the contents of the specified file into the specified directory in the specified Azure file share.

:::code language="python" source="~/azure-storage-snippets/files/howto/python/python-v12/file_share_ops.py" id="Snippet_UploadFile":::

Enumerate files and directories in an Azure file share

To list the files and directories in a subdirectory, use the list_directories_and_files method. This method returns an auto-paging iterable. The following code outputs the name of each file and subdirectory in the specified directory to the console.

:::code language="python" source="~/azure-storage-snippets/files/howto/python/python-v12/file_share_ops.py" id="Snippet_ListFilesAndDirs":::

Download a file

To download data from a file, use download_file.

The following example demonstrates using download_file to get the contents of the specified file and store it locally with DOWNLOADED- prepended to the filename.

:::code language="python" source="~/azure-storage-snippets/files/howto/python/python-v12/file_share_ops.py" id="Snippet_DownloadFile":::

Create a share snapshot

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

:::code language="python" source="~/azure-storage-snippets/files/howto/python/python-v12/file_share_ops.py" id="Snippet_CreateSnapshot":::

List shares and snapshots

You can list all the snapshots for a particular share.

:::code language="python" source="~/azure-storage-snippets/files/howto/python/python-v12/file_share_ops.py" id="Snippet_ListSharesAndSnapshots":::

Browse share snapshot

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

:::code language="python" source="~/azure-storage-snippets/files/howto/python/python-v12/file_share_ops.py" id="Snippet_BrowseSnapshotDir":::

Get file from share snapshot

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

:::code language="python" source="~/azure-storage-snippets/files/howto/python/python-v12/file_share_ops.py" id="Snippet_DownloadSnapshotFile":::

Delete a single share snapshot

You can delete a single share snapshot.

:::code language="python" source="~/azure-storage-snippets/files/howto/python/python-v12/file_share_ops.py" id="Snippet_DeleteSnapshot":::

Delete a file

To delete a file, call delete_file.

:::code language="python" source="~/azure-storage-snippets/files/howto/python/python-v12/file_share_ops.py" id="Snippet_DeleteFile":::

Delete share when share snapshots exist

To delete a share that contains snapshots, call delete_share with delete_snapshots=True.

:::code language="python" source="~/azure-storage-snippets/files/howto/python/python-v12/file_share_ops.py" id="Snippet_DeleteShare":::

Next steps

Now that you've learned how to manipulate Azure Files with Python, follow these links to learn more.

For related code samples using deprecated Python version 2 SDKs, see Code samples using Python version 2.