Skip to content

prefect_azure.deployments.steps

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:

prefect_version: ...
name: ...

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 }}"

pull:
    - prefect_azure.deployments.steps.pull_from_azure_blob_storage:
        requires: prefect-azure[blob_storage]
        container: "{{ container }}"
        folder: "{{ folder }}"
        credentials: "{{ prefect.blocks.azure-blob-storage-credentials.dev-credentials }}"

Note

Azure Storage account needs to have Hierarchical Namespace disabled.

For more information about using deployment steps, check out out the Prefect docs.

pull_from_azure_blob_storage

Pulls from an Azure Blob Storage container.

Parameters:

Name Type Description Default
container str

The name of the container to pull files from

required
folder str

The folder within the container to pull from

required
credentials Dict[str, str]

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:

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:

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/

Source code in prefect_azure/deployments/steps.py
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
def pull_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/
        ```
    """  # noqa
    local_path = Path.cwd()
    if credentials.get("connection_string") is not None:
        container_client = ContainerClient.from_connection_string(
            credentials["connection_string"], container_name=container
        )
    elif credentials.get("account_url") is not None:
        container_client = ContainerClient(
            account_url=credentials["account_url"],
            container_name=container,
            credential=DefaultAzureCredential(),
        )
    else:
        raise ValueError(
            "Credentials must contain either connection_string or account_url"
        )

    with container_client as client:
        for blob in client.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)
            with open(target, "wb") as f:
                client.download_blob(blob).readinto(f)

    return {
        "container": container,
        "folder": folder,
        "directory": local_path,
    }

push_to_azure_blob_storage

Pushes to an Azure Blob Storage container.

Parameters:

Name Type Description Default
container str

The name of the container to push files to

required
folder str

The folder within the container to push to

required
credentials Dict[str, str]

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:

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:

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/

Source code in prefect_azure/deployments/steps.py
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
def push_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/
        ```
    """  # noqa
    local_path = Path.cwd()
    if credentials.get("connection_string") is not None:
        container_client = ContainerClient.from_connection_string(
            credentials["connection_string"], container_name=container
        )
    elif credentials.get("account_url") is not None:
        container_client = ContainerClient(
            account_url=credentials["account_url"],
            container_name=container,
            credential=DefaultAzureCredential(),
        )
    else:
        raise ValueError(
            "Credentials must contain either connection_string or account_url"
        )

    included_files = None
    if ignore_file and Path(ignore_file).exists():
        with open(ignore_file, "r") as f:
            ignore_patterns = f.readlines()

        included_files = filter_files(str(local_path), ignore_patterns)

    with container_client as client:
        for local_file_path in local_path.expanduser().rglob("*"):
            if (
                included_files is not None
                and str(local_file_path.relative_to(local_path)) not in included_files
            ):
                continue
            elif not local_file_path.is_dir():
                remote_file_path = Path(folder) / local_file_path.relative_to(
                    local_path
                )
                with open(local_file_path, "rb") as f:
                    client.upload_blob(str(remote_file_path), f, overwrite=True)

    return {
        "container": container,
        "folder": folder,
    }