Skip to content

openavmkit.cloud.azure

AzureCredentials

AzureCredentials(connection_string)

Bases: CloudCredentials

Authentication credentials for Azure

Initialize AzureCredentials object

Parameters:

Name Type Description Default
connection_string str

Your Azure connection string

required
Source code in openavmkit/cloud/azure.py
15
16
17
18
19
20
21
22
23
24
def __init__(self, connection_string: str):
    """Initialize AzureCredentials object

    Parameters
    ----------
    connection_string : str
        Your Azure connection string
    """
    super().__init__()
    self.connection_string = connection_string

AzureService

AzureService(credentials, container_name, access)

Bases: CloudService

Azure-specific CloudService object.

Attributes:

Name Type Description
connection_string str

Your Azure connection string

blob_service_client BlobServiceClient

Azure Blob Service Client

container_client ContainerClient

Azure Container Client

Initialize AzureService object

Attributes:

Name Type Description
credentials AzureCredentials

Authentication credentials for Azure

container_name str

The name of your Azure container

access CloudAccess

What kind of access/permission ("read_only", "read_write")

Source code in openavmkit/cloud/azure.py
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
def __init__(
    self, credentials: AzureCredentials, container_name: str, access: CloudAccess
):
    """Initialize AzureService object

    Attributes
    ----------
    credentials : AzureCredentials
        Authentication credentials for Azure
    container_name : str
        The name of your Azure container
    access : CloudAccess
        What kind of access/permission ("read_only", "read_write")
    """
    super().__init__("azure", credentials, access)
    self.connection_string = credentials.connection_string
    self.blob_service_client = BlobServiceClient.from_connection_string(
        credentials.connection_string
    )
    self.container_client = self.blob_service_client.get_container_client(
        container_name
    )

download_file

download_file(remote_file, local_file_path)

Download a remote file from the Azure service

Parameters:

Name Type Description Default
remote_file CloudFile

The file to download

required
local_file_path str

The path on your local computer you want to save the remote file to

required
Source code in openavmkit/cloud/azure.py
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
def download_file(self, remote_file: CloudFile, local_file_path: str):
    """Download a remote file from the Azure service

    Parameters
    ----------
    remote_file : CloudFile
        The file to download
    local_file_path : str
        The path on your local computer you want to save the remote file to
    """
    super().download_file(remote_file, local_file_path)
    blob_client = self.container_client.get_blob_client(remote_file.name)
    with open(local_file_path, "wb") as f:
        download_stream = blob_client.download_blob()
        f.write(download_stream.readall())

list_files

list_files(remote_path)

List all the files at the given path on Azure

Parameters:

Name Type Description Default
remote_path str

Path on Azure you want to query

required

Returns:

Type Description
list[CloudFile]

A listing of all the files contained within the queried path on the remote Azure service

Source code in openavmkit/cloud/azure.py
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
def list_files(self, remote_path: str) -> list[CloudFile]:
    """List all the files at the given path on Azure

    Parameters
    ----------
    remote_path : str
        Path on Azure you want to query

    Returns
    -------
    list[CloudFile]
        A listing of all the files contained within the queried path on the remote Azure service
    """
    blob_list = self.container_client.list_blobs(name_starts_with=remote_path)
    return [
        CloudFile(
            name=blob.name, last_modified_utc=blob.last_modified, size=blob.size
        )
        for blob in blob_list
    ]

upload_file

upload_file(remote_file_path, local_file_path)

Upload a local file to the Azure service

Parameters:

Name Type Description Default
remote_file_path str

The remote path on the Azure service you want to upload your local file to

required
local_file_path str

The local path to the file on your local computer that you want to upload

required
Source code in openavmkit/cloud/azure.py
100
101
102
103
104
105
106
107
108
109
110
111
112
113
def upload_file(self, remote_file_path: str, local_file_path: str):
    """Upload a local file to the Azure service

    Parameters
    ----------
    remote_file_path : str
        The remote path on the Azure service you want to upload your local file to
    local_file_path : str
        The local path to the file on your local computer that you want to upload
    """
    super().upload_file(remote_file_path, local_file_path)
    blob_client = self.container_client.get_blob_client(remote_file_path)
    with open(local_file_path, "rb") as f:
        blob_client.upload_blob(f, overwrite=True)

get_creds_from_env_azure

get_creds_from_env_azure()

Reads and returns Azure credentials from the environment settings

Returns:

Type Description
AzureCredentials

The credentials for Azure stored in environment settings

Source code in openavmkit/cloud/azure.py
138
139
140
141
142
143
144
145
146
147
148
149
def get_creds_from_env_azure() -> AzureCredentials:
    """Reads and returns Azure credentials from the environment settings

    Returns
    -------
    AzureCredentials
        The credentials for Azure stored in environment settings
    """
    connection_string = os.getenv("AZURE_STORAGE_CONNECTION_STRING")
    if not connection_string:
        raise ValueError("Missing Azure connection string in environment.")
    return AzureCredentials(connection_string)

init_service_azure

init_service_azure(credentials, access)

Initializes the Azure service

Parameters:

Name Type Description Default
credentials AzureCredentials

The credentials to your Azure account

required
access CloudAccess

What kind of access/permission ("read_only", "read_write")

required
Source code in openavmkit/cloud/azure.py
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
def init_service_azure(
    credentials: AzureCredentials, access: CloudAccess
) -> AzureService:
    """Initializes the Azure service

    Parameters
    ----------
    credentials : AzureCredentials
        The credentials to your Azure account
    access : CloudAccess
        What kind of access/permission ("read_only", "read_write")
    """
    container_name = os.getenv("AZURE_STORAGE_CONTAINER_NAME")
    if container_name is None:
        raise ValueError("Missing 'AZURE_STORAGE_CONTAINER_NAME' in environment.")
    if isinstance(credentials, AzureCredentials):
        service = AzureService(credentials, container_name, access)
    else:
        raise ValueError("Invalid credentials for Azure service.")
    return service