简体   繁体   中英

Use Google Cloud Storage API with 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,"\n","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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM