【问题标题】:Golang GCP Storage Client Memory LeakGolang GCP 存储客户端内存泄漏
【发布时间】: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


【解决方案1】:

我不知道您是如何创建 HttpClient 的。无论如何,如果你指定你的 HTTPClient,it is used and this one by default not

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

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

【讨论】:

    【解决方案2】:

    我相信这将允许您使用自己的 HTTP 客户端。

        import {
           gcpTransport "google.golang.org/api/transport/http"
        }
    
        ...
    
        httpClient.Transport, err = gcpTransport.NewTransport(ctx, option.WithCredentialsFile(cred))
        if err != nil {
            return err
        }
        client, err := storage.NewClient(ctx, option.WithHTTPClient(httpClient))
        if err != nil {
            return err
        }
        defer client.Close()
    

    【讨论】:

      猜你喜欢
      • 2019-05-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多