dtrt2368 2016-10-08 05:45
浏览 31
已采纳

将Google Cloud Storage API与Go AppEngine结合使用

I've written a test code to list buckets from Google Cloud Storage through the Cloud Storage API, however I'm getting the error below when I run the code:

googleapi: Error 403: Forbidden, forbiddenFinished

I've checked the permissions and the appengine service account has access to the buckets and both the appengine app and cloud storage bucket are on the same project.

This is my sample code:

package src

import (
    "fmt"
    "net/http"
    "golang.org/x/oauth2/google"
    storage "google.golang.org/api/storage/v1"
    appengine "google.golang.org/appengine"
)

func init() {
    http.HandleFunc("/", index)
}

func ListBuckets(r *http.Request, projectID string) ([]*storage.Bucket, error) {

    ctx := appengine.NewContext(r)

    client, err := google.DefaultClient(ctx, storage.DevstorageReadOnlyScope)
    if err != nil {
        return nil, err
    }

    service, err := storage.New(client)
    if err != nil {
        return nil, err
    }

    buckets, err := service.Buckets.List(projectID).Do()
    if err != nil {
        return nil, err
    }

    return buckets.Items, nil
}

func index(w http.ResponseWriter, r *http.Request) {
    r.Header.Set("x-goog-project-id", "theIdProvidedByTheAPI")
    bucket, err := ListBuckets(r,"myProject")
    if err != nil {
        fmt.Fprint(w,err.Error())
    }
    for i:=range bucket {
    fmt.Fprint(w,bucket[i].Name)
    }
    fmt.Fprint(w,"
","Finished.")

}

And this is the yaml file:

application: myProject
version: alpha-001
runtime: go
api_version: go1

handlers:

- url: /
  script: _go_app

The error message is nor helpful as it does not provide much useful information. I just can't figure out what I'm missing.

  • 写回答

1条回答 默认 最新

  • duanlu1793 2016-10-13 00:12
    关注

    I've had issues with the google.DefaultClient method in the past. Here's a more explicit strategy for configuring the storage client object that might be of use for you:

    httpClient = &http.Client{
        Transport: &oauth2.Transport{
            Source: google.AppEngineTokenSource(ctx, scopes...),
            Base:   &urlfetch.Transport{Context: ctx},
        },
    }
    
    service, err := storage.New(httpClient)
    if err != nil {
        return nil, err
    }
    

    However, I'm not familiar with the forbiddenFinished error message, which may indicate that the issue lies elsewhere.

    Additionally, if you don't have a specific reason for using the autogenerated google.golang.org/api/storage/v1 library, I'd recommend using the higher level interface, which can be found at cloud.google.com/go/storage. Here's the go doc for it:

    https://godoc.org/cloud.google.com/go/storage

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 微信公众号自制会员卡没有收款渠道啊
  • ¥15 stable diffusion
  • ¥100 Jenkins自动化部署—悬赏100元
  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘
  • ¥15 perl MISA分析p3_in脚本出错
  • ¥15 k8s部署jupyterlab,jupyterlab保存不了文件
  • ¥15 ubuntu虚拟机打包apk错误
  • ¥199 rust编程架构设计的方案 有偿