Azure Storage Accounts
Globally unique resources that provide access to data management services and serve as the parent namespace for the services.
3,524 questions
This browser is no longer supported.
Upgrade to Microsoft Edge to take advantage of the latest features, security updates, and technical support.
How to automatically generate SAS Key for a given blob in Gen 2 storage account and store the key in key vault secret automatically post generation.
hello there,
you can check the SAS account
check the code
from azure.identity import DefaultAzureCredential
from azure.keyvault.secrets import SecretClient
from azure.storage.blob import BlobServiceClient, generate_blob_sas, ResourceTypes, AccountSasPermissions
Connect to the storage account using a connection string or other authentication method
connection_string = "<your_storage_connection_string>"
blob_service_client = BlobServiceClient.from_connection_string(connection_string)
Generate the SAS token for the blob or container
account_url = "<your_storage_account_url>"
container_name = "<your_container_name>"
blob_name = "<your_blob_name>"
sas_token = generate_blob_sas(
account_url=account_url,
container_name=container_name,
blob_name=blob_name,
account_key=None, # Set this if you have an account key instead of using SAS
resource_types=ResourceTypes(object=True),
permission=AccountSasPermissions(read=True, list=True),
expiry=<your_sas_expiry_time> # Specify the SAS token expiry time
)
Connect to the Key Vault using a managed identity or other authentication method
credential = DefaultAzureCredential()
key_vault_url = "<your_key_vault_url>"
secret_name = "<your_secret_name>"
secret_value = sas_token
Store the SAS key in the Key Vault secret
client = SecretClient(vault_url=key_vault_url, credential=credential)
client.set_secret(secret_name, secret_value)
print("SAS key
Make sure to replace the placeholder values (<your_storage_connection_string>, <your_storage_account_url>, <your_container_name>, <your_blob_name>, <your_sas_expiry_time>, <your_key_vault_url>, and <your_secret_name>) with your actual values.
And see if it helps,
Thank you
--If the reply is helpful, please Upvote and Accept as answer--