在Appengine上使用Golang与Google Cloud Storage进行交互

huangapple go评论89阅读模式
英文:

Using Google Cloud Storage with Golang on Appengine

问题

我一直在努力理解如何在使用Go的App Engine上正确使用GCS。我读过一些堆栈帖子,提到Go App Engine开发服务器不会模拟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上使用云存储的首选方式吗?

我之所以问这个问题,是因为我对为什么一些示例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的App Engine应用程序中访问云存储?

英文:

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

我在使用应用引擎时也遇到了与GCS配合使用的困难。在使用PEM和JSON密钥之间,我遇到了冲突的文档。我整理了这个演示文稿来讨论这个问题,并教人们如何在应用引擎中使用GCS。

我成功地将GCS与应用引擎配合使用。这是可工作的GCS应用引擎代码

我还进行了一次详细的演讲,其中有两到三个视频在这个播放列表中详细介绍了GCS和应用引擎。

英文:

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.

huangapple
  • 本文由 发表于 2015年12月24日 12:09:10
  • 转载请务必保留本文链接:https://go.coder-hub.com/34447124.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定