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

huangapple go评论80阅读模式
英文:

How can I list all image URLs inside a GCP project with an API?

问题

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

这个API的GO客户端库有一个名为findVulnerabilityOccurrencesForImage()的函数可以实现这个功能,但是它要求你以resourceURL := "https://gcr.io/my-project/my-repo/my-image"的形式传递你想要获取漏洞报告的图像的URL以及项目ID。这意味着如果你的项目中有多个图像,你必须先列出并存储它们,然后才能递归调用findVulnerabilityOccurrencesForImage()函数来获取所有的漏洞。

因此,我需要一种方法来获取并存储给定GCP项目中所有存储库中的所有图像的URL,但是到目前为止我还没有找到解决方案。我可以通过运行gcloud container images list命令在CLI中轻松实现这一点,但是我不知道如何通过API来实现。

非常感谢您的帮助!

英文:

I'm trying to write an application in GO that will get all the image vulnerabilities inside a GCP project for me using the Container Analysis API.

The GO Client library for this API has the function findVulnerabilityOccurrencesForImage() to do this, however it requires you to pass the URL of the image you want to get the vulnerability report from in the form resourceURL := "https://gcr.io/my-project/my-repo/my-image" and the projectID. This means that if there are multiple images in your project, you have to list and store them first and only after that you can recursively call the findVulnerabilityOccurrencesForImage() function to get ALL of the vulnerabilities.

So I need a way to get and store all of the images' URLs inside all of the repos inside a given GCP project, but so far I couldn't find a solution. I can easily do that in the CLI by running gcloud container images list command but I don't see a way how that can be done with an API.

Thank you in advance for your help!

答案1

得分: 1

你可以使用Cloud Storage包和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("迭代结果时出错:%w", err)
        }

        urls = append(urls, fmt.Sprint("https://storage.googleapis.com", "/", bucket, "/", attrs.Name))
    }

    return urls, nil
}
英文:

You can use the Cloud Storage package and the Objects method to do so. For example:

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
}

huangapple
  • 本文由 发表于 2021年11月12日 23:42:25
  • 转载请务必保留本文链接:https://go.coder-hub.com/69945472.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定