Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/18.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 3.x Boto3脚本删除所有未标记的图像_Python 3.x_Boto3_Aws Ecr - Fatal编程技术网

Python 3.x Boto3脚本删除所有未标记的图像

Python 3.x Boto3脚本删除所有未标记的图像,python-3.x,boto3,aws-ecr,Python 3.x,Boto3,Aws Ecr,我想删除所有未标记的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_reposi

我想删除所有未标记的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',
    imageIds=[
        {

            'imageTag': 'untagged'
        },
    ]
)
我可以列出所有ecr图像,但不能删除“未标记”的图像 如果我将“未标记”替换为“最新”,则该图像将被删除
如何引用所有未标记的图像

您可以使用
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)