遍历 Azure ItemPaged 对象

huangapple go评论74阅读模式
英文:

Iterating through Azure ItemPaged object

问题

以下是翻译的代码部分,只包括你要的内容:

我正在调用`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}}


然而,如果我只是简单地打印 items,输出会更大:

{'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` 的字段都已被移除。如何扩展示例代码以包括这些空字段并将最终输出作为列表?

希望这个翻译对你有帮助。如果你需要进一步的解释或有其他问题,请随时提出。

<details>
<summary>英文:</summary>

I am calling the `list` operation to retrieve the metadata values of a blob storage. 
My code looks like:

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


What&#39;s happening in this case is that the returned output only contains the items which had a corresponding Azure object:

{'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, If I do a simple print of items, the output is much larger: 

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


Any value which is `None` has been removed from my example code. How can I extend my example code to include the None fields and have the final output as a list?


</details>


# 答案1
**得分**: 1

以下是翻译好的部分:

**我在我的环境中尝试并获得以下结果:**

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

**代码:**

```python
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**值的操作。

遍历 Azure ItemPaged 对象

英文:

I tried in my environment and got below results:

If you need to include the None values in the dictionary you can follow the below code:

Code:

from azure.mgmt.storage import StorageManagementClient
from azure.identity import DefaultAzureCredential

storage_client=StorageManagementClient(credential=DefaultAzureCredential(),subscription_id=&quot;&lt;your sub id&gt;&quot;) 
blob_service_list = storage_client.blob_services.list(&#39;v-venkat-rg&#39;, &#39;venkat123&#39;)
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)

Console:

The above code executed with None value successfully.

遍历 Azure ItemPaged 对象

huangapple
  • 本文由 发表于 2023年2月9日 03:28:24
  • 转载请务必保留本文链接:https://go.coder-hub.com/75390823.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定