如何遍历Azure ItemPaged对象并保留None字段输出为列表?
问题:保留Azure Blob服务元数据的None字段并整理为列表
我用以下代码调用list操作检索Blob存储的元数据:
blob_service_list = storage_client.blob_services.list('rg-exercise1', 'sa36730') for items in blob_service_list: print((items.as_dict()))
当前返回结果只包含有对应Azure对象的字段,值为None的字段被移除:
{'id': '/subscriptions/0601ba03-2e68-461a-a239-98cxxxxxx/resourceGroups/rg-exercise1/providers/Microsoft.Storage/storageAccounts/sa36730/blobServices/default', 'name': 'default', 'type': 'Microsoft.Storage/storageAccounts/blobServices', 'sku': {'name': 'Standard_LRS', 'tier': 'Standard'}, 'cors': {'cors_rules': [{'allowed_origins': ['www.xyz.com'], 'allowed_methods': ['GET'], 'max_age_in_seconds': 0, 'exposed_headers': [''], 'allowed_headers': ['']}]}, 'delete_retention_policy': {'enabled': False}}
但直接打印items时,输出包含所有字段(包括值为None的):
{'additional_properties': {}, 'id': '/subscriptions/0601ba03-2e68-461a-a239-98c1xxxxx/resourceGroups/rg-exercise1/providers/Microsoft.Storage/storageAccounts/sa36730/blobServices/default', 'name': 'default', 'type': 'Microsoft.Storage/storageAccounts/blobServices', 'sku': <azure.mgmt.storage.v2021_06_01.models._models_py3.Sku object at 0x7ff2f8f1a520>, 'cors': <azure.mgmt.storage.v2021_06_01.models._models_py3.CorsRules object at 0x7ff2f8f1a640>, 'default_service_version': None, 'delete_retention_policy': <azure.mgmt.storage.v2021_06_01.models._models_py3.DeleteRetentionPolicy object at 0x7ff2f8f1a6d0>, 'is_versioning_enabled': None, 'automatic_snapshot_policy_enabled': None, 'change_feed': None, 'restore_policy': None, 'container_delete_retention_policy': None, 'last_access_time_tracking_policy': None}
请问如何修改代码,保留这些None字段并将最终输出整理为列表?
解决方案
Azure SDK的as_dict()方法默认会过滤掉值为None的字段,要保留这些字段,需要手动遍历模型对象的所有属性,包括那些值为None的,同时将嵌套的模型对象递归转换为字典。
修改后的代码
def model_to_dict(obj): """将Azure SDK模型对象转换为包含所有字段(包括None)的字典""" result = {} # 遍历对象的所有属性,跳过内置属性 for attr_name in dir(obj): if attr_name.startswith('_'): continue attr_value = getattr(obj, attr_name) # 递归处理嵌套的Azure模型对象 if hasattr(attr_value, 'as_dict'): result[attr_name] = model_to_dict(attr_value) else: result[attr_name] = attr_value return result # 初始化存储结果的列表 blob_service_metadata_list = [] blob_service_list = storage_client.blob_services.list('rg-exercise1', 'sa36730') for item in blob_service_list: # 转换为包含所有字段的字典 item_dict = model_to_dict(item) blob_service_metadata_list.append(item_dict) # 可选:打印单个结果 print(item_dict) # 输出最终整理后的列表 print("整理后的元数据列表:") print(blob_service_metadata_list)
代码说明
model_to_dict函数:遍历模型对象的所有非内置属性,将嵌套的Azure模型对象递归转换为字典,确保所有字段(包括值为None的)都被保留。- 列表收集:循环中把每个转换后的字典添加到列表,最终得到完整的元数据列表。
预期输出
输出的列表中每个元素会包含所有字段,示例如下:
[ { 'additional_properties': {}, 'id': '/subscriptions/0601ba03-2e68-461a-a239-98c1xxxxx/resourceGroups/rg-exercise1/providers/Microsoft.Storage/storageAccounts/sa36730/blobServices/default', 'name': 'default', 'type': 'Microsoft.Storage/storageAccounts/blobServices', 'sku': {'name': 'Standard_LRS', 'tier': 'Standard'}, 'cors': {'cors_rules': [{'allowed_origins': ['www.xyz.com'], 'allowed_methods': ['GET'], 'max_age_in_seconds': 0, 'exposed_headers': [''], 'allowed_headers': ['']}]}, 'default_service_version': None, 'delete_retention_policy': {'enabled': False}, 'is_versioning_enabled': None, 'automatic_snapshot_policy_enabled': None, 'change_feed': None, 'restore_policy': None, 'container_delete_retention_policy': None, 'last_access_time_tracking_policy': None } ]
内容的提问来源于stack exchange,提问作者abhinav singh
相关产品推荐
相关产品推荐

