Prefect deployment steps for code storage and retrieval in Azure Blob Storage.
These steps can be used in a prefect.yaml file to define the default
push and pull steps for a group of deployments, or they can be used to
define the push and pull steps for a specific deployment.
Example
Sample prefect.yaml file that is configured to push and pull to and
from an Azure Blob Storage container:
A dictionary of credentials with keys connection_string or
account_url and values of the corresponding connection string or
account url. If both are provided, connection_string will be used.
required
Note
Azure Storage account needs to have Hierarchical Namespace disabled.
Example
Pull from an Azure Blob Storage container using credentials stored in
a block:
defpull_from_azure_blob_storage(container:str,folder:str,credentials:Dict[str,str],):""" Pulls from an Azure Blob Storage container. Args: container: The name of the container to pull files from folder: The folder within the container to pull from credentials: A dictionary of credentials with keys `connection_string` or `account_url` and values of the corresponding connection string or account url. If both are provided, `connection_string` will be used. Note: Azure Storage account needs to have Hierarchical Namespace disabled. Example: Pull from an Azure Blob Storage container using credentials stored in a block: ```yaml pull: - prefect_azure.deployments.steps.pull_from_azure_blob_storage: requires: prefect-azure[blob_storage] container: my-container folder: my-folder credentials: "{{ prefect.blocks.azure-blob-storage-credentials.dev-credentials }}" ``` Pull from an Azure Blob Storage container using an account URL and default credentials: ```yaml pull: - prefect_azure.deployments.steps.pull_from_azure_blob_storage: requires: prefect-azure[blob_storage] container: my-container folder: my-folder credentials: account_url: https://myaccount.blob.core.windows.net/ ``` """# noqalocal_path=Path.cwd()ifcredentials.get("connection_string")isnotNone:container_client=ContainerClient.from_connection_string(credentials["connection_string"],container_name=container)elifcredentials.get("account_url")isnotNone:container_client=ContainerClient(account_url=credentials["account_url"],container_name=container,credential=DefaultAzureCredential(),)else:raiseValueError("Credentials must contain either connection_string or account_url")withcontainer_clientasclient:forblobinclient.list_blobs(name_starts_with=folder):target=PurePosixPath(local_path/relative_path_to_current_platform(blob.name).relative_to(folder))Path.mkdir(Path(target.parent),parents=True,exist_ok=True)withopen(target,"wb")asf:client.download_blob(blob).readinto(f)return{"container":container,"folder":folder,"directory":local_path,}
A dictionary of credentials with keys connection_string or
account_url and values of the corresponding connection string or
account url. If both are provided, connection_string will be used.
required
ignore_file
Optional[str]
The path to a file containing patterns of files to ignore when
pushing to Azure Blob Storage. If not provided, the default .prefectignore
file will be used.
'.prefectignore'
Example
Push to an Azure Blob Storage container using credentials stored in a
block:
defpush_to_azure_blob_storage(container:str,folder:str,credentials:Dict[str,str],ignore_file:Optional[str]=".prefectignore",):""" Pushes to an Azure Blob Storage container. Args: container: The name of the container to push files to folder: The folder within the container to push to credentials: A dictionary of credentials with keys `connection_string` or `account_url` and values of the corresponding connection string or account url. If both are provided, `connection_string` will be used. ignore_file: The path to a file containing patterns of files to ignore when pushing to Azure Blob Storage. If not provided, the default `.prefectignore` file will be used. Example: Push to an Azure Blob Storage container using credentials stored in a block: ```yaml push: - prefect_azure.deployments.steps.push_to_azure_blob_storage: requires: prefect-azure[blob_storage] container: my-container folder: my-folder credentials: "{{ prefect.blocks.azure-blob-storage-credentials.dev-credentials }}" ``` Push to an Azure Blob Storage container using an account URL and default credentials: ```yaml push: - prefect_azure.deployments.steps.push_to_azure_blob_storage: requires: prefect-azure[blob_storage] container: my-container folder: my-folder credentials: account_url: https://myaccount.blob.core.windows.net/ ``` """# noqalocal_path=Path.cwd()ifcredentials.get("connection_string")isnotNone:container_client=ContainerClient.from_connection_string(credentials["connection_string"],container_name=container)elifcredentials.get("account_url")isnotNone:container_client=ContainerClient(account_url=credentials["account_url"],container_name=container,credential=DefaultAzureCredential(),)else:raiseValueError("Credentials must contain either connection_string or account_url")included_files=Noneifignore_fileandPath(ignore_file).exists():withopen(ignore_file,"r")asf:ignore_patterns=f.readlines()included_files=filter_files(str(local_path),ignore_patterns)withcontainer_clientasclient:forlocal_file_pathinlocal_path.expanduser().rglob("*"):if(included_filesisnotNoneandstr(local_file_path.relative_to(local_path))notinincluded_files):continueelifnotlocal_file_path.is_dir():remote_file_path=Path(folder)/local_file_path.relative_to(local_path)withopen(local_file_path,"rb")asf:client.upload_blob(str(remote_file_path),f,overwrite=True)return{"container":container,"folder":folder,}