Boto3脚本删除所有未标记的图像

我要删除所有未标记的ecr图像

import boto3
import pprint

s3 = boto3.resource('s3')
for bucket in s3.buckets.all():
    print(bucket.name)


pp = pprint.PrettyPrinter(indent=4)
client = boto3.client('ecr',region_name='us-west-2')
response = client.describe_repositories(repositoryNames=['localstack-centos'])
#print(response)

""" response1 = client.describe_images(
    repositoryName='localstack-centos',#maxResults=2,imageIds=[
        {

            'imagetag': 'untagged'
        },],) """

#print(response1)


response2 = client.list_images(

    repositoryName='localstack-centos',maxResults=123,filter={
        'tagStatus': 'UNTAGGED'
    }
)

print(response2)
pp.pprint(response2)  

response = client.batch_delete_image(
    registryId='string',repositoryName='localstack-centos',]
)

我能够列出所有ecr图片,但不能删除“未标记”图片 如果我将'untagged'替换为Latest,则该图像将被删除 我将如何引用所有未标记的图像

WO129876SHI 回答:Boto3脚本删除所有未标记的图像

您可以使用imageDigest删除图像。收集要首先删除的imageDigest张图像,然后将其删除。

import boto3

client = boto3.client('ecr')

response = client.list_images(repositoryName='localstack-centos')
untaggedImageList = [image for image in response['imageIds'] if image['imageTag'] == 'untagged']
response2 = client.batch_delete_image(repositoryName='localstack-centos',imageIds=untaggedImageList)

print(response2)
本文链接:https://www.f2er.com/3107419.html

大家都在问