无法使用Go客户端库列出GCP中的文件夹。

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

Unable to list folders in GCP using go client library

问题

我正在阅读cloudresourcemanager包的文档,并尝试构建一个简单的示例来列出我的GCP项目的文件夹。

然而,以下示例失败了:

package main

import (
	"context"
	"fmt"
	"log"

	cloudresourcemanager "google.golang.org/api/cloudresourcemanager/v2"
)

func main() {
	ctx := context.Background()
	svc, err := cloudresourcemanager.NewService(ctx)
	if err != nil {
		log.Fatal(err)
	}
	foldersService := cloudresourcemanager.NewFoldersService(svc)
	fmt.Println(foldersService)
	foldersListCall := foldersService.List()
	resp, err := foldersListCall.Do()
	if err != nil {
		fmt.Println("Here")
		log.Fatal(err)
	}
	for _, fld := range resp.Folders {
		fmt.Println(fld.Name)
	}
}

它在以下代码处失败:

resp, err := foldersListCall.Do()

错误信息为:

googleapi: Error 400: Request contains an invalid argument., badRequest

我设置了以下环境变量:

GOOGLE_CLOUD_PROJECT=my-project-id
GOOGLE_APPLICATION_CREDENTIALS=/path/to/application_default_credentials.json

并且gcloud命令行工具正常工作。

有什么建议吗?我可能漏掉了什么吗?

英文:

I am going through the documentation of cloudresourcemanager package and trying to build a simple example to list the folders of my GCP project.

The following example however fails

package main

import (
	"context"
	"fmt"
	"log"

	cloudresourcemanager "google.golang.org/api/cloudresourcemanager/v2"
)

func main() {
	ctx := context.Background()
	svc, err := cloudresourcemanager.NewService(ctx)
	if err != nil {
		log.Fatal(err)
	}
	foldersService := cloudresourcemanager.NewFoldersService(svc)
	fmt.Println(foldersService)
	foldersListCall := foldersService.List()
	resp, err := foldersListCall.Do()
	if err != nil {
		fmt.Println("Here")
		log.Fatal(err)
	}
	for _, fld := range resp.Folders {
		fmt.Println(fld.Name)
	}
}

It fails in

resp, err := foldersListCall.Do()

and the error is

googleapi: Error 400: Request contains an invalid argument., badRequest

I have the following environment variables set

GOOGLE_CLOUD_PROJECT=my-project-id
GOOGLE_APPLICATION_CREDENTIALS=/path/to/application_default_credentials.json

and gcloud cli works fine.

Any suggestions what I might be missing?

答案1

得分: 2

错误消息一点帮助都没有...

问题是我没有在请求中设置Parent参数,即organization(切换到v3有所帮助)

package main

import (
	"context"
	"fmt"
	"log"

	cloudresourcemanager "google.golang.org/api/cloudresourcemanager/v3"
)

func main() {
	ctx := context.Background()
	svc, err := cloudresourcemanager.NewService(ctx)
	if err != nil {
		log.Fatal(err)
	}
	foldersService := cloudresourcemanager.NewFoldersService(svc)
	foldersListCall := foldersService.List()
	foldersListCall.Parent("organizations/12345678910")
	resp, err := foldersListCall.Do()
	if err != nil {
		log.Fatal(err)
	}
	for _, fld := range resp.Folders {
		fmt.Println(fld.DisplayName)
	}
}
英文:

The error message is not helpful at all...

The problem is I was not setting the Parent parameter in the request, i.e. the organization (switching to v3 helped a bit)

package main

import (
	"context"
	"fmt"
	"log"

	cloudresourcemanager "google.golang.org/api/cloudresourcemanager/v3"
)

func main() {
	ctx := context.Background()
	svc, err := cloudresourcemanager.NewService(ctx)
	if err != nil {
		log.Fatal(err)
	}
	foldersService := cloudresourcemanager.NewFoldersService(svc)
	foldersListCall := foldersService.List()
	foldersListCall.Parent("organizations/12345678910")
	resp, err := foldersListCall.Do()
	if err != nil {
		log.Fatal(err)
	}
	for _, fld := range resp.Folders {
		fmt.Println(fld.DisplayName)
	}
}


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

发表评论

匿名网友

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

确定