使用OpenApi弹性管理云服务器ECS

本文涉及的产品
轻量应用服务器 2vCPU 1GiB,适用于搭建电商独立站
轻量应用服务器 2vCPU 4GiB,适用于网站搭建
轻量应用服务器 2vCPU 4GiB,适用于搭建Web应用/小程序
简介: 阿里云的云服务器ECS除了提供控制台来进行日常的管理和资源创建,还提供了OpenApi来进行资源的管理和定制开发。通过OpenApi您可以更加灵活的管理和配置云服务器。 阿里云提供了SDK来包装OpenApi,可以让您将云服务器的管理集成到您的已有系统中。

阿里云的云服务器ECS除了提供控制台来进行日常的管理和资源创建,还提供了OpenApi来进行资源的管理和定制开发。通过OpenApi您可以更加灵活的管理和配置云服务器。

阿里云提供了SDK来包装OpenApi,可以让您将云服务器的管理集成到您的已有系统中。本文以Python的开发来说明OpenApi如何来管理云服务器,即便您没有Python的开发经验,通过本文也可以轻松的0基础入门进行云服务的开发。其它语言的开发和管理您可以通过留言沟通。

安装ECS Python SDK

首先确保您已经具备Python的Runtime,本文中使用的Python版本为2.7+。

pip install aliyun-python-sdk-ecs
AI 代码解读

如果提示您没有权限,请切换sudo 继续执行。

sudo pip install aliyun-python-sdk-ecs
AI 代码解读

本文使用的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')
AI 代码解读

完成了实例化之后就可以进行您的第一个应用的开发。做一个简单的查询查询下当前您的账号支持的地域列表。具体的文档参见查询可用地域列表.


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()    
AI 代码解读

在命令行运行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']
AI 代码解读

查询当前的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
AI 代码解读

输出结果为如下

current region include instance [u'i-****', u'i-****'']
AI 代码解读

更多的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()
AI 代码解读
相关实践学习
通义万相文本绘图与人像美化
本解决方案展示了如何利用自研的通义万相AIGC技术在Web服务中实现先进的图像生成。
7天玩转云服务器
云服务器ECS(Elastic Compute Service)是一种弹性可伸缩的计算服务,可降低 IT 成本,提升运维效率。本课程手把手带你了解ECS、掌握基本操作、动手实操快照管理、镜像管理等。了解产品详情: https://www.aliyun.com/product/ecs
祝犁
+关注
目录
打赏
0
0
0
0
354
分享
相关文章
阿里云服务器ECS是什么?ECS应用场景、租用流程及使用教程整理
阿里云ECS(弹性计算服务)是性能稳定、弹性扩展的云计算服务,支持多种处理器架构和实例类型,适用于网站托管、开发测试、数据存储、企业服务、游戏多媒体及微服务架构等场景。提供从注册、配置到部署、运维的完整使用流程,助力用户高效上云。
阿里云服务器4核8G配置:ECS实例规格、CPU型号及使用场景说明
阿里云4核8G服务器ECS提供多种实例规格,包括高主频计算型hfc8i、计算型c8i、通用算力型u1、经济型e等。各规格配备不同CPU型号与主频性能,适用于机器学习、数据分析、游戏服务器、Web前端等多种场景。用户可根据需求选择Intel或AMD处理器,如第四代Xeon或AMD EPYC系列,满足高性能计算及企业级应用要求。更多详情参见阿里云官方文档。
218 1
阿里云服务器ECS是什么?一张图看懂云服务器ECS全解析
阿里云云服务器ECS(Elastic Compute Service)是阿里云提供的高性能、稳定可靠、弹性扩展的基础设施即服务(IaaS)云计算服务。它免去传统IT硬件采购流程,让用户像使用水电一样便捷使用计算资源,实现即开即用与弹性伸缩。详细了解请访问阿里云官方页面。
阿里云服务器一小时收费价格,不同ECS是实例按量付费1小时费用整理
阿里云ECS云服务器按小时计费,价格根据实例类型和配置不同而异。例如经济型e实例2核2G配置0.094元/小时,通用算力型u1实例2核4G配置0.351元/小时,计算型c9i实例2核4G配置0.3873元/小时,4核8G配置0.7746元/小时。不同规格实例价格差异明显,具体以官网信息为准。
阿里云海外云服务器租赁价格:轻量+ECS云服务器,境外节点整理
阿里云推出2025年最新海外云服务器租赁方案,轻量应用服务器200M带宽,25元/月起,支持中国香港、新加坡、日本、美国等14个地域节点。配置从2核0.5G到4核16G可选,ESSD系统盘、BGP线路,适合多场景应用。ECS云服务器同样提供丰富配置选择,满足不同业务需求,详情请访问阿里云官网。
160 0
阿里云服务器收费标准与最新活动价格一览,轻量应用服务器38元起,云服务器99元起
阿里云服务器最新价格参考,云服务器的收费标准主要包含CPU内存配置价格、云盘价格和带宽价格等,官方会不定期调整收费标准和活动价格,目前,共享型经济型e实例云服务器2核2G3M还是只要99元1年,独享型通用算力型u1实例云服务器2核4G5M企业用户购买只要199元1年,而轻量应用服务器的抢购价格已经到了38元1年,每天仅需0.1元。更多配置的云服务器的最新收费标准和活动价格表见下文。
3分钟幻兽帕鲁游戏链接服务器一键部署教程,基于阿里云服务器
本教程介绍如何使用阿里云服务器快速部署《幻兽帕鲁》联机服务,支持与好友联机游戏。内容包括服务器配置、计费说明、服务创建及登录游戏步骤,同时提供存档管理与配置修改方法,助您轻松搭建专属游戏服务器。
阿里云服务器租用价格:云服务器ECS/轻量/GPU收费标准与活动价格参考
阿里云服务器产品主要包括云服务器ECS、轻量应用服务器以及GPU云服务器等。为了方便大家了解阿里云各类服务器的价格信息,本文整理汇总了阿里云服务器、轻量应用服务器、GPU云服务器的最新收费标准以及活动价格情况,供大家参考选择。

热门文章

最新文章

相关产品

  • 云服务器 ECS
  • AI助理

    你好,我是AI助理

    可以解答问题、推荐解决方案等

    登录插画

    登录以查看您的控制台资源

    管理云资源
    状态一览
    快捷访问