Hello Denis Mukhamedov,
Thank you for posting your query here!
Can you please try with the following code to add a new access policy to an Azure Blob Storage container without deleting existing policies:
from azure.storage.blob import BlobServiceClient, ContainerSasPermissions, AccessPolicy
from datetime import datetime, timedelta
# Initialize the BlobServiceClient
storageAccountName = 'azure_account'
storageAccountKey = 'azure_key'
containerName = 'azure_container'
storageUrl = f'https://{storageAccountName}.blob.core.windows.net'
storageServiceClient = BlobServiceClient(account_url=storageUrl, credential=storageAccountKey)
# Get the existing access policies
containerServiceClient = storageServiceClient.get_container_client(containerName)
existing_policies = containerServiceClient.get_container_access_policy()
# Check if the 'signed_identifiers' field exists, create it if it doesn't
if 'signed_identifiers' not in existing_policies:
existing_policies['signed_identifiers'] = {}
# Define your new access policy
new_policy = AccessPolicy(permission=ContainerSasPermissions(read=True),
expiry=datetime.utcnow() + timedelta(hours=24),
start=datetime.utcnow() - timedelta(minutes=1))
# Add the new policy to the existing ones
existing_policies['signed_identifiers']['new_policy'] = new_policy
# Update the container with the full set of policies
containerServiceClient.set_container_access_policy(signed_identifiers=existing_policies['signed_identifiers'])
Do let us know if you have any further queries. I’m happy to assist you further.