azure-blob-storageattributeerrorazure-machine-learning-serviceazureml-python-sdk

AttributeError: 'BlobServiceClient' object has no attribute '_to_rest_object'


I was trying to add a blob storage connection to MLClient and it gives me an attribute error.

What I have:

client=MLClient(
    DefaultAzureCredential(), 
    subscription_id=sub_id,
    resource_group_name=group_id,
    workspace_name=workspace
    )

blob_service_client = BlobServiceClient(account_url=account_url,
                                        credential=credential)

client.connections.create_or_update(blob_service_client)

Output:

rest_workspace_connection = workspace_connection._to_rest_object()
AttributeError: 'BlobServiceClient' object has no attribute '_to_rest_object'

NOTE: I checked everything. All the links to the storage and credentials are correct, so it should be working fine.

Is there any way I can make a connection with blob storage without the lobServiceClient class? I believe something is wrong with it.


Solution

  • Is there any way I can make a connection with blob storage without the blobServiceClient class? I believe something is wrong with it.

    You can use the below code that will create connection in azure machine learning using Azure python SDK.

    Code:

    from azure.ai.ml import MLClient
    from azure.ai.ml.entities import AzureBlobStoreConnection,SasTokenConfiguration
    from azure.identity import DefaultAzureCredential
    
    # Replace with your Azure details
    subscription_id = "xxx"
    resource_group = "xxx"
    workspace_name = "xxx"
    
    # Storage details
    connection_name = "my_blob_store"
    account_name = "venkat906"
    container_name = "test"
    url="xxxx"
    
    # Initialize MLClient with DefaultAzureCredential removed
    ml_client = MLClient(
        credential=DefaultAzureCredential(),  # Optionally, you can try removing this to avoid conflicts
        subscription_id=subscription_id,
        resource_group_name=resource_group,
        workspace_name=workspace_name,
    )
    
    # Create AzureBlobStoreConnection entity with AccountKeyConfiguration
    blob_connection = AzureBlobStoreConnection(
        name=connection_name,
        url=url,
        container_name=container_name,
        account_name=account_name,
        credentials=SasTokenConfiguration(sas_token="sv=2022-11-02&ss=bfqt&srt=sco&sp=rwdlacupiytfx&se=2025-01-16T14:36:46Z&st=2025-01-16T06:36:46Z&spr=https&sig=redacted")
    )
    
    # Create or update the connection
    try:
        ml_client.connections.create_or_update(blob_connection)
        print(f"Blob storage connection '{connection_name}' created successfully.")
    except Exception as e:
        print(f"Failed to create connection: {e}")
    
    connection = ml_client.connections.get(name=connection_name)
    print(f"Connection '{connection.name}' created successfully.")
    print(f"Type: {connection.type}")
    print(f"Target URL: {connection.target}") 
    

    Output:

    Blob storage connection 'my_blob_store' created successfully.
    Connection 'my_blob_store' created successfully.
    Type: azure_blob
    Target URL: /subscriptions/cxx/resourceGroups/xx/providers/Microsoft.Storage/storageAccounts/venkat906/blobServices/default
    

    enter image description here

    Portal: enter image description here

    Reference: azure.ai.ml.entities.AzureBlobStoreConnection class | Microsoft Learn