簡體   English   中英

python boto3 ecr start_image_scan 出錯,顯示“[ERROR] TypeError:start_image_scan() 僅接受關鍵字 arguments。”

[英]python boto3 ecr start_image_scan erroring out with "[ERROR] TypeError: start_image_scan() only accepts keyword arguments."

我正在嘗試創建一個 AWS lambda 腳本來自動掃描 ecr (Elastic Container Registry) 圖像。 我有一個 lambda (python) 循環遍歷我的圖像並提供 boto3 start_image_scan 命令來啟動掃描。 該代碼用於生成 start_image_scan() 的輸入。 如果我使用輸入啟動掃描,它會失敗並顯示錯誤“[ERROR] TypeError:start_image_scan() 只接受關鍵字 arguments。” 如果我將 output 硬編碼到命令中,它就可以工作。 我嘗試了幾種將輸入轉換為字符串的方法。 我已將它們作為注釋包含在代碼中。 他們因相同的錯誤消息而失敗。 任何人的任何意見都將不勝感激。

import json
import boto3


def get_reponames():
    client = boto3.client('ecr')
    reponames = [repo['repositoryName'] for repo in client.describe_repositories()['repositories']]

    return reponames


def get_imageids(prepo):
    client = boto3.client('ecr')
    imageids = [
        {"imageDigest": img['imageDigest'], "imageTag": img.get('imageTag', None)} for img in
        client.list_images(repositoryName=prepo, )['imageIds']
    ]

    return imageids


def lambda_handler(event, context):
    client = boto3.client('ecr')
    output = get_reponames()
    for rn in output:
        outputii = get_imageids(rn)
        for ii in outputii:
            if ii['imageTag'].lower() == "latest":
                print("scan this image")
                scanid = 'repositoryName="'+rn+'", imageId={"imageDigest": "'+ii['imageDigest']+'", "imageTag": "'+ii['imageTag']+'"}'
                # scanid = (json.dumps('repositoryName="'+rn+'", imageId={"imageDigest": "'+ii['imageDigest']+'", "imageTag": "'+ii['imageTag']+'"}').replace("\\", ""))[1:-1]
                # scanid = (str('repositoryName="'+rn+'", imageId={"imageDigest": "'+ii['imageDigest']+'", "imageTag": "'+ii['imageTag']+'"}')
                print(scanid)
                # client.start_image_scan(repositoryName="testimage", imageId={'imageDigest': 'sha256:24d7013a7b2805accd177279c6549d08e45059cc5696e32f5c0184f3652daaaa', 'imageTag': '15'})
                client.start_image_scan(scanid)
            else:
                print("DONT SCAN")

    return {
        'body': json.dumps("hello world")
    }

根據文檔,您需要遵循方法的簽名。

client.start_image_scan(repositoryName=rn, imageId={'imageDigest': ii['imageDigest']})

您還只需要指定一個標識符——標簽或摘要,不必兩者都指定。

由於您似乎只想掃描latest標簽,您也可以執行以下操作:

client.start_image_scan(repositoryName=rn, imageId={'imageTag': 'latest'})

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM