gpt4 book ai didi

Golang GCP 存储客户端内存泄漏

转载 作者:行者123 更新时间:2023-12-01 22:14:58 25 4
gpt4 key购买 nike

我在 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 会导致内存泄漏(至少上传有效)

最佳答案

我不知道您如何创建 HttpClient。在任何情况下,如果您指定您的 HTTPClient,it is used and this one by default not

但是,使用的传输层是您的 HTTPClient 之一。默认情况下,它不嵌入库默认创建的安全层(并且使用您的凭据)。您必须在 HTTPClient 中重现此逻辑。

因此,当您调用端点时,您调用它时没有任何安全 header ,因此,您的 401 错误是正常的。

关于Golang GCP 存储客户端内存泄漏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60964677/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com