如何使用API列出GCP项目中的所有图像URL



我正在尝试在GO中编写一个应用程序,该应用程序将使用Container Analysis API为我获取GCP项目中的所有图像漏洞。

此API的GO客户端库具有函数findVulnerabilityOccurrencesForImage()来执行此操作,但它要求您以resourceURL := "https://gcr.io/my-project/my-repo/my-image"和项目ID的形式传递要从中获取漏洞报告的映像的URL。这意味着,如果您的项目中有多个映像,则必须首先列出并存储它们,然后才能递归调用findVulnerabilityOccurrencesForImage()函数来获取所有漏洞。

因此,我需要一种方法来获取和存储给定GCP项目中所有转发中的所有图像的URL,但到目前为止,我还找不到解决方案。我可以通过运行gcloud container images list命令在CLI中轻松地完成这项工作,但我看不出如何使用API来完成这项任务。

提前感谢您的帮助!

您可以使用云存储包和Objects方法来执行此操作。例如:

func GetURLs() ([]string, error) {
bucket := "bucket-name"
urls := []string{}
results := client.Bucket(bucket).Objects(context.Background(), nil)
for {
attrs, err := results.Next()
if err != nil {
if err == iterator.Done {
break
}
return nil, fmt.Errorf("iterating results: %w", err)
}
urls = append(urls, fmt.Sprint("https://storage.googleapis.com", "/", bucket, "/", attrs.Name))
}
return urls, nil
}

最新更新