最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

詳解通過API管理或定制開發(fā)ECS實(shí)例

 更新時(shí)間:2018年09月30日 14:23:30   投稿:laozhang  
在本文里我們給大家整理了關(guān)于通過API管理或定制開發(fā)ECS的相關(guān)實(shí)例內(nèi)容,有需要的朋友們參考學(xué)習(xí)下。

彈性管理 ECS 實(shí)例

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

使用API管理ECS實(shí)例,您需要能訪問ECS資源的API密鑰(AccessKey ID 和 AccessKey Secret)。為了保證云服務(wù)的安全,您需要?jiǎng)?chuàng)建一個(gè)能訪問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

如果提示您沒有權(quán)限,請切換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對象,這里需要RAM用戶的AccessKey ID和AccessKey Secret。

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

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)用的開發(fā)。查詢當(dāng)前賬號(hào)支持的地域列表。具體的文檔參見 查詢可用地域列表。

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)ο鬄镈escribeInstancesRequest 即可,更多的查詢參數(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()

相關(guān)文章

  • centos系統(tǒng)升級(jí)python 2.7.3

    centos系統(tǒng)升級(jí)python 2.7.3

    CentOS上安裝的python版本是2.6,不能滿足我運(yùn)行軟件的要求,所以對python進(jìn)行升級(jí)。Python的最新版本已經(jīng)是3.3,但是Python3的兼容性可能還有一定的問題,所以還是升級(jí)到2.7較為保險(xiǎn)。
    2014-07-07
  • Python中inplace、subset參數(shù)的意義及說明

    Python中inplace、subset參數(shù)的意義及說明

    這篇文章主要介紹了Python中inplace、subset參數(shù)的意義及說明,具有很好的參考價(jià)值,希望對大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2023-08-08
  • python算法測試結(jié)果自動(dòng)保存到excel表格的實(shí)現(xiàn)步驟

    python算法測試結(jié)果自動(dòng)保存到excel表格的實(shí)現(xiàn)步驟

    我們在進(jìn)行算法評估是通常會(huì)針對每個(gè)樣本的算法處理結(jié)果進(jìn)行統(tǒng)計(jì),例如每個(gè)樣本正確預(yù)測數(shù)量、漏檢數(shù)量和誤檢數(shù)量、精度等,本文小編將給大家介紹python算法測試結(jié)果自動(dòng)保存到excel表格的實(shí)現(xiàn)步驟,感興趣的朋友可以參考下
    2023-12-12
  • python typing模塊--類型提示支持

    python typing模塊--類型提示支持

    這篇文章主要介紹python typing模塊類型提示支持, typing 模塊只有在python3.5以上的版本中才可以使用,pycharm目前支持typing檢查,下面進(jìn)入文章一起了解詳細(xì)內(nèi)容吧
    2021-10-10
  • python爬取一組小姐姐圖片實(shí)例

    python爬取一組小姐姐圖片實(shí)例

    大家好,本篇文章主要講的是python爬取一組小姐姐圖片實(shí)例,感興趣的同學(xué)趕快來看一看吧,對你有幫助的話記得收藏一下
    2022-01-01
  • Python中pandas dataframe刪除一行或一列:drop函數(shù)詳解

    Python中pandas dataframe刪除一行或一列:drop函數(shù)詳解

    今天小編就為大家分享一篇Python中pandas dataframe刪除一行或一列:drop函數(shù)詳解,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2018-07-07
  • python的描述器descriptor詳解

    python的描述器descriptor詳解

    這篇文章主要介紹了python的描述器descriptor詳解,描述器可以用于控制屬性的讀取、寫入和刪除等操作,同時(shí)還可以用于實(shí)現(xiàn)計(jì)算屬性、類屬性、屬性別名等高級(jí)功能,需要的朋友可以參考下
    2023-07-07
  • python中numpy包使用教程之?dāng)?shù)組和相關(guān)操作詳解

    python中numpy包使用教程之?dāng)?shù)組和相關(guān)操作詳解

    這篇文章主要給大家介紹了關(guān)于python中numpy包的使用教程,包含數(shù)組和相關(guān)操作等內(nèi)容,文中通過示例代碼介紹的非常詳細(xì),對大家具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來跟著小編一起學(xué)習(xí)學(xué)習(xí)吧。
    2017-07-07
  • Python實(shí)現(xiàn)數(shù)據(jù)清洗的示例詳解

    Python實(shí)現(xiàn)數(shù)據(jù)清洗的示例詳解

    這篇文章主要通過五個(gè)示例帶大家深入了解下Python實(shí)現(xiàn)數(shù)據(jù)清洗的具體方法,文中的示例代碼講解詳細(xì),對我們學(xué)習(xí)Python有一定幫助,需要的可以參考一下
    2022-08-08
  • Python 集合之set詳解

    Python 集合之set詳解

    這篇文章主要介紹了python基礎(chǔ)之set集合詳解,文中有非常詳細(xì)的代碼示例,對正在學(xué)習(xí)python的小伙伴們有很好地幫助,需要的朋友可以參考下
    2021-09-09

最新評論

陕西省| 柘城县| 内江市| 金塔县| 麟游县| 榕江县| 河源市| 长沙县| 中山市| 竹溪县| 清苑县| 三台县| 方正县| 兴义市| 扬州市| 甘南县| 美姑县| 新余市| 西丰县| 峡江县| 元朗区| 龙井市| 安西县| 桦甸市| 鲁甸县| 原平市| 丽水市| 长垣县| 赞皇县| 大余县| 婺源县| 阿拉尔市| 沈丘县| 宣化县| 广灵县| 濮阳县| 新疆| 岫岩| 包头市| 淄博市| 巴彦县|