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 |
[!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.
File share type | SMB | NFS |
---|---|---|
Standard file shares (GPv2), LRS/ZRS | ||
Standard file shares (GPv2), GRS/GZRS | ||
Premium file shares (FileStorage), LRS/ZRS |
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+.
To install via the Python Package Index (PyPI), type:
pip install azure-storage-file-share
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":::
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":::
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":::
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":::
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":::
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":::
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":::
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":::
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":::
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":::
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":::
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":::
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":::
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":::
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.