2014-09-25 111 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 
相关问题