5 min,  azure,  Python

5 min Create and configure an Azure Files shares connect with Python SDK (con str)

https://docs.microsoft.com/en-us/azure/storage/files/storage-how-to-create-file-share?tabs=azure-portal

Questions to think about:
What are the performance requirements for your Azure file share?
What are your redundancy requirements for your Azure file share?
What size file share do you need?

File share type: SMB connect SMB protocol 3 or higher

Create a storage account:
Basics
General purpose version 2 (GPv2) storage accounts: HDD-based, ++
FileStorage storage accounts: SSD-based, ++

Replication: Although this is labeled replication, this field actually means redundancy
Networking
Data protection
Advanced
Secure transfer required:
This field indicates whether the storage account requires encryption in transit for communication to the storage account.
If you require SMB 2.1 support, you must disable this.
Large file shares:up to 100 TiB (LRS, ZRS)

NOTE:
Selecting the blob access tier does not affect the tier of the file share.

(Before you create an Azure filse share on the account, you can:Enable large files shares on an existing account)

Create a file share:

Edit quota also

Connect:

We can mount it on Windows /Linux/mac but we have already done that in a previous tutorial.

The connect button has a script for win/lin and mac.

Add a directory and upload two file’s from portal, storage explorer or SDK’S.

To give access you have many options: to the account, to the folder, to the files…

Azure Storage File Share client library for Python – Version 12.6.0

Azure Storage File Share client library for Python | Microsoft Docs

Upgrade pip if necessary

pip install azure-storage-file-share

Auth SAS token

from datetime import datetime, timedelta
from azure.storage.fileshare import ShareServiceClient, generate_account_sas, ResourceTypes, AccountSasPermissions

sas_token = generate_account_sas(
account_name=”“,
account_key=”“,
resource_types=ResourceTypes(service=True),
permission=AccountSasPermissions(read=True),
expiry=datetime.utcnow() + timedelta(hours=1)
)

share_service_client = ShareServiceClient(account_url=”https://.file.core.windows.net”, credential=sas_token)

Auth KEY

from azure.storage.fileshare import ShareServiceClient
service = ShareServiceClient(account_url=”https://.file.core.windows.net”, credential=”“)

Connection string

from azure.storage.fileshare import ShareServiceClient

connection_string = “DefaultEndpointsProtocol=https;AccountName=xxxx;AccountKey=xxxx;EndpointSuffix=core.windows.net”
service = ShareServiceClient.from_connection_string(conn_str=connection_string)

Access the storage account on security + networking

Listing contents of directory with Python:

Here is what we have in the file share:

Now lets list some meta data:

(Python OOP code is at git)

Now lets upload a local file.

New code

# Uploading a file
file_client = ShareFileClient.from_connection_string(conn_str=connection_string, share_name=”testit3fs”, file_path=”testdir/az_local_file.txt”)

with open(“az_local_file.txt”, “rb”) as source_file:
rv = file_client.upload_file(source_file)
print(rv)

Result in VSC

Result in Azure fs

Rotate Key

Now lets rotate the key we used (key 1), we should get a connection error.

Go to storage account->access key->rotatet key 1

Successfully regenerated access key ‘key1’ for storage account ‘testit3straccount’.

  • run the python .\az_storage_py_sdk.py
  • to list the dirs
  • comment out the fs_upload_file()

And we do not have a valid key now as expected.

Get the key 1, Connection string and update the config in the Python project and run the same script

And success

You can also create a share and delete share and files, as well as download.

There are a lot of code ready at:

Azure Storage File Share client library for Python | Microsoft Docs

Just look at section the bottom “More sample code”

or samples

azure-sdk-for-python/sdk/storage/azure-storage-file-share/samples at azure-storage-file-share_12.6.0 · Azure/azure-sdk-for-python (github.com)

There is also a REST SERVICE for almost everything in Azure

https://docs.microsoft.com/en-us/rest/api/storageservices/

Comments Off on 5 min Create and configure an Azure Files shares connect with Python SDK (con str)