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

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

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

彈性管理 ECS 實例

獲取 RAM 子賬號 AK 密鑰

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

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

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

直接給RAM用戶授權(quán),授予RAM用戶 管理云服務器服務(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,首先實例化AcsClient對象,這里需要RAM用戶的AccessKey ID和AccessKey Secret。

AccessKey ID和AccessKey Secret是RAM用戶訪問阿里云ECS服務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')

完成實例化后可以進行第一個應用的開發(fā)。查詢當前賬號支持的地域列表。具體的文檔參見 查詢可用地域列表。

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 列表非常類似,替換入?yún)ο鬄镈escribeInstancesRequest 即可,更多的查詢參數(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 概覽,您可以嘗試作一個 查詢磁盤列表,將實例的參數(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)升級python 2.7.3

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

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

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

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

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

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

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

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

    python爬取一組小姐姐圖片實例

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

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

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

    python的描述器descriptor詳解

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

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

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

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

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

    Python 集合之set詳解

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

最新評論

仙居县| 隆安县| 葫芦岛市| 安顺市| 广宗县| 桃园市| 交城县| 乌拉特前旗| 万年县| 开封县| 西青区| 余庆县| 绥芬河市| 海宁市| 磐安县| 梅河口市| 北票市| 内丘县| 岑溪市| 招远市| 嵊州市| 弥勒县| 博爱县| 林甸县| 临桂县| 读书| 富民县| 平昌县| 沁源县| 得荣县| 衡水市| 奉新县| 印江| 铅山县| 洛隆县| 海宁市| 博兴县| 宜兰县| 垦利县| 平远县| 隆林|