classAzureBlobStorageCredentials(Block):""" Stores credentials for authenticating with Azure Blob Storage. Args: account_url: The URL for your Azure storage account. If provided, the account URL will be used to authenticate with the discovered default Azure credentials. connection_string: The connection string to your Azure storage account. If provided, the connection string will take precedence over the account URL. Example: Load stored Azure Blob Storage credentials and retrieve a blob service client: ```python from prefect_azure import AzureBlobStorageCredentials azure_credentials_block = AzureBlobStorageCredentials.load("BLOCK_NAME") blob_service_client = azure_credentials_block.get_blob_client() ``` """_block_type_name="Azure Blob Storage Credentials"_logo_url="https://cdn.sanity.io/images/3ugk85nk/production/54e3fa7e00197a4fbd1d82ed62494cb58d08c96a-250x250.png"# noqa_documentation_url="https://prefecthq.github.io/prefect-azure/credentials/#prefect_azure.credentials.AzureBlobStorageCredentials"# noqaconnection_string:Optional[SecretStr]=Field(default=None,description=("The connection string to your Azure storage account. If provided, the ""connection string will take precedence over the account URL."),)account_url:Optional[str]=Field(default=None,title="Account URL",description=("The URL for your Azure storage account. If provided, the account ""URL will be used to authenticate with the discovered default ""Azure credentials."),)@root_validatordefcheck_connection_string_or_account_url(cls,values:Dict[str,Any])->Dict[str,Any]:""" Checks that either a connection string or account URL is provided, not both. """has_account_url=values.get("account_url")isnotNonehas_conn_str=values.get("connection_string")isnotNoneifnothas_account_urlandnothas_conn_str:raiseValueError("Must provide either a connection string or an account URL.")ifhas_account_urlandhas_conn_str:raiseValueError("Must provide either a connection string or account URL, but not both.")returnvalues@_raise_help_msg("blob_storage")defget_client(self)->"BlobServiceClient":""" Returns an authenticated base Blob Service client that can be used to create other clients for Azure services. Example: Create an authorized Blob Service session ```python import os import asyncio from prefect import flow from prefect_azure import AzureBlobStorageCredentials @flow async def example_get_client_flow(): connection_string = os.getenv("AZURE_STORAGE_CONNECTION_STRING") azure_credentials = AzureBlobStorageCredentials( connection_string=connection_string, ) async with azure_credentials.get_client() as blob_service_client: # run other code here pass asyncio.run(example_get_client_flow()) ``` """ifself.connection_stringisNone:returnBlobServiceClient(account_url=self.account_url,credential=ADefaultAzureCredential(),)returnBlobServiceClient.from_connection_string(self.connection_string.get_secret_value())@_raise_help_msg("blob_storage")defget_blob_client(self,container,blob)->"BlobClient":""" Returns an authenticated Blob client that can be used to download and upload blobs. Args: container: Name of the Blob Storage container to retrieve from. blob: Name of the blob within this container to retrieve. Example: Create an authorized Blob session ```python import os import asyncio from prefect import flow from prefect_azure import AzureBlobStorageCredentials @flow async def example_get_blob_client_flow(): connection_string = os.getenv("AZURE_STORAGE_CONNECTION_STRING") azure_credentials = AzureBlobStorageCredentials( connection_string=connection_string, ) async with azure_credentials.get_blob_client( "container", "blob" ) as blob_client: # run other code here pass asyncio.run(example_get_blob_client_flow()) ``` """ifself.connection_stringisNone:returnBlobClient(account_url=self.account_url,container_name=container,credential=ADefaultAzureCredential(),blob_name=blob,)blob_client=BlobClient.from_connection_string(self.connection_string.get_secret_value(),container,blob)returnblob_client@_raise_help_msg("blob_storage")defget_container_client(self,container)->"ContainerClient":""" Returns an authenticated Container client that can be used to create clients for Azure services. Args: container: Name of the Blob Storage container to retrieve from. Example: Create an authorized Container session ```python import os import asyncio from prefect import flow from prefect_azure import AzureBlobStorageCredentials @flow async def example_get_container_client_flow(): connection_string = os.getenv("AZURE_STORAGE_CONNECTION_STRING") azure_credentials = AzureBlobStorageCredentials( connection_string=connection_string, ) async with azure_credentials.get_container_client( "container" ) as container_client: # run other code here pass asyncio.run(example_get_container_client_flow()) ``` """ifself.connection_stringisNone:returnContainerClient(account_url=self.account_url,container_name=container,credential=ADefaultAzureCredential(),)container_client=ContainerClient.from_connection_string(self.connection_string.get_secret_value(),container)returncontainer_client
@root_validatordefcheck_connection_string_or_account_url(cls,values:Dict[str,Any])->Dict[str,Any]:""" Checks that either a connection string or account URL is provided, not both. """has_account_url=values.get("account_url")isnotNonehas_conn_str=values.get("connection_string")isnotNoneifnothas_account_urlandnothas_conn_str:raiseValueError("Must provide either a connection string or an account URL.")ifhas_account_urlandhas_conn_str:raiseValueError("Must provide either a connection string or account URL, but not both.")returnvalues
Returns an authenticated Blob client that can be used to
download and upload blobs.
Parameters:
Name
Type
Description
Default
container
Name of the Blob Storage container to retrieve from.
required
blob
Name of the blob within this container to retrieve.
required
Example
Create an authorized Blob session
importosimportasynciofromprefectimportflowfromprefect_azureimportAzureBlobStorageCredentials@flowasyncdefexample_get_blob_client_flow():connection_string=os.getenv("AZURE_STORAGE_CONNECTION_STRING")azure_credentials=AzureBlobStorageCredentials(connection_string=connection_string,)asyncwithazure_credentials.get_blob_client("container","blob")asblob_client:# run other code herepassasyncio.run(example_get_blob_client_flow())
@_raise_help_msg("blob_storage")defget_blob_client(self,container,blob)->"BlobClient":""" Returns an authenticated Blob client that can be used to download and upload blobs. Args: container: Name of the Blob Storage container to retrieve from. blob: Name of the blob within this container to retrieve. Example: Create an authorized Blob session ```python import os import asyncio from prefect import flow from prefect_azure import AzureBlobStorageCredentials @flow async def example_get_blob_client_flow(): connection_string = os.getenv("AZURE_STORAGE_CONNECTION_STRING") azure_credentials = AzureBlobStorageCredentials( connection_string=connection_string, ) async with azure_credentials.get_blob_client( "container", "blob" ) as blob_client: # run other code here pass asyncio.run(example_get_blob_client_flow()) ``` """ifself.connection_stringisNone:returnBlobClient(account_url=self.account_url,container_name=container,credential=ADefaultAzureCredential(),blob_name=blob,)blob_client=BlobClient.from_connection_string(self.connection_string.get_secret_value(),container,blob)returnblob_client
Returns an authenticated base Blob Service client that can be used to create
other clients for Azure services.
Example
Create an authorized Blob Service session
importosimportasynciofromprefectimportflowfromprefect_azureimportAzureBlobStorageCredentials@flowasyncdefexample_get_client_flow():connection_string=os.getenv("AZURE_STORAGE_CONNECTION_STRING")azure_credentials=AzureBlobStorageCredentials(connection_string=connection_string,)asyncwithazure_credentials.get_client()asblob_service_client:# run other code herepassasyncio.run(example_get_client_flow())
@_raise_help_msg("blob_storage")defget_client(self)->"BlobServiceClient":""" Returns an authenticated base Blob Service client that can be used to create other clients for Azure services. Example: Create an authorized Blob Service session ```python import os import asyncio from prefect import flow from prefect_azure import AzureBlobStorageCredentials @flow async def example_get_client_flow(): connection_string = os.getenv("AZURE_STORAGE_CONNECTION_STRING") azure_credentials = AzureBlobStorageCredentials( connection_string=connection_string, ) async with azure_credentials.get_client() as blob_service_client: # run other code here pass asyncio.run(example_get_client_flow()) ``` """ifself.connection_stringisNone:returnBlobServiceClient(account_url=self.account_url,credential=ADefaultAzureCredential(),)returnBlobServiceClient.from_connection_string(self.connection_string.get_secret_value())
Returns an authenticated Container client that can be used to create clients
for Azure services.
Parameters:
Name
Type
Description
Default
container
Name of the Blob Storage container to retrieve from.
required
Example
Create an authorized Container session
importosimportasynciofromprefectimportflowfromprefect_azureimportAzureBlobStorageCredentials@flowasyncdefexample_get_container_client_flow():connection_string=os.getenv("AZURE_STORAGE_CONNECTION_STRING")azure_credentials=AzureBlobStorageCredentials(connection_string=connection_string,)asyncwithazure_credentials.get_container_client("container")ascontainer_client:# run other code herepassasyncio.run(example_get_container_client_flow())
@_raise_help_msg("blob_storage")defget_container_client(self,container)->"ContainerClient":""" Returns an authenticated Container client that can be used to create clients for Azure services. Args: container: Name of the Blob Storage container to retrieve from. Example: Create an authorized Container session ```python import os import asyncio from prefect import flow from prefect_azure import AzureBlobStorageCredentials @flow async def example_get_container_client_flow(): connection_string = os.getenv("AZURE_STORAGE_CONNECTION_STRING") azure_credentials = AzureBlobStorageCredentials( connection_string=connection_string, ) async with azure_credentials.get_container_client( "container" ) as container_client: # run other code here pass asyncio.run(example_get_container_client_flow()) ``` """ifself.connection_stringisNone:returnContainerClient(account_url=self.account_url,container_name=container,credential=ADefaultAzureCredential(),)container_client=ContainerClient.from_connection_string(self.connection_string.get_secret_value(),container)returncontainer_client
classAzureContainerInstanceCredentials(Block):""" Block used to manage Azure Container Instances authentication. Stores Azure Service Principal authentication data. """_block_type_name="Azure Container Instance Credentials"_logo_url="https://cdn.sanity.io/images/3ugk85nk/production/54e3fa7e00197a4fbd1d82ed62494cb58d08c96a-250x250.png"# noqa_documentation_url="https://prefecthq.github.io/prefect-azure/credentials/#prefect_azure.credentials.AzureContainerInstanceCredentials"# noqaclient_id:Optional[str]=Field(default=None,title="Client ID",description=("The service principal client ID. ""If none of client_id, tenant_id, and client_secret are provided, ""will use DefaultAzureCredential; else will need to provide all three to ""use ClientSecretCredential."),)tenant_id:Optional[str]=Field(default=None,title="Tenant ID",description=("The service principal tenant ID.""If none of client_id, tenant_id, and client_secret are provided, ""will use DefaultAzureCredential; else will need to provide all three to ""use ClientSecretCredential."),)client_secret:Optional[SecretStr]=Field(default=None,description=("The service principal client secret.""If none of client_id, tenant_id, and client_secret are provided, ""will use DefaultAzureCredential; else will need to provide all three to ""use ClientSecretCredential."),)credential_kwargs:Dict[str,Any]=Field(default_factory=dict,title="Additional Credential Keyword Arguments",description=("Additional keyword arguments to pass to ""`ClientSecretCredential` or `DefaultAzureCredential`."),)@root_validatordefvalidate_credential_kwargs(cls,values):""" Validates that if any of `client_id`, `tenant_id`, or `client_secret` are provided, all must be provided. """auth_args=("client_id","tenant_id","client_secret")has_any=any(values.get(key)isnotNoneforkeyinauth_args)has_all=all(values.get(key)isnotNoneforkeyinauth_args)ifhas_anyandnothas_all:raiseValueError("If any of `client_id`, `tenant_id`, or `client_secret` are provided, ""all must be provided.")returnvaluesdefget_container_client(self,subscription_id:str):""" Creates an Azure Container Instances client initialized with data from this block's fields and a provided Azure subscription ID. Args: subscription_id: A valid Azure subscription ID. Returns: An initialized `ContainerInstanceManagementClient` """returnContainerInstanceManagementClient(credential=self._create_credential(),subscription_id=subscription_id,)defget_resource_client(self,subscription_id:str):""" Creates an Azure resource management client initialized with data from this block's fields and a provided Azure subscription ID. Args: subscription_id: A valid Azure subscription ID. Returns: An initialized `ResourceManagementClient` """returnResourceManagementClient(credential=self._create_credential(),subscription_id=subscription_id,)def_create_credential(self):""" Creates an Azure credential initialized with data from this block's fields. Returns: An initialized Azure `TokenCredential` ready to use with Azure SDK client classes. """auth_args=(self.client_id,self.tenant_id,self.client_secret)ifauth_args==(None,None,None):returnDefaultAzureCredential(**self.credential_kwargs)returnClientSecretCredential(tenant_id=self.tenant_id,client_id=self.client_id,client_secret=self.client_secret.get_secret_value(),**self.credential_kwargs,)
Creates an Azure Container Instances client initialized with data from
this block's fields and a provided Azure subscription ID.
Parameters:
Name
Type
Description
Default
subscription_id
str
A valid Azure subscription ID.
required
Returns:
Type
Description
An initialized ContainerInstanceManagementClient
Source code in prefect_azure/credentials.py
537538539540541542543544545546547548549550551552
defget_container_client(self,subscription_id:str):""" Creates an Azure Container Instances client initialized with data from this block's fields and a provided Azure subscription ID. Args: subscription_id: A valid Azure subscription ID. Returns: An initialized `ContainerInstanceManagementClient` """returnContainerInstanceManagementClient(credential=self._create_credential(),subscription_id=subscription_id,)
Creates an Azure resource management client initialized with data from
this block's fields and a provided Azure subscription ID.
Parameters:
Name
Type
Description
Default
subscription_id
str
A valid Azure subscription ID.
required
Returns:
Type
Description
An initialized ResourceManagementClient
Source code in prefect_azure/credentials.py
554555556557558559560561562563564565566567568569
defget_resource_client(self,subscription_id:str):""" Creates an Azure resource management client initialized with data from this block's fields and a provided Azure subscription ID. Args: subscription_id: A valid Azure subscription ID. Returns: An initialized `ResourceManagementClient` """returnResourceManagementClient(credential=self._create_credential(),subscription_id=subscription_id,)
Validates that if any of client_id, tenant_id, or client_secret are
provided, all must be provided.
Source code in prefect_azure/credentials.py
521522523524525526527528529530531532533534535
@root_validatordefvalidate_credential_kwargs(cls,values):""" Validates that if any of `client_id`, `tenant_id`, or `client_secret` are provided, all must be provided. """auth_args=("client_id","tenant_id","client_secret")has_any=any(values.get(key)isnotNoneforkeyinauth_args)has_all=all(values.get(key)isnotNoneforkeyinauth_args)ifhas_anyandnothas_all:raiseValueError("If any of `client_id`, `tenant_id`, or `client_secret` are provided, ""all must be provided.")returnvalues
classAzureCosmosDbCredentials(Block):""" Block used to manage Cosmos DB authentication with Azure. Azure authentication is handled via the `azure` module through a connection string. Args: connection_string: Includes the authorization information required. Example: Load stored Azure Cosmos DB credentials: ```python from prefect_azure import AzureCosmosDbCredentials azure_credentials_block = AzureCosmosDbCredentials.load("BLOCK_NAME") ``` """_block_type_name="Azure Cosmos DB Credentials"_logo_url="https://cdn.sanity.io/images/3ugk85nk/production/54e3fa7e00197a4fbd1d82ed62494cb58d08c96a-250x250.png"# noqa_documentation_url="https://prefecthq.github.io/prefect-azure/credentials/#prefect_azure.credentials.AzureCosmosDbCredentials"# noqaconnection_string:SecretStr=Field(default=...,description="Includes the authorization information required.")@_raise_help_msg("cosmos_db")defget_client(self)->"CosmosClient":""" Returns an authenticated Cosmos client that can be used to create other clients for Azure services. Example: Create an authorized Cosmos session ```python import os from prefect import flow from prefect_azure import AzureCosmosDbCredentials @flow def example_get_client_flow(): connection_string = os.getenv("AZURE_COSMOS_CONNECTION_STRING") azure_credentials = AzureCosmosDbCredentials( connection_string=connection_string, ) cosmos_client = azure_credentials.get_client() return cosmos_client example_get_client_flow() ``` """returnCosmosClient.from_connection_string(self.connection_string.get_secret_value())defget_database_client(self,database:str)->"DatabaseProxy":""" Returns an authenticated Database client. Args: database: Name of the database. Example: Create an authorized Cosmos session ```python import os from prefect import flow from prefect_azure import AzureCosmosDbCredentials @flow def example_get_client_flow(): connection_string = os.getenv("AZURE_COSMOS_CONNECTION_STRING") azure_credentials = AzureCosmosDbCredentials( connection_string=connection_string, ) cosmos_client = azure_credentials.get_database_client() return cosmos_client example_get_database_client_flow() ``` """cosmos_client=self.get_client()database_client=cosmos_client.get_database_client(database=database)returndatabase_clientdefget_container_client(self,container:str,database:str)->"ContainerProxy":""" Returns an authenticated Container client used for querying. Args: container: Name of the Cosmos DB container to retrieve from. database: Name of the Cosmos DB database. Example: Create an authorized Container session ```python import os from prefect import flow from prefect_azure import AzureBlobStorageCredentials @flow def example_get_container_client_flow(): connection_string = os.getenv("AZURE_COSMOS_CONNECTION_STRING") azure_credentials = AzureCosmosDbCredentials( connection_string=connection_string, ) container_client = azure_credentials.get_container_client(container) return container_client example_get_container_client_flow() ``` """database_client=self.get_database_client(database)container_client=database_client.get_container_client(container=container)returncontainer_client
@_raise_help_msg("cosmos_db")defget_client(self)->"CosmosClient":""" Returns an authenticated Cosmos client that can be used to create other clients for Azure services. Example: Create an authorized Cosmos session ```python import os from prefect import flow from prefect_azure import AzureCosmosDbCredentials @flow def example_get_client_flow(): connection_string = os.getenv("AZURE_COSMOS_CONNECTION_STRING") azure_credentials = AzureCosmosDbCredentials( connection_string=connection_string, ) cosmos_client = azure_credentials.get_client() return cosmos_client example_get_client_flow() ``` """returnCosmosClient.from_connection_string(self.connection_string.get_secret_value())
defget_container_client(self,container:str,database:str)->"ContainerProxy":""" Returns an authenticated Container client used for querying. Args: container: Name of the Cosmos DB container to retrieve from. database: Name of the Cosmos DB database. Example: Create an authorized Container session ```python import os from prefect import flow from prefect_azure import AzureBlobStorageCredentials @flow def example_get_container_client_flow(): connection_string = os.getenv("AZURE_COSMOS_CONNECTION_STRING") azure_credentials = AzureCosmosDbCredentials( connection_string=connection_string, ) container_client = azure_credentials.get_container_client(container) return container_client example_get_container_client_flow() ``` """database_client=self.get_database_client(database)container_client=database_client.get_container_client(container=container)returncontainer_client
classAzureMlCredentials(Block):""" Block used to manage authentication with AzureML. Azure authentication is handled via the `azure` module. Args: tenant_id: The active directory tenant that the service identity belongs to. service_principal_id: The service principal ID. service_principal_password: The service principal password/key. subscription_id: The Azure subscription ID containing the workspace. resource_group: The resource group containing the workspace. workspace_name: The existing workspace name. Example: Load stored AzureML credentials: ```python from prefect_azure import AzureMlCredentials azure_ml_credentials_block = AzureMlCredentials.load("BLOCK_NAME") ``` """_block_type_name="AzureML Credentials"_logo_url="https://cdn.sanity.io/images/3ugk85nk/production/54e3fa7e00197a4fbd1d82ed62494cb58d08c96a-250x250.png"# noqa_documentation_url="https://prefecthq.github.io/prefect-azure/credentials/#prefect_azure.credentials.AzureMlCredentials"# noqatenant_id:str=Field(default=...,description="The active directory tenant that the service identity belongs to.",)service_principal_id:str=Field(default=...,description="The service principal ID.")service_principal_password:SecretStr=Field(default=...,description="The service principal password/key.")subscription_id:str=Field(default=...,description="The Azure subscription ID containing the workspace, in format: '00000000-0000-0000-0000-000000000000'.",# noqa)resource_group:str=Field(default=...,description="The resource group containing the workspace.")workspace_name:str=Field(default=...,description="The existing workspace name.")@_raise_help_msg("ml_datastore")defget_workspace(self)->"Workspace":""" Returns an authenticated base Workspace that can be used in Azure's Datasets and Datastores. Example: Create an authorized workspace ```python import os from prefect import flow from prefect_azure import AzureMlCredentials @flow def example_get_workspace_flow(): azure_credentials = AzureMlCredentials( tenant_id="tenant_id", service_principal_id="service_principal_id", service_principal_password="service_principal_password", subscription_id="subscription_id", resource_group="resource_group", workspace_name="workspace_name" ) workspace_client = azure_credentials.get_workspace() return workspace_client example_get_workspace_flow() ``` """service_principal_password=self.service_principal_password.get_secret_value()service_principal_authentication=ServicePrincipalAuthentication(tenant_id=self.tenant_id,service_principal_id=self.service_principal_id,service_principal_password=service_principal_password,)workspace=Workspace(subscription_id=self.subscription_id,resource_group=self.resource_group,workspace_name=self.workspace_name,auth=service_principal_authentication,)returnworkspace
@_raise_help_msg("ml_datastore")defget_workspace(self)->"Workspace":""" Returns an authenticated base Workspace that can be used in Azure's Datasets and Datastores. Example: Create an authorized workspace ```python import os from prefect import flow from prefect_azure import AzureMlCredentials @flow def example_get_workspace_flow(): azure_credentials = AzureMlCredentials( tenant_id="tenant_id", service_principal_id="service_principal_id", service_principal_password="service_principal_password", subscription_id="subscription_id", resource_group="resource_group", workspace_name="workspace_name" ) workspace_client = azure_credentials.get_workspace() return workspace_client example_get_workspace_flow() ``` """service_principal_password=self.service_principal_password.get_secret_value()service_principal_authentication=ServicePrincipalAuthentication(tenant_id=self.tenant_id,service_principal_id=self.service_principal_id,service_principal_password=service_principal_password,)workspace=Workspace(subscription_id=self.subscription_id,resource_group=self.resource_group,workspace_name=self.workspace_name,auth=service_principal_authentication,)returnworkspace