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 如何用Labview在myRIO上做LCD显示?(语言-开发语言)
  • ¥15 Vue3地图和异步函数使用
  • ¥15 C++ yoloV5改写遇到的问题
  • ¥20 win11修改中文用户名路径
  • ¥15 win2012磁盘空间不足,c盘正常,d盘无法写入
  • ¥15 用土力学知识进行土坡稳定性分析与挡土墙设计
  • ¥70 PlayWright在Java上连接CDP关联本地Chrome启动失败,貌似是Windows端口转发问题
  • ¥15 帮我写一个c++工程
  • ¥30 Eclipse官网打不开,官网首页进不去,显示无法访问此页面,求解决方法
  • ¥15 关于smbclient 库的使用