正在循环访问Azure ItemPaged对象

eufgjt7s  于 2023-02-09  发布在  其他
关注(0)|答案(1)|浏览(79)

我调用list操作来检索blob存储的元数据值。

blob_service_list = storage_client.blob_services.list('rg-exercise1', 'sa36730')
for items in blob_service_list:
    print((items.as_dict()))

在这种情况下,返回的输出仅包含具有对应Azure对象的项:

{'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}}

Where-as,如果我做一个简单的项目打印,输出要大得多:

{'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值都已从示例代码中删除。如何扩展示例代码以包含None字段并将最终输出作为列表?

a1o7rhls

a1o7rhls1#

    • 我在我的环境中尝试,得到以下结果:**

如果需要在字典中包含**None**值,可以按照以下代码操作:

    • 代码:**
from azure.mgmt.storage import StorageManagementClient
from azure.identity import DefaultAzureCredential

storage_client=StorageManagementClient(credential=DefaultAzureCredential(),subscription_id="<your sub id>") 
blob_service_list = storage_client.blob_services.list('v-venkat-rg', 'venkat123')
for items in blob_service_list:
      items_dict = items.as_dict()
for key, value in items.__dict__.items():
        if value is None:
            items_dict[key] = value
            print(items_dict)
    • 控制台:**

上述代码使用**None**值成功执行。

相关问题