Using Google Cloud Storage with Golang on Appengin

2019-05-10 17:27发布

I have been struggling with understanding how to properly use GCS on the App Engine using Go. I've read a few stack posts mentioning that the Go App Engine Development Server does not emulate the GCS. Is this still the case? I have been able to store objects to my actual default GCS when I construct my client using this code:

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)))

Is this the preferred way of working with cloud storage on the App Engine?

I ask because I'm confused why some of the sample app engine code does not work properly. For instance, I was attempting to follow the code in this example (https://github.com/GoogleCloudPlatform/gcloud-golang/blob/master/examples/storage/appengine/app.go#L73). You'll notice that building the storage client simply uses

client, err := storage.NewClient(ctx)

Using this sample code I get a googleapi: Error 401: Invalid Credentials, authError error. Is this an error or am I doing something wrong? Which way should I be accessing cloud storage from a Go app engine application?

1条回答
相关推荐>>
2楼-- · 2019-05-10 18:01

I struggled getting GCS to work with app engine also. I encountered conflicting documentation between using PEM and JSON keys. I put this presentation together to discuss this issue and teach people how to use GCS with app engine.

I was able to get GCS working with app engine. Here is working GCS app engine code.

I also gave a talk detailing all of this - there are two or three videos in this playlist detailing GCS and app engine.

查看更多
登录 后发表回答