2014-09-25 116 views

回答

2

確保您的實例具有範圍t o首先訪問BigQuery - 您只能在創建時決定這一點。

在bash腳本

,通過調用得到的OAuth令牌:

ACCESSTOKEN=`curl -s "http://metadata/computeMetadata/v1/instance/service-accounts/default/token" -H "X-Google-Metadata-Request: True" | jq ".access_token" | sed 's/"//g'` 
echo "retrieved access token $ACCESSTOKEN" 

現在讓我們假設你想要的數據組中的一個項目列表:

CURL_URL="https://www.googleapis.com/bigquery/v2/projects/YOURPROJECTID/datasets" 
CURL_OPTIONS="-s --header 'Content-Type: application/json' --header 'Authorization: OAuth $ACCESSTOKEN' --header 'x-goog-project-id:YOURPROJECTID' --header 'x-goog-api-version:1'" 
CURL_COMMAND="curl --request GET $CURL_URL $CURL_OPTIONS"  
CURL_RESPONSE=`eval $CURL_COMMAND` 

JSON格式的響應可以在變量CURL_RESPONSE中找到

PS:我現在意識到這個問題被標記爲Python,但同樣的原則適用。

+0

感謝您的回答,我添加了bash和curl標籤以保證適當性:) – 2014-09-27 21:47:33

3

在Python:

AppAssertionCredentials是一個python類,允許計算引擎實例將自身標識爲谷歌和其它的OAuth 2.0服務器,全無需要流動。

https://developers.google.com/api-client-library/python/

項目ID可以從元數據服務器讀取,所以它並不需要設置爲一個變量。

https://cloud.google.com/compute/docs/metadata

以下代碼獲取令牌使用AppAssertionCredentials,從元數據服務器的項目編號,並實例化一個BigqueryClient與此數據:

import bigquery_client 
import urllib2 
from oauth2client import gce 

def GetMetadata(path): 
    return urllib2.urlopen(
     'http://metadata/computeMetadata/v1/%s' % path, 
     headers={'Metadata-Flavor': 'Google'} 
    ).read() 

credentials = gce.AppAssertionCredentials(
    scope='https://www.googleapis.com/auth/bigquery') 

client = bigquery_client.BigqueryClient(
    credentials=credentials, 
    api='https://www.googleapis.com', 
    api_version='v2', 
    project_id=GetMetadata('project/project-id')) 

對於這個工作,你需要給創建它時,GCE實例訪問BigQuery API:

gcloud compute instances create <your_instance_name> --scopes storage-ro bigquery 
相關問題