Getting Started with Azure File Storage

Azure File Storage, a Microsoft cloud storage solution for modern applications, offers a file-sharing system in the cloud. This system is easily accessible via Server Message Block (SMB) protocol or NFS protocol. Azure File Storage solution is a useful replacement or supplement for the on-site servers. Not only the file system is fully managed, but it can also be accessed via SMB, HTTP, and NFS protocols. 

This article will cover how to create a simple text file and store it in Azure File Storage. Furthermore, it will also explain how to perform other CRUD operations on the file using Python library, PyPI, and Azure Python SDK.  

Prerequisites

For this tutorial, you will need the Python 3.5.x version. Also, make sure to use an account with Active Azure Subscription. Furthermore, this tutorial will require an existing Azure Storage Account. You can create one from the official website

1. Create File Shares in Azure Storage:

To get started with file sharing, click on the “File Shares” in your Azure Storage account. 

Next, hit “New File Share” on the top. A panel will appear on the right-hand side of the screen. Type the name. In our case, we are giving it a “demo-share”.

 

Next, mention the Quota. A quota is the maximum capacity of a file-sharing system. Click on the “Create” button, and your File Share resource will appear on the screen. You can also perform this step using the Python code, but it will require you to install the following package:

pip install azure-storage-file-share

Alternatively, you can use the following package for Azure SDK v2:

pip install azure-storage-file

However, Azure SDK v2 requires azure-storage-file and azure-storage-common packages.

2. Get the Connection String:

To get started with the File Share, get the key from the “Access Key” under the “Settings” panel of your Azure Storage Account. Next, set the connection string as shown below: 

AZURE_STORAGE_CONNECTION_STRING = "Your_Connection_String" 

3. Install the Required Packages:

The next step is to install the required packages for the Azure File Storage. Since we are using Azure Storage SDK v12, use the following command to install the required packages:

pip install azure-storage-file-share

4. Create the Sample File in Python:

In this step, we will create a sample txt file using Python which will be stored in our Azure File Share. 

# Create a file in the local data directory to upload and download

newFile = open("demo.txt","w+")

for i in range(10):

     newFile.write("This is line %d\r\n" % (i+1))

newFile.close()

5. Create Azure File Storage Client:

Now that our Azure Storage Account and our resource is ready, the next step is to set up a client to communicate with the Azure Container. Through this client, you can communicate with the Azure Storage Account itself, file shares, directories, and files. Run the following code to create the client:

from azure.storage.fileshare import ShareServiceClient

# Create a ShareClient from a connection string

share_name = demo-share

service = ShareServiceClient.from_connection_string(AZURE_STORAGE_CONNECTION_STRING , share_name)

share_client.create_share()

Head over to Azure File Share to check the file on the cloud.

6. Create/ Upload the Sample File on the Azure File Share:

Now that the AzureFile Client is ready, you can easily upload the sample file that you have created using the following code:

local_path = "./data"

os.mkdir(local_path)

source_file = "demo.txt"

local_file_path = os.path.join(local_path, source_file)

source_file = open(local_file_path, "rb")

data = source_file.read()

# Create a ShareFileClient from a connection string

file_client = ShareFileClient.from_connection_string(

            AZURE_STORAGE_CONNECTION_STRING, share_name, dest_file_path)

file_client.upload_file(data)

7. Download File from Azure File Share:

To read the file that you have uploaded on the Azure File Share, execute the following module:

# Build the remote path

source_file_path = dir_name + "/" + file_name

# Create a ShareFileClient from a connection string

file_client = ShareFileClient.from_connection_string(

            AZURE_STORAGE_CONNECTION_STRING, share_name, source_file_path)

8. Edit the File on Azure File Share:

You can also open the file directly from Azure File Share and edit the file components. For this step, execute the following code:

 # Open a file for writing bytes on the local system

with open(source_file , "wb") as data:

# Download the file from Azure into a stream

stream = file_client.download_file()

     # Write the stream to the local file

     data.write(stream.readall())

Hence, the new data will be written in the file. 

9. Delete the File from the Azure File Share:

Just like any storage, delete operation is also possible in Azure File Share using the following code:

# Create a ShareFileClient from a connection string

file_client = ShareFileClient.from_connection_string(

            AZURE_STORAGE_CONNECTION_STRING, share_name, file_path)

# Delete the file

file_client.delete_file()

After executing the command, you can check your File Share to confirm that the operation has been successfully carried out. 

Conclusion

In conclusion, the Azure File Share provides functionality to perform CRUD operations on the files stored in the Azure Storage. The article has covered the create, read, update and delete operations. There are other functionalities such as listing the files, creating directories, and enumerating the files which you can cover from its official website.

We hope this guide will assist you in setting up your Azure File Storage and performing CRUD operations using Python and Azure Storage SDK successfully. 

Happy coding!


Similar Articles