查询云服务器ECS的镜像

简介: 查询ECS的镜像,针对特定的场景如何查找镜像,包括 1. 查找 Windows的 系统镜像,2.查找 被实例使用着的 镜像 3. 查找 共享 的镜像 (其他人共享给我的)4.查找 带Tag( key=xxx value=yyy) 的镜像

  • 接口名称

    查询镜像源列表:DescribeImages

  • 背景:

    如何查找你所需的镜像 DescribeImages接口查询镜像列表

  • 针对场景做相应的查询

场景1. 查找 Windows的 系统镜像
场景2. 查找 被实例使用着的 镜像
场景3. 查找 共享 的镜像 (其他人共享给我的)
场景4. 查找 Tag key=xxx value=yyy 的镜像

除了您现在看到的这文章,您还可以前往:
您也可以使用 ECS 管理控制台、ECS 命令行工具或开发工具包在 ECS 区域内查询 ECS 系统镜像。

ECS 管理控制台-快照和镜像-镜像
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()
相关实践学习
通义万相文本绘图与人像美化
本解决方案展示了如何利用自研的通义万相AIGC技术在Web服务中实现先进的图像生成。
7天玩转云服务器
云服务器ECS(Elastic Compute Service)是一种弹性可伸缩的计算服务,可降低 IT 成本,提升运维效率。本课程手把手带你了解ECS、掌握基本操作、动手实操快照管理、镜像管理等。了解产品详情: https://www.aliyun.com/product/ecs
目录
相关文章
|
5月前
|
弹性计算 搜索推荐 异构计算
阿里云服务器多少钱一年?亲自整理ECS、轻量和GPU服务器租赁价格表
2025年阿里云服务器优惠汇总:轻量应用服务器2核2G 38元/年起,ECS 2核2G 99元/年,2核4G 199元/年,4核16G 89元/月,8核32G 160元/月,香港轻量25元/月起,新老用户同享,续费同价。
1556 158
|
5月前
|
弹性计算 运维 安全
阿里云轻量应用服务器与云服务器ECS啥区别?新手帮助教程
阿里云轻量应用服务器适合个人开发者搭建博客、测试环境等低流量场景,操作简单、成本低;ECS适用于企业级高负载业务,功能强大、灵活可扩展。二者在性能、网络、镜像及运维管理上差异显著,用户应根据实际需求选择。
446 10
|
5月前
|
弹性计算 运维 安全
区别及选择指南:阿里云轻量应用服务器与ECS云服务器有什么区别?
阿里云轻量应用服务器适合个人开发者、学生搭建博客、测试环境,易用且性价比高;ECS功能更强大,适合企业级应用如大数据、高流量网站。根据需求选择:轻量入门首选,ECS专业之选。
373 2
|
5月前
|
弹性计算 ice
阿里云4核8G云服务器配置价格:热门ECS实例及CPU处理器型号说明
阿里云2025年4核8G服务器配置价格汇总,涵盖经济型e实例、计算型c9i等热门ECS实例,CPU含Intel Xeon及AMD EPYC系列,月费159元起,年付低至1578元,按小时计费0.45元起,实际购买享折扣优惠。
1690 1
|
5月前
|
弹性计算 运维 安全
阿里云轻量应用服务器38元1年和云服务器99元1年怎么选?二者性能区别及选择参考
在阿里云当下的活动中,38元/年的轻量应用服务器与99元/年的云服务器ECS成为众多新用户的关注焦点。但是有部分用户并不是很清楚二者之间的区别,因此就不知道应该如何选择。接下来,笔者将为您详细剖析ECS云服务器与轻量应用服务器的差异,以供您参考和选择。
567 4
阿里云轻量应用服务器38元1年和云服务器99元1年怎么选?二者性能区别及选择参考
|
5月前
|
弹性计算 网络协议 Linux
阿里云服务器简介及使用教程,附送云服务器ECS自定义创建流程
阿里云ECS是安全可靠、弹性灵活的云计算服务,支持多种实例规格与操作系统,可快速创建和管理云服务器。本文详解ECS介绍、购买流程及使用教程,涵盖配置选择、网络设置、安全组规则等,助您轻松上手。
576 16
|
5月前
|
存储 弹性计算 网络协议
超详细的阿里云服务器购买流程,ECS自定义购买配置教程
本文详细图解阿里云ECS服务器自定义购买全流程,涵盖付费模式、地域选择、网络配置、实例规格、镜像、存储、安全组及登录设置等核心步骤,助您轻松掌握专业级云服务器搭建方法。
|
5月前
|
弹性计算 搜索推荐 异构计算
租用阿里云服务器一年要多少钱?ECS、轻量和GPU服务器租赁价格,手动整理
2025年10月阿里云服务器优惠持续,轻量应用服务器200M带宽38元起/年,ECS 2核2G 99元/年、2核4G 199元/年,4核16G 89元/月,8核32G 160元/月,香港轻量25元/月起,新老同享,续费不涨价。
1139 2
|
5月前
|
存储 弹性计算 网络协议
阿里云服务器ECS是什么?ECS介绍、云服务器创建及使用教程
阿里云ECS是安全可靠、弹性灵活的云计算服务,支持多种实例规格与操作系统,可快速创建和管理云服务器。本文详解ECS介绍、购买流程(含付费模式、地域、网络、存储等设置)及使用教程,助您轻松上手云服务器。
642 4

热门文章

最新文章

相关产品

  • 云服务器 ECS