在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