Huawei 云 SDK 使用

环境信息

  • Python 3.10
  • huaweicloudsdk 3.1.23

常用方法

华为云 Python SDK 安装方法官网说明

ECS

获取云服务器 (ECS) 实例信息

通过华为云提供的 Python SDK 获取云服务器实例信息 [1]

python
>>> from huaweicloudsdkcore.auth.credentials import BasicCredentials
>>> from huaweicloudsdkecs.v2.region.ecs_region import EcsRegion
>>> from huaweicloudsdkcore.exceptions import exceptions
>>> from huaweicloudsdkecs.v2 import *

>>> ak = 'QONOKCeljdngdsEGSAM'
>>> sk = 'uD4ndlLqP5xDGSrgRRRPJGF8hg^HGkGl2'

>>> credentials = BasicCredentials(ak, sk)

>>> client = EcsClient.new_builder().with_credentials(credentials).with_region(EcsRegion.value_of("cn-east-2")).build()

>>> request = ListServersDetailsRequest()
>>> response = client.list_servers_details(request)


response 返回一个 huaweicloudsdkecs.v2.model.list_servers_details_response.ListServersDetailsResponse 对象,其中包含了服务器数量和服务器详情列表,要转换为 Python 字典对象,可以通过以下方法

python
>>> import json

>>> server_info = json.loads(str(response))

>>> type(server_info)
<class 'dict'>

### 获取查询出的机器数量
>>> server_info['count']
2

### 云服务器列表 list[dict]
>>> server_info['servers']

### 获取云服务器的名称及 id
>>> server_info['servers'][0]['name']
>>> server_info['servers'][0]['id']

### 获取云服务器的配置规格信息
>>> server_info['servers'][0]['flavor']
{'id': 'c3.xlarge.2', 'name': 'c3.xlarge.2', 'disk': '0', 'vcpus': '4', 'ram': '8192'}

### 获取云服务器挂载的磁盘id
>>> server_info['servers'][0]['os-extended-volumes:volumes_attached']
[{'id': '1fd566f2-82bb-49d3-95d1-7452a5868f80', 'delete_on_termination': 'false', 'device': '/dev/vdb'}, {'id': '2deefa5c-e92b-4ec5-9914-10af9919c121', 'delete_on_termination': 'false', 'bootIndex': '0', 'device': '/dev/vda'}]

### 获取云服务器 IP 信息
### 首先在 'metadata' 中获取云服务器的 VPC id 信息,根据 VPC id 获取对应的 ip 信息
>>> server_info['servers'][0]['metadata']
{'lockScene': '', 'charging_mode': '1', 'vpc_id': '6827d6f5-4614-42a5-9762-15d3d173411c', 'metering.product_id': '00301-15033-0--0', 'lockSource': '', 'lockSourceId': '', 'metering.imagetype': 'gold', 'metering.order_id': 'CS23010203413KRGB', 'image_name': 'Windows Server 2016 Standard 64bit', 'metering.resourcespeccode': 'c3.xlarge.2.win', 'os_type': 'Windows', 'metering.resourcetype': '1', 'metering.image_id': 'dd714f2c-f470-43b6-afc8-34d8ec689516', 'os_bit': '64', 'EcmResStatus': '', 'lockCheckEndpoint': '', 'cascaded.instance_extrainfo': 'clock_adjustment:28800,pcibridge:1'}

>>> server_info['servers'][0]['metadata']['vpc_id']
'6827d6f5-4614-42a5-9762-15d3d173411c'

### 其中 {'OS-EXT-IPS:type': 'floating'} 表示公网 IP
>>> server_info['servers'][0]['addresses']['6827d6f5-4614-42a5-9762-15d3d173411c']
[{'version': '4', 'addr': '192.168.0.65', 'OS-EXT-IPS:type': 'fixed', 'OS-EXT-IPS-MAC:mac_addr': 'fa:16:3e:00:76:2a', 'OS-EXT-IPS:port_id': 'c1ec2783-6d3c-4d8a-9c55-abb59c2c648d'}, {'version': '4', 'addr': '159.138.38.203', 'OS-EXT-IPS:type': 'floating', 'OS-EXT-IPS-MAC:mac_addr': 'fa:16:3e:00:76:2a', 'OS-EXT-IPS:port_id': 'c1ec2783-6d3c-4d8a-9c55-abb59c2c648d'}]

根据云服务器 id,查询云服务器挂载的磁盘信息 [2]

python
>>> request = ListServerBlockDevicesRequest(server_id='a7dd4502-cb3e-4754-b7b7-73bcb5696a1c')

>>> response = client.list_server_block_devices(request)

>>> response
{"attachableQuantity": {"free_scsi": 58, "free_blk": 22, "free_disk": 58}, \
"volumeAttachments": [{"bootIndex": 0, "pciAddress": "0000:02:01.0", "volumeId": "2deefa5c-e92b-4ec5-9914-10af9919c121", "device": "/dev/vda", "serverId": "a7dd4502-cb3e-4754-b7b7-73bcb5696a1c", \
"id": "2deefa5c-e92b-4ec5-9914-10af9919c121", "size": 100, "bus": "virtio"}, \
{"pciAddress": "0000:02:02.0", "volumeId": "1fd566f2-82bb-49d3-95d1-7452a5868f80", \
"device": "/dev/vdb", "serverId": "a7dd4502-cb3e-4754-b7b7-73bcb5696a1c", "id": "1fd566f2-82bb-49d3-95d1-7452a5868f80", "size": 100, "bus": "virtio"}]}

CDN

CDN 数据统计

获取 CDN 域名统计数据 [3]

python
>>> from huaweicloudsdkcore.auth.credentials import GlobalCredentials
>>> from huaweicloudsdkcdn.v1.region.cdn_region import CdnRegion
>>> from huaweicloudsdkcore.exceptions import exceptions
>>> from huaweicloudsdkcdn.v1 import *

>>> ak = ""
>>> sk = ""

>>> credentials = GlobalCredentials(ak, sk)
>>> client = CdnClient.new_builder() \
... .with_credentials(credentials) \
... .with_region(CdnRegion.value_of("cn-north-1")) \
... .build()

>>> request = ListDomainsRequest()
>>> response = client.list_domains(request)

# response 数据转换为 Python 数据格式
>>> response = json.loads(str(response))

输出中包含已经配置在 CDN 上面的域名列表。

查询 CDN 加速域名的流量 [4]

python
request = ShowDomainLocationStatsRequest(action='location_summary', interval=86400, \
start_time=1669852800000, end_time=1672531200000, \
domain_name="test.a.com", stat_type="flux")
>>> response = client.show_domain_location_stats(request)
>>> response
{"result": {"flux": 1050601}}

添加 CDN 域名

代码示例。参考文档

credentials = GlobalCredentials(ak, sk)
client = CdnClient.new_builder().with_credentials(credentials).with_region(CdnRegion.value_of("cn-north-1")).build()

domain_name = "test1.test.com"
business_type = "web"
service_area = "global"
origin_type = "ipaddr"
ip_or_domain = "11.33.5.76"

sources=[Sources(origin_type=origin_type, ip_or_domain=ip_or_domain, active_standby=1)]
domain = DomainBody(domain_name=domain_name, business_type=business_type, service_area=service_area, sources=sources)


request = CreateDomainRequest()
request.body = CreateDomainRequestBody(domain=domain)
response = client.create_domain(request)

EIP

获取 EIP 信息及统计 IP 流量

本示例演示获取到指定账号和区域的所有 EIP 信息,并根据 EIP ID 统计对应 IP 的公网出口流量。[5]

python
>>> from huaweicloudsdkcore.auth.credentials import BasicCredentials
>>> from huaweicloudsdkeip.v2.region.eip_region import EipRegion
>>> from huaweicloudsdkcore.exceptions import exceptions
>>> from huaweicloudsdkeip.v2 import *
>>> import json

>>> zone = 'ap-southeast-1'
>>> credentials = BasicCredentials(ak, sk)
>>> client = EipClient.new_builder() \
... .with_credentials(credentials) \
... .with_region(EipRegion.value_of(zone)) \
... .build()

>>> request = ListPublicipsRequest()
>>> response = client.list_publicips(request)

>>> r = json.loads(str(response))

输出中包含了此区域内的所有 EIP 信息,查询 IP 的流量统计信息需要 EIP ID,以下输出 EIP ID

python
>>> r['publicips'][0]['id']
'3fda9feb-b8e3-440d-8fda-16b5728a6e1f'

根据 EIP ID 信息获取 IP 公网流量统计数据,ShowMetricDataRequest 方法的帮助信息可以通过 help(ShowMetricDataRequest) 查看 [6]

python
>>> from huaweicloudsdkcore.auth.credentials import BasicCredentials
>>> from huaweicloudsdkces.v1.region.ces_region import CesRegion
>>> from huaweicloudsdkcore.exceptions import exceptions
>>> from huaweicloudsdkces.v1 import *

>>> name_space = "SYS.VPC"
>>> metric_name = "up_stream"
>>> dim_01 = "publicip_id,f0304346-2388-4c9b-a9d0-6f321238f2be"

>>> credentials = BasicCredentials(ak, sk)
>>>
>>> client = CesClient.new_builder() \
... .with_credentials(credentials) \
... .with_region(CesRegion.value_of(zone)) \
... .build()
>>> request = ShowMetricDataRequest()
>>> request.namespace = name_space
>>> request.metric_name = metric_name

>>> request.dim_0 = dim_01
>>> request.filter = 'sum'
>>> request.period = 86400

>>> request._from = start_timestamp
>>> request.to = end_timestamp
>>> response = client.show_metric_data(request)
>>> r = json.loads(str(response))

输出中包含了按天分割的流量数据

python
>>> r
{'datapoints': [{'sum': 4768802.0, 'timestamp': 1672531200000, 'unit': 'Byte'}, {'sum': 1331712163.0, 'timestamp': 1672617600000, 'unit': 'Byte'}, {'sum': 12761506.0, 'timestamp': 1672704000000, 'unit': 'Byte'}, {'sum': 17119425.0, 'timestamp': 1672790400000, 'unit': 'Byte'}, {'sum': 16735886.0, 'timestamp': 1672876800000, 'unit': 'Byte'}, {'sum': 186881526.0, 'timestamp': 1672963200000, 'unit': 'Byte'}, {'sum': 22091616.0, 'timestamp': 1673049600000, 'unit': 'Byte'}, {'sum': 9926109.0, 'timestamp': 1673136000000, 'unit': 'Byte'}, {'sum': 14668113.0, 'timestamp': 1673222400000, 'unit': 'Byte'}, {'sum': 4655251.0, 'timestamp': 1673308800000, 'unit': 'Byte'}, {'sum': 151734721.0, 'timestamp': 1673395200000, 'unit': 'Byte'}, {'sum': 65481126.0, 'timestamp': 1673481600000, 'unit': 'Byte'}, {'sum': 1236561691.0, 'timestamp': 1673568000000, 'unit': 'Byte'}, {'sum': 47856976.0, 'timestamp': 1673654400000, 'unit': 'Byte'}, {'sum': 1352841.0, 'timestamp': 1673740800000, 'unit': 'Byte'}, {'sum': 265917499.0, 'timestamp': 1673827200000, 'unit': 'Byte'}, {'sum': 35810298.0, 'timestamp': 1673913600000, 'unit': 'Byte'}, {'sum': 23425333.0, 'timestamp': 1674000000000, 'unit': 'Byte'}, {'sum': 20177091.0, 'timestamp': 1674086400000, 'unit': 'Byte'}, {'sum': 18610343.0, 'timestamp': 1674172800000, 'unit': 'Byte'}, {'sum': 8464183.0, 'timestamp': 1674259200000, 'unit': 'Byte'}, {'sum': 4749783.0, 'timestamp': 1674345600000, 'unit': 'Byte'}, {'sum': 12750334.0, 'timestamp': 1674432000000, 'unit': 'Byte'}, {'sum': 12662461.0, 'timestamp': 1674518400000, 'unit': 'Byte'}, {'sum': 15664751.0, 'timestamp': 1674604800000, 'unit': 'Byte'}, {'sum': 16539739.0, 'timestamp': 1674691200000, 'unit': 'Byte'}, {'sum': 27175842.0, 'timestamp': 1674777600000, 'unit': 'Byte'}, {'sum': 784220244.0, 'timestamp': 1674864000000, 'unit': 'Byte'}, {'sum': 23921455.0, 'timestamp': 1674950400000, 'unit': 'Byte'}, {'sum': 30317158.0, 'timestamp': 1675036800000, 'unit': 'Byte'}, {'sum': 141162735.0, 'timestamp': 1675123200000, 'unit': 'Byte'}, {'sum': 309507524.0, 'timestamp': 1675209600000, 'unit': 'Byte'}], 'metric_name': 'up_stream'}

脚注