将服务帐户密钥文件传递给bigquery客户端

问题描述 投票:0回答:1

我试图使用服务帐户与bigquery客户端。出于某种原因,在制作客户端时没有Go示例如何传递服务文件。我在这里看到https://cloud.google.com/docs/authentication/production,但这种模式并没有遵循任何显示。它也没有在https://godoc.org/cloud.google.com/go/bigquery上显示

我有

//bigquery_caller.go
package main

import "C"
import "fmt"
import "cloud.google.com/go/bigquery"
import "golang.org/x/net/context"


func main() {
    ctx := context.Background()
    client, err := bigquery.NewClient(ctx, "project-name")
    if err != nil {
        fmt.Println("Had an error")
    }

    q := client.Query(`
        select * from mytables
    `)

    it, err := q.Read(ctx)

    fmt.Println(it)
    fmt.Println(err)
}

我得到了代码

googleapi: Error 403: Access Denied: Project project-name:

如何将服务密钥文件传递到客户端?

go google-bigquery google-cloud-platform
1个回答
1
投票

您需要在项目中创建服务帐户并提供新的JSON私钥。

然后确保将GOOGLE_APPLICATION_CREDENTIALS环境变量设置为此文件的完整路径。例如,在Linux上:

GOOGLE_APPLICATION_CREDENTIALS=/home/user/Downloads/key.json go run main.go

另外,最佳做法是确保您保护对此私钥文件的访问,并确保服务帐户具有执行其角色所需的最低权限。

© www.soinside.com 2019 - 2024. All rights reserved.