简体   繁体   中英

Bigquery Golang client get projects list

How can I get a list of projects that the golang cloud client has access to using the serviceaccount key that I have provided? Seems like there is an API available: https://cloud.google.com/bigquery/docs/reference/rest/v2/projects/list

However, the Google Cloud client library does not expose it.

My code looks like this

client, err := bigquery.NewClient(ctx, conn.ProjectID, option.WithServiceAccountFile(fname))

I want to enumerate the list of projects that this client can access.

below example can give you an idea - it is sandboxed go that is part of our BQ UI


在此处输入图片说明


func mainReturn() interface{} {
    bigqueryService, err := getBigqueryService() ; if(err != nil) {return "ERROR"}
    err2 := getProjects(bigqueryService)         ; if(err2 != nil) {return "ERROR"}
    return "OK"
}

func getProjects(bigqueryService *bigquery.Service)(error) {
    var buf bytes.Buffer
    var pagetoken string = ""
    ind := 0
    for {
        call := bigqueryService.Projects.List()
        if len(pagetoken) > 0 {call = call.PageToken(pagetoken)}

        list, err := call.Do() ; if err != nil {return err}

        if len(pagetoken) == 0 {
            xFprintf(&buf, "TotalItems: %v\n", list.TotalItems)
        }

        for _, project := range list.Projects {
            xFprintf(&buf, "========= Projects %v ==============\n", ind)
            xFprintf(&buf, "Id: %v\n", project.Id)
            xFprintf(&buf, "Kind: %v\n", project.Kind)
            project := project.Id
            del := strings.LastIndex(project, ".")
            if del != -1 {project = project[del+1:]}
            ind++;
        }

        pagetoken = list.NextPageToken ; if len(pagetoken) == 0 {break}
    }

    xPrintln(buf.String())
    return nil
}

Please ignore some funny looking functions like xFprintf - this is just way we sandboxed go

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM