我想得到最新的码头形象从ECR使用boto3。目前,我正在从图片客户机中使用ecr方法,并得到了一个带有imageDetails的字典
import boto3
registry_name = 'some_registry_name_in_aws'
client = boto3.client('ecr')
response = client.describe_images(
repositoryName=registry_name,
)有一个使用解决方案的aws-cli,但是文档中没有描述任何可以传递给describe_images的--query参数。那么,如何使用boto3从ECR获得最新的对接图像呢?
发布于 2021-12-30 15:37:11
TL;DR
您需要在describe_images上使用分页器和JMESPath表达式。
import boto3
registry_name = 'some_registry_name_in_aws'
jmespath_expression = 'sort_by(imageDetails, &to_string(imagePushedAt))[-1].imageTags'
client = boto3.client('ecr')
paginator = client.get_paginator('describe_images')
iterator = client_paginator.paginate(repositoryName=registry_name)
filter_iterator = iterator.search(jmespath_expression)
result = list(filter_iterator)[0]
result
>>>
'latest_image_tag'解释
在阅读cli 描述-图像文档之后,可以发现
描述-图像是一个分页操作。
boto3可以使用分页器方法为特定方法提供分页操作。
但是,如果您尝试直接应用JMESPath表达式'sort_by(imageDetails,& imagePushedAt)[-1].imageTags[0]',您将得到一个错误,因为来自imagePushedAt的结果是一个datetime.datetime对象,并且根据这个回答
Boto3 Jmespath实现不支持日期筛选
因此,您需要将imagePushedAt转换为字符串'sort_by(imageDetails, &to_string(imagePushedAt))[-1].imageTags'。
https://stackoverflow.com/questions/70533690
复制相似问题