Mig*_*ejo 4 python amazon-web-services docker boto3 amazon-ecr
我想使用 .ECR 从 ECR 获取最新的 Docker 映像boto3。目前我正在使用客户端的describe_images方法ecr,我得到了一本字典imageDetails
import boto3
registry_name = 'some_registry_name_in_aws'
client = boto3.client('ecr')
response = client.describe_images(
repositoryName=registry_name,
)
Run Code Online (Sandbox Code Playgroud)
有一个使用 的解决方案aws-cli,但文档没有描述任何--query可以传递给 的参数describe_images。那么,如何使用 boto3 从 ECR 获取最新的 docker 镜像?
您需要使用分页器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 = paginator.paginate(repositoryName=registry_name)
filter_iterator = iterator.search(jmespath_expression)
result = list(filter_iterator)[0]
result
>>>
'latest_image_tag'
Run Code Online (Sandbox Code Playgroud)
阅读 cli描述图像文档后发现
描述图像是一个分页操作。
并且可以使用get_paginatorboto3方法为您提供特定方法的分页操作。
但是,如果您尝试直接应用表达式JMESPath,'sort_by(imageDetails,& imagePushedAt)[-1].imageTags[0]'则会收到错误,因为结果是imagePushedAt一个datetime.datetime对象,并且根据此答案
Boto3 Jmespath实现不支持日期过滤
所以,你需要转换imagePushedAt为 string 'sort_by(imageDetails, &to_string(imagePushedAt))[-1].imageTags'。
| 归档时间: |
|
| 查看次数: |
4491 次 |
| 最近记录: |