【发布时间】:2020-04-01 06:11:20
【问题描述】:
我在 github https://github.com/googleapis/google-cloud-go/issues/1025 上阅读了以下问题,发现创建的空闲连接存在内存泄漏,因此将 http 客户端添加到 ClientOption 就可以了。但是我想使用 'WithCredentialsFile' 选项来实现它,但根据文档,'WithHTTPClient' 不兼容。
以下是我的方法:
client, err := storage.NewClient(ctx, option.WithCredentialsFile(cred), option.WithHTTPClient(httpClient))
if err != nil {
return err
}
defer client.Close()
而且我总是收到以下错误:
"googleapi: Error 401: Anonymous caller does not have storage.objects.create access to <filepath>, required"
有什么解决办法吗?
更新: 我在调用 newClient 时尝试了以下方法来摆脱 withCred
os.Setenv("GOOGLE_APPLICATION_CREDENTIALS", cre)
client, err := storage.NewClient(ctx, option.WithHTTPClient(httpClient))
if err != nil {
return err
}
defer client.Close()
但它仍然导致上述错误。如果我尝试删除 withHttp 会导致内存泄漏(至少上传有效)
【问题讨论】:
-
如果我理解得很好,你想同时使用 withCred 和 WithHttp 但它不起作用。我对吗?如果是这样,您的代码在哪里运行?在 GCP 环境中?
-
@guillaumeblaquiere 是的,这就是我的方法。 WithHttp 取消了 withCred。此外,该服务确实在 GCP 环境中运行。
-
好的,如果你在GCP环境下运行,解决方案很简单:你不需要凭证文件!!使用底层服务的默认标识。
-
@guillaumeblaquiere 您能否提供一个链接或示例以供参考?我不太明白。谢谢
标签: go google-cloud-platform google-cloud-storage