阿里云的云服务器ECS除了提供控制台来进行日常的管理和资源创建,还提供了OpenApi来进行资源的管理和定制开发。通过OpenApi您可以更加灵活的管理和配置云服务器。
阿里云提供了SDK来包装OpenApi,可以让您将云服务器的管理集成到您的已有系统中。本文以Python的开发来说明OpenApi如何来管理云服务器,即便您没有Python的开发经验,通过本文也可以轻松的0基础入门
进行云服务的开发。其它语言的开发和管理您可以通过留言沟通。
安装ECS Python SDK
首先确保您已经具备Python的Runtime,本文中使用的Python版本为2.7+。
pip install aliyun-python-sdk-ecs
如果提示您没有权限,请切换sudo
继续执行。
sudo pip install aliyun-python-sdk-ecs
本文使用的sdk版本为2.1.2
, 如果您使用是旧版本的sdk,建议你更新下。
Hello Aliyun ECS
我们首先创建一个文件hello_ecs_api.py
. 为了使用SDK,首先实例化 AcsClient
对象,这里需要输入的是您的阿里云在Accesskey和Accesskey Secrect,你可以通过https://ak-console.aliyun.com/ 获取自己的AK。
Access Key ID和Access Key Secret是您访问阿里云API的密钥,具有该账户完全的权限,请您妥善保管。
。
from aliyunsdkcore import client
from aliyunsdkecs.request.v20140526.DescribeInstancesRequest import DescribeInstancesRequest
from aliyunsdkecs.request.v20140526.DescribeRegionsRequest import DescribeRegionsRequest
clt = client.AcsClient('Your Access Key Id', 'Your Access Key Secrect', 'cn-beijing')
完成了实例化之后就可以进行您的第一个应用的开发。做一个简单的查询查询下当前您的账号支持的地域列表。具体的文档参见查询可用地域列表.
def hello_aliyun_regions():
request = DescribeRegionsRequest()
response = _send_request(request)
region_list = response.get('Regions').get('Region')
assert response is not None
assert region_list is not None
result = map(_print_region_id, region_list)
logging.info("region list: %s", result)
def _print_region_id(item):
region_id = item.get("RegionId")
return region_id
def _send_request(request):
request.set_accept_format('json')
try:
response_str = clt.do_action(request)
logging.info(response_str)
response_detail = json.loads(response_str)
return response_detail
except Exception as e:
logging.error(e)
hello_aliyun_regions()
在命令行运行python hello_ecs_api.py
会得到当前的支持的Region列表。类似的输出如下
[u'cn-shenzhen', u'ap-southeast-1', u'cn-qingdao', u'cn-beijing', u'cn-shanghai', u'us-east-1', u'cn-hongkong', u'me-east-1', u'ap-southeast-2', u'cn-hangzhou', u'eu-central-1', u'ap-northeast-1', u'us-west-1']
查询当前的Region下的ECS实例列表
查询实例列表和查询Region列表非常类似,替换入参对象为DescribeInstancesRequest
即可,更多的查询参数参考查询实例列表
def list_instances():
request = DescribeInstancesRequest()
response = _send_request(request)
if response is not None:
instance_list = response.get('Instances').get('Instance')
result = map(_print_instance_id, instance_list)
logging.info("current region include instance %s", result)
def _print_instance_id(item):
instance_id = item.get('InstanceId');
return instance_id
输出结果为如下
current region include instance [u'i-****', u'i-****'']
更多的API参考ECS API 概览,尝试做一个查询磁盘列表。将实例的参数替换为DescribeDisksRequest
。
下一步
完成了上面的任务之后我们下一步将包含新的任务。下面的内容将持续更新,敬请关注:
全部的代码如下
# coding=utf-8
# if the python sdk is not install using 'sudo pip install aliyun-python-sdk-ecs'
# if the python sdk is install using 'sudo pip install --upgrade aliyun-python-sdk-ecs'
# make sure the sdk version is 2.1.2, you can use command 'pip show aliyun-python-sdk-ecs' to check
import json
import logging
from aliyunsdkcore import client
from aliyunsdkecs.request.v20140526.DescribeInstancesRequest import DescribeInstancesRequest
from aliyunsdkecs.request.v20140526.DescribeRegionsRequest import DescribeRegionsRequest
# configuration the log output formatter, if you want to save the output to file,
# append ",filename='ecs_invoke.log'" after datefmt.
logging.basicConfig(level=logging.INFO,
format='%(asctime)s %(filename)s[line:%(lineno)d] %(levelname)s %(message)s',
datefmt='%a, %d %b %Y %H:%M:%S')
clt = client.AcsClient('Your Access Key Id', 'Your Access Key Secrect', 'cn-beijing')
# sample api to list aliyun open api.
def hello_aliyun_regions():
request = DescribeRegionsRequest()
response = _send_request(request)
if response is not None:
region_list = response.get('Regions').get('Region')
assert response is not None
assert region_list is not None
result = map(_print_region_id, region_list)
logging.info("region list: %s", result)
# output the instance owned in current region.
def list_instances():
request = DescribeInstancesRequest()
response = _send_request(request)
if response is not None:
instance_list = response.get('Instances').get('Instance')
result = map(_print_instance_id, instance_list)
logging.info("current region include instance %s", result)
def _print_instance_id(item):
instance_id = item.get('InstanceId');
return instance_id
def _print_region_id(item):
region_id = item.get("RegionId")
return region_id
# send open api request
def _send_request(request):
request.set_accept_format('json')
try:
response_str = clt.do_action(request)
logging.info(response_str)
response_detail = json.loads(response_str)
return response_detail
except Exception as e:
logging.error(e)
if __name__ == '__main__':
logging.info("Hello Aliyun OpenApi!")
hello_aliyun_regions()
list_instances()