API管理或定制如何開(kāi)發(fā)ECS-創(chuàng)新互聯(lián)

今天就跟大家聊聊有關(guān) API 管理或定制如何開(kāi)發(fā) ECS,可能很多人都不太了解,為了讓大家更加了解,小編給大家總結(jié)了以下內(nèi)容,希望大家根據(jù)這篇文章可以有所收獲。

創(chuàng)新互聯(lián)主要從事做網(wǎng)站、網(wǎng)站制作、網(wǎng)頁(yè)設(shè)計(jì)、企業(yè)做網(wǎng)站、公司建網(wǎng)站等業(yè)務(wù)。立足成都服務(wù)辛集,十載網(wǎng)站建設(shè)經(jīng)驗(yàn),價(jià)格優(yōu)惠、服務(wù)專業(yè),歡迎來(lái)電咨詢建站服務(wù):028-86922220

彈性管理 ECS 實(shí)例

獲取 RAM 子賬號(hào) AK 密鑰

使用API管理ECS實(shí)例,您需要能訪問(wèn)ECS資源的API密鑰(AccessKey ID 和 AccessKey Secret)。為了保證云服務(wù)的安全,您需要?jiǎng)?chuàng)建一個(gè)能訪問(wèn)ECS資源的RAM用戶,獲取該用戶的AccessKey密鑰,并使用這個(gè)RAM用戶和API管理ECS實(shí)例。

以下是獲取RAM用戶AccessKey密鑰的操作步驟:

創(chuàng)建RAM用戶并獲取AccessKey密鑰。

直接給RAM用戶授權(quán),授予RAM用戶 管理云服務(wù)器服務(wù)(ECS)的權(quán)限。

安裝 ECS Python SDK

首先確保您已經(jīng)具備Python的Runtime,本文中使用的Python版本為2.7+。

pip install aliyun-python-sdk-ecs

如果提示您沒(méi)有權(quán)限,請(qǐng)切換sudo繼續(xù)執(zhí)行。

sudo pip install aliyun-python-sdk-ecs

本文使用的SDK版本為 2.1.2。

Hello Alibaba Cloud

創(chuàng)建文件 hello_ecs_api.py。為了使用SDK,首先實(shí)例化AcsClient對(duì)象,這里需要RAM用戶的AccessKey ID和AccessKey Secret。

AccessKey ID和AccessKey Secret是RAM用戶訪問(wèn)阿里云ECS服務(wù)API的密鑰,具有該賬戶完全的權(quán)限,請(qǐng)妥善保管。

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')

完成實(shí)例化后可以進(jìn)行第一個(gè)應(yīng)用的開(kāi)發(fā)。查詢當(dāng)前賬號(hào)支持的地域列表。具體的文檔參見(jiàn) 查詢可用地域列表。

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()

在命令行運(yùn)行 python hello_ecs_api.py 會(huì)得到當(dāng)前支持的 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']

查詢當(dāng)前的 Region 下的 ECS 實(shí)例列表

查詢實(shí)例列表和查詢 Region 列表非常類似,替換入?yún)?duì)象為DescribeInstancesRequest 即可,更多的查詢參數(shù)參考 查詢實(shí)例列表。

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

輸出結(jié)果為如下:

current region include instance [u'i-****', u'i-****'']

更多的API參考 ECS API 概覽,您可以嘗試作一個(gè) 查詢磁盤(pán)列表,將實(shí)例的參數(shù)替換為 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()

看完上述內(nèi)容,你們對(duì) API 管理或定制如何開(kāi)發(fā) ECS有進(jìn)一步的了解嗎?如果還想了解更多知識(shí)或者相關(guān)內(nèi)容,請(qǐng)關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道,感謝大家的支持。

分享題目:API管理或定制如何開(kāi)發(fā)ECS-創(chuàng)新互聯(lián)
標(biāo)題URL:http://muchs.cn/article26/pdpcg.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供軟件開(kāi)發(fā)網(wǎng)頁(yè)設(shè)計(jì)公司、品牌網(wǎng)站設(shè)計(jì)靜態(tài)網(wǎng)站、企業(yè)網(wǎng)站制作、微信小程序

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)

成都app開(kāi)發(fā)公司