【问题标题】:How can I programmatically create a Google Cloud Function using the Go API client如何使用 Go API 客户端以编程方式创建 Google Cloud Function
【发布时间】:2019-06-24 04:04:40
【问题描述】:

有一个用于与 Cloud Functions API (google.golang.org/api/cloudfunctions/v1) 交互的 Go 包,但我不知道如何使用它来创建新函数。我在尝试上传到 Cloud Storage 存储桶的签名 URL 时遇到 404 和 403 错误。

有人知道如何使用这个包来部署 Cloud Functions 吗?

【问题讨论】:

  • 请提供代码,您已经尝试过的解决方案。我只能复制和粘贴您的一般问题的文档。

标签: go google-cloud-platform google-cloud-functions


【解决方案1】:

我在使用 google.golang.org/api/cloudfunctions/v1 时遇到了类似的问题, 我遇到的第一个 403 错误问题是由于使用了身份验证客户端 使用预签名的生成上传 URL,使用裸 http 客户端帮助

httpClient := http.DefaultClient
data, err := ioutil.ReadAll(reader)
if err != nil {
    return err
}
request, err := http.NewRequest("PUT", uploadURL, bytes.NewReader(data))
if err != nil {
    return err
}
request.Header.Set("content-type", "application/zip")
request.Header.Set("x-goog-content-length-range", "0,104857600")
request.Header.Set("Content-Length", fmt.Sprintf("%d", len(data)))
response, err := httpClient.Do(request)
if err != nil {
    return err
}

我在 404 中看到的另一个问题是当我使用位置作为区域而不是下面 sn-p 中显示的完全限定名称时

var location =  'projects/${projectID}/locations/${region}'  
projectService := cloudfunctions.NewProjectsLocationsFunctionsService(ctxClient.service)
createCall := projectService.Create(location, request.CloudFunction)
createCall.Context(ctxClient.Context())
return createCall.Do()
h

您还可以查看本项目中的 golang 云函数 google.golang.org/api/cloudfunctions/v1 API 使用情况:

Cloud function service

【讨论】:

    猜你喜欢
    • 2018-02-18
    • 2010-12-11
    • 2017-03-30
    • 1970-01-01
    • 2017-07-13
    • 2018-09-08
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多