在Google Compute Engine实例上验证Google BigQuery的最简单方法是什么?如何在Google Compute Engine上验证BigQuery?
4
A
回答
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,但同样的原则适用。
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
相关问题
- 1. Google Compute Engine GPU
- 2. Google Compute Engine上的Spark SQL
- 3. 在Google Compute Engine的Tomcat上启用SSL
- 4. 在Google Compute Engine上成功安装Oryx?
- 5. 在Google Compute Engine上添加Secundary IP
- 6. 'Google App Engine'远比'Google Compute Engine'贵吗?
- 7. Google App Engine +验证
- 8. Google Compute Engine Windows RDP登录
- 9. 如何随时记录Google Compute Engine上的RAM使用情况?
- 10. Google App Engine身份验证
- 11. Google App Engine身份验证
- 12. 启用Google Container Engine API不会自动启用Google Compute Engine API
- 13. 如何获取我在Google Compute Engine中生成的SSH密钥?
- 14. Google Compute Engine - 删除帐户+资源
- 15. Google Compute Engine负载平衡器限制
- 16. Google Compute Engine虚拟机备份策略
- 17. 在Google Compute Engine上使用gcloud安装node.js
- 18. 是否可以在Google Compute Engine上为Kubernetes设置Stackdriver Monitoring?
- 19. 在Google Compute Engine实例上自定义Java堆大小
- 20. yii-node套接字无法在Google Compute Engine上工作
- 21. 使用Google Compute Engine在nodejs上加载测试瓶颈
- 22. 在Google Compute Engine的Ubuntu上安装可扩展服务代理
- 23. 通过Google Compute Engine从Jenkins构建的认证范围不足
- 24. 如何在Google Compute Engine上为高可用性Kubernetes启用领导者选举?
- 25. Google云中的Google Compute Engine,App Engine和Container Engine有什么区别?
- 26. Google Compute和OpenGL?
- 27. Google Compute Engine:如何禁用VM实例的防火墙?
- 28. Google Compute Engine的根磁盘大小如何减少?
- 29. 如何将实例从Google Compute Engine导出到其他云
- 30. 如何使用GUI连接到Google Compute Engine虚拟服务器?
感谢您的回答,我添加了bash和curl标签以保证适当性:) – 2014-09-27 21:47:33