diff --git a/phdi/cloud/azure.py b/phdi/cloud/azure.py index 55c99ef50b..826893acbe 100644 --- a/phdi/cloud/azure.py +++ b/phdi/cloud/azure.py @@ -219,3 +219,20 @@ def list_objects(self, container_name: str, prefix: str = "") -> List[str]: blob_name_list.append(blob_propreties.name) return blob_name_list + + def blob_exists(self, container_name: str, filename: str) -> bool: + """ + Check if a blob exists within a container given its name and the name of the + container. + + :param container_name: The name of the container to look for the blob in. + :param filename: The name of the blob to check the existence of. + :param prefix: Filter the objects returned to filenames beginning + with this value. + :return: A boolean of true if the file exists and false if it does not. + """ + + container_location = f"{self.storage_account_url}/{container_name}" + container_client = self._get_container_client(container_location) + blob_client = container_client.get_blob_client(filename) + return blob_client.exists() diff --git a/tests/cloud/test_cloud.py b/tests/cloud/test_cloud.py index 8037d65ff1..c5d07caf40 100644 --- a/tests/cloud/test_cloud.py +++ b/tests/cloud/test_cloud.py @@ -453,6 +453,34 @@ def test_azure_list_objects(mock_get_client): assert blob_list == ["blob1", "blob2"] +@mock.patch.object(AzureCloudContainerConnection, "_get_container_client") +def test_azure_blob_exists(mock_get_client): + mocked_blob_client = mock.Mock() + mocked_blob_client.exists.return_value = True + + mocked_container_client = mock.Mock() + mocked_container_client.get_blob_client.return_value = mocked_blob_client + + mock_get_client.return_value = mocked_container_client + + object_storage_account = "some-resource-location" + object_container = "some-container-name" + filename = "some-file-name" + + mock_cred_manager = mock.Mock() + + phdi_container_client = AzureCloudContainerConnection( + object_storage_account, mock_cred_manager + ) + + exists = phdi_container_client.blob_exists(object_container, filename) + + mock_get_client.assert_called_with(f"{object_storage_account}/{object_container}") + mocked_container_client.get_blob_client.assert_called_with(filename) + mocked_blob_client.exists.assert_called() + assert exists is True + + def test_gcp_storage_connect_init(): phdi_container_client = GcpCloudStorageConnection() assert phdi_container_client._GcpCloudStorageConnection__storage_client is None