Boto3:如何从 ECR 获取最新的 docker 图像?
Boto3: How to get newest docker image from ECR?
我想使用 boto3
从 ECR 获取最新的 Docker 图像。目前我正在使用来自 ecr
客户端的 describe_images 方法,我得到了一本包含 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 获取最新的 docker 图像?
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 describe-images 文档后发现
describe-images is a paginated operation.
和boto3
可以通过get_paginator方法为您提供对特定方法的分页操作。
但是,如果您尝试直接应用 JMESPath
表达式 'sort_by(imageDetails,& imagePushedAt)[-1].imageTags[0]'
,您将收到错误消息,因为 imagePushedAt
的结果是一个 datetime.datetime
对象并且根据这个answer
Boto3 Jmespath implementation does not support dates filtering
因此,您需要将 imagePushedAt
转换为字符串 'sort_by(imageDetails, &to_string(imagePushedAt))[-1].imageTags'
。
我想使用 boto3
从 ECR 获取最新的 Docker 图像。目前我正在使用来自 ecr
客户端的 describe_images 方法,我得到了一本包含 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 获取最新的 docker 图像?
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 describe-images 文档后发现
describe-images is a paginated operation.
和boto3
可以通过get_paginator方法为您提供对特定方法的分页操作。
但是,如果您尝试直接应用 JMESPath
表达式 'sort_by(imageDetails,& imagePushedAt)[-1].imageTags[0]'
,您将收到错误消息,因为 imagePushedAt
的结果是一个 datetime.datetime
对象并且根据这个answer
Boto3 Jmespath implementation does not support dates filtering
因此,您需要将 imagePushedAt
转换为字符串 'sort_by(imageDetails, &to_string(imagePushedAt))[-1].imageTags'
。