在 Appengine 上将 Google 云存储与 Golang 结合使用

Using Google Cloud Storage with Golang on Appengine

我一直在努力理解如何使用 Go 在 App Engine 上正确使用 GCS。我读过一些堆栈帖子,提到 Go App Engine Development Server 不模拟 GCS。 情况仍然如此吗?当我使用以下代码构建我的客户端时,我已经能够将对象存储到我的实际默认 GCS 中:

ctx := appengine.NewContext(r)
    jsonKey, err := ioutil.ReadFile("path/to/json.json")
    if err != nil {
        log.Fatal(err)
    }
    conf, err := google.JWTConfigFromJSON(
        jsonKey,
        storage.ScopeFullControl,
    )
    if err != nil {
        log.Fatal(err)
    }
    client, err := storage.NewClient(ctx, cloud.WithTokenSource(conf.TokenSource(ctx)))

这是在 App Engine 上使用云存储的首选方式吗?

我问是因为我很困惑为什么一些示例应用程序引擎代码不能正常工作。例如,我试图遵循此示例中的代码 (https://github.com/GoogleCloudPlatform/gcloud-golang/blob/master/examples/storage/appengine/app.go#L73)。您会注意到构建存储客户端仅使用

client, err := storage.NewClient(ctx)

使用此示例代码时出现 googleapi: Error 401: Invalid Credentials, authError 错误。这是错误还是我做错了什么?我应该通过哪种方式从 Go 应用引擎应用程序访问云存储?

我也很难让 GCS 与 App Engine 一起工作。我在使用 PEM 和 JSON 键时遇到了冲突的文档。我把 this presentation 放在一起讨论这个问题并教人们如何将 GCS 与 App Engine 一起使用。

我能够让 GCS 与 App Engine 一起工作。这里是 working GCS app engine code.

我还做了一个详细介绍所有这些的演讲 - this playlist 中有两三个视频详细介绍了 GCS 和应用程序引擎。