小編給大家分享一下如何通過(guò)API管理或定制開(kāi)發(fā)ECS實(shí)例,希望大家閱讀完這篇文章之后都有所收獲,下面讓我們一起去探討吧!
彈性管理 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è) 查詢磁盤列表,將實(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()
看完了這篇文章,相信你對(duì)“如何通過(guò)API管理或定制開(kāi)發(fā)ECS實(shí)例”有了一定的了解,如果想了解更多相關(guān)知識(shí),歡迎關(guān)注創(chuàng)新互聯(lián)成都網(wǎng)站設(shè)計(jì)公司行業(yè)資訊頻道,感謝各位的閱讀!
另外有需要云服務(wù)器可以了解下創(chuàng)新互聯(lián)scvps.cn,海內(nèi)外云服務(wù)器15元起步,三天無(wú)理由+7*72小時(shí)售后在線,公司持有idc許可證,提供“云服務(wù)器、裸金屬服務(wù)器、高防服務(wù)器、香港服務(wù)器、美國(guó)服務(wù)器、虛擬主機(jī)、免備案服務(wù)器”等云主機(jī)租用服務(wù)以及企業(yè)上云的綜合解決方案,具有“安全穩(wěn)定、簡(jiǎn)單易用、服務(wù)可用性高、性價(jià)比高”等特點(diǎn)與優(yōu)勢(shì),專為企業(yè)上云打造定制,能夠滿足用戶豐富、多元化的應(yīng)用場(chǎng)景需求。
網(wǎng)站名稱:如何通過(guò)API管理或定制開(kāi)發(fā)ECS實(shí)例-創(chuàng)新互聯(lián)
URL分享:http://aaarwkj.com/article0/ihpoo.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站維護(hù)、Google、品牌網(wǎng)站建設(shè)、外貿(mào)建站、網(wǎng)站設(shè)計(jì)公司、全網(wǎng)營(yí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)
猜你還喜歡下面的內(nèi)容