使用Azure SDK v2创建AzureBlobDatastore遇AttributeError问题求助
Azure SDK v2 创建AzureBlobDatastore时AttributeError错误解决
问题场景
尝试使用Azure SDK v2创建AzureBlobDatastore,此前通过SDK v1可正常完成操作。执行以下代码时触发错误:
ml_client = MLClient.from_config(credential=DefaultAzureCredential(), path="./workspace_config_sdk2.json") store = AzureBlobDatastore( name="azureml_sdk2_blob", description="Datastore created with sdkv2", account_name=storage_account_name, container_name=container_name, protocol="wasbs", credentials={ "account_key": "..my_account_key.." }, ) ml_client.create_or_update(store)
错误信息
AttributeError: 'dict' object has no attribute '_to_datastore_rest_object'
错误来自SDK内部代码(azure\ai\ml\entities\_datastore\azure_storage.py):
175 def _to_rest_object(self) -> DatastoreData: 176 blob_ds = RestAzureBlobDatastore( 177 account_name=self.account_name, 178 container_name=self.container_name, --> 179 credentials=self.credentials._to_datastore_rest_object(), 180 endpoint=self.endpoint, 181 protocol=self.protocol, 182 tags=self.tags, 183 description=self.description, 184 ) 185 return DatastoreData(properties=blob_ds)
解决方案
SDK v2中AzureBlobDatastore的credentials参数不能直接传入字典,需要使用SDK提供的凭证配置实体类。针对账号密钥的场景,应使用AccountKeyConfiguration类替代字典。
修改后的代码如下:
from azure.ai.ml.entities import AzureBlobDatastore, AccountKeyConfiguration from azure.ai.ml import MLClient from azure.identity import DefaultAzureCredential ml_client = MLClient.from_config(credential=DefaultAzureCredential(), path="./workspace_config_sdk2.json") store = AzureBlobDatastore( name="azureml_sdk2_blob", description="Datastore created with sdkv2", account_name=storage_account_name, container_name=container_name, protocol="wasbs", credentials=AccountKeyConfiguration(account_key="..my_account_key..") ) ml_client.create_or_update(store)
原因说明
SDK v2的实体类内部需要调用凭证对象的_to_datastore_rest_object方法转换为REST API所需格式,普通字典没有该方法,因此必须传入SDK提供的对应凭证配置类(如AccountKeyConfiguration、SasTokenConfiguration等,根据认证方式选择)。
内容的提问来源于stack exchange,提问作者Timbus Calin
相关产品推荐
相关产品推荐

