Comments (6)
what you hae there and the part below should work locally and the same remotely if using getServiceAccount()
reads the json file....
but is there a specific reason you want to use a json cert file within the cloud fn?
(GCF has its own service account identity which you can use by default)
package main
import (
"io/ioutil"
"log"
"cloud.google.com/go/bigquery"
"golang.org/x/net/context"
"golang.org/x/oauth2/google"
"google.golang.org/api/iterator"
"google.golang.org/api/option"
)
func main() {
serviceAccountJSONFile := "json_cert_file.json"
dat, err := ioutil.ReadFile(serviceAccountJSONFile)
if err != nil {
log.Fatalf("Unable to read service account file %v", err)
}
conf, err := google.JWTConfigFromJSON(dat, bigquery.Scope)
if err != nil {
log.Fatalf("Unable to acquire generate config: %v", err)
}
src := conf.TokenSource(context.Background())
ctx := context.Background()
client, err := bigquery.NewClient(ctx, "project2",option.WithTokenSource(src))
if err != nil {
log.Fatalf("Unable to acquire storage Client: %v", err)
}
ts := client.Dataset("dataset1").Tables(ctx)
for {
t, err := ts.Next()
if err == iterator.Done {
break
}
if err != nil {
log.Fatalf("Unable to acquire storage Client: %v", err)
}
log.Printf("Table: %q\n", t.TableID)
}
}
$ go run src/main.go
2017/09/05 10:51:52 Unable to acquire storage Client: googleapi: Error 403: Access Denied: Dataset project2:table1: The user [email protected] does not have bigquery.tables.list permission for dataset project2:table1., accessDenied
exit status 1
then add Bigquery Data viewer role to the remote project2 for this service account
$ go run src/main.go
2017/09/05 10:52:37 Table: "customers"
from cloud-functions-go.
You should also be able to get credentials from the metadata service:
https://cloud.google.com/compute/docs/storing-retrieving-metadata
from cloud-functions-go.
You don't have to call the metadata service explicitly either....the default clients would pick up creds from GCF's metadata server.
see
from cloud-functions-go.
@salrashid123, metadata.google.internal
is GCF's metadata server.
from cloud-functions-go.
yeah, what i was stating by that is you don't have to explicitly interrogate the metadata server to get a token (ADC will go down the list to seek it out for you automatically)
from cloud-functions-go.
thanks I have re-code following your links and it works perfectly paste my code to share with others. I my opinion al realted with permission, credentials etc is very very powerfull but need more examples real howto. When you see the end solution is easy the dificulty is found it.
ctx := context.TODO()
ts, err := google.DefaultTokenSource(ctx, bigquery.Scope)
if err != nil {
// Handle error.
}
client, err := bigquery.NewClient(ctx, "xxxx", option.WithTokenSource(ts))
if err != nil {
log.Fatalf("Unable get Client: %v", err)
}
q := client.Query(QUERY)
from cloud-functions-go.
Related Issues (20)
- about function-go HOT 6
- How to log to GCP Stackdriver? HOT 18
- currently only allow one main.go HOT 3
- there are any update? HOT 6
- Function always dies when issuing HTTP(S)-Requests with nodego.OverrideLogger() HOT 3
- Deployment failure: Operation interrupted. HOT 24
- Error: could not handle the request HOT 10
- NodeJS, PassportJS or JWT in Google Cloud Functions HOT 1
- Official Golang support plans? HOT 6
- Can't deploy function to Google Cloud Platform HOT 6
- How to deploy function to local emulator? HOT 2
- PubSub trigger `json: cannot unmarshal object` HOT 6
- Can't get this package. HOT 3
- How to use with a web server framework HOT 4
- Deployment issues HOT 9
- Official Go support is in public beta
- Why vendor folder is ignored (go project) HOT 1
- Deployment failure without invisibility! HOT 2
- Use Real Cloud Functions Go Runtime, Not Fake One HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from cloud-functions-go.