查詢雲伺服器ECS的映象

洛小蒙發表於2018-03-17

  • 介面名稱

查詢映象源列表:DescribeImages

  • 背景:

如何查詢你所需的映象 DescribeImages介面查詢映象列表

  • 針對場景做相應的查詢

場景1. 查詢 Windows的 系統映象
場景2. 查詢 被例項使用著的 映象
場景3. 查詢 共享 的映象 (其他人共享給我的)
場景4. 查詢 Tag key=xxx value=yyy 的映象

除了您現在看到的這文章,您還可以前往:
您也可以使用 ECS 管理控制檯、ECS 命令列工具或開發工具包在 ECS 區域內查詢 ECS 系統映象。

ECS 管理控制檯-快照和映象-映象
ECS ALIYUN CLI
API文件:DescribeImages


下文以Python為示例。

安裝ECS Python SDK

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

pip install aliyun-python-sdk-ecs
如果提示您沒有許可權,請切換sudo 繼續執行。

sudo pip install aliyun-python-sdk-ecs
本文使用的sdk版本為4.6.3。

使用場景事例

# your access key Id
ak_id = "YOU_ACCESS_KEY_ID"
# your access key secret
ak_secret = "YOU_ACCESS_SECRET"

region_id = "cn-hangzhou"

clt = client.AcsClient(ak_id, ak_secret, region_id)

def _execute_request(request):
    response = _send_request(request)
    if response is None:
        print `response is None`
        return
    if response.get(`Code`) is None:
        images = response.get("Images").get(`Image`)
        totalCount = response.get(`TotalCount`)
        imageIds = []
        for image  in images :
            imageId = image.get(`ImageId`)
            imageIds.append(imageId)
        print "ecs images num %s , list is %s"%(totalCount, imageIds)

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

場景1. 查詢 Windows的 系統映象

如果你開發平臺為.Net,建議選擇windows的映象來建立ECS雲伺服器,系統自帶正版啟用的,正版啟用費用不再收取。注意:512記憶體不支援選擇Windows系統,1G以上記憶體才能很好支援該系統。

Windows

阿里雲提供了8種window系統,涵蓋了Server 2008 R2 | Server 2012 R2及 Server 2016 | window Version 1709(不含UI)這幾大類作業系統。

Linux

阿里雲官方提供的Linux公共映象:
CentOS | Ubuntu | Debian | SUSE Linux | OpenSUSE | Aliyun Linux | CoreOS | FreeBSD

# describe windows system images .
def describe_images_filter_windows():
    request = DescribeImagesRequest()
    #osType : linux | windows
    request.set_OSType(`windows`)
    request.set_ImageOwnerAlias(`system`)
    _execute_request(request)

場景2. 查詢 被ECS例項使用著的 映象

# describe used_by_instance images.
def describe_images_used_by_instance():
    request = DescribeImagesRequest()
    # usage : instance (被例項使用)| none (沒被使用)
    request.set_Usage(`instance`)
    _execute_request(request)

場景3. 查詢 共享 的映象 (其他人共享給我的)

# describe to be shared images.
def describe_images_shared_by_others():
    request = DescribeImagesRequest()
    # 映象類別:system | self (自己建立)| others (被共享的) | marketplace
    request.set_ImageOwnerAlias(`others`)
    _execute_request(request)

場景4. 查詢 Tag key=xxx value=yyy 的映象

# describe tag(key = value) images.
def describe_images_by_tag():
    request = DescribeImagesRequest()
    # 也可以只根據key過濾:  建立時設定的Tag的key
    request.set_Tag1Key(`xxxx`)
    # 也可以只根據value過濾:  建立時設定的Tag的value
    request.set_Tag1Value(`yyyy`)
    _execute_request(request)

完整程式碼

#  coding=utf-8

# if the python sdk is not install using `sudo pip install aliyun-python-sdk-ecs`

import json
import logging

from aliyunsdkcore import client
from aliyunsdkecs.request.v20140526.DescribeImagesRequest import DescribeImagesRequest

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

# your access key Id
ak_id = "YOU_ACCESS_KEY_ID"
# your access key secret
ak_secret = "YOU_ACCESS_SECRET"

region_id = "cn-hangzhou"


clt = client.AcsClient(ak_id, ak_secret, region_id)

# describe windows system images .
def describe_images_filter_windows():
    request = DescribeImagesRequest()
    #osType : linux | windows
    request.set_OSType(`windows`)
    request.set_ImageOwnerAlias(`system`)
    _execute_request(request)

# describe used_by_instance images.
def describe_images_used_by_instance():
    request = DescribeImagesRequest()
    # usage : instance (被例項使用)| none (沒被使用)
    request.set_Usage(`instance`)
    _execute_request(request)

# describe to be shared images.
def describe_images_shared_by_others():
    request = DescribeImagesRequest()
    # 映象類別:system | self (自己建立)| others (被共享的) | marketplace
    request.set_ImageOwnerAlias(`others`)
    _execute_request(request)

# describe tag(key = value) images.
def describe_images_by_tag():
    request = DescribeImagesRequest()
    # 也可以只根據key過濾:  建立時設定的Tag的key
    request.set_Tag1Key(`xxxx`)
    # 也可以只根據value過濾:  建立時設定的Tag的value
    request.set_Tag1Value(`yyyy`)
    _execute_request(request)

def _execute_request(request):
    response = _send_request(request)
    if response is None:
        print `response is None`
        return
    if response.get(`Code`) is None:
        images = response.get("Images").get(`Image`)
        totalCount = response.get(`TotalCount`)
        imageIds = []
        for image  in images :
            imageId = image.get(`ImageId`)
            imageIds.append(imageId)
        print "ecs images num %s , list is %s"%(totalCount, imageIds)

# 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__`:
    print "hello ecs describe images"
    # describe_images_filter_windows()
    # describe_images_shared_by_others()
    # describe_images_used_by_instance()
    #describe_images_by_tag()


相關文章