2015-12-21 71 views
0
  1. GAE是否需要某种WSGI(https://cloud.google.com/appengine/docs/python/tools/webapp/running)?这就像HTTPD的CGI配置吗? 即,在app.yaml我必须有script.app和参考app到一个wsgi/webapp对象?GAE运行Python脚本和GCE权限

  2. 试图使用AppAssertionCredentials从GAE到GCE进行身份验证。 我做了另一个脚本,该片段在工作:

credentials = AppAssertionCredentials( scope='https://www.googleapis.com/auth/compute') auth_http = credentials.authorize(httplib2.Http()) compute = discovery.build('compute', 'v1', http=auth_http)

我试图现在要做的就是创建一个使用REST API从GAE GCE快照。 我不明白如何引用我的POSTcompute对象,以便获得auth的工作(现在获得未授权)。

这是我的脚本(太多import原因可能会测试):

import requests 
import urllib2 
import logging 
import sys 
import argparse 
import httplib2 
from oauth2client.client import flow_from_clientsecrets 
from oauth2client.file import Storage 
from oauth2client import tools 
from oauth2client.tools import run_flow 
from oauth2client.appengine import AppAssertionCredentials 
from google.appengine.api import memcache 
import datetime 
import httplib2 
import json 
import logging 
from pprint import pformat 
from apiclient import discovery 
from google.appengine.api import memcache 
from oauth2client.appengine import AppAssertionCredentials 
import cgi 
from google.appengine.api import users 
import urllib 
from google.appengine.api import users 
from google.appengine.ext import ndb 
import time 

PROJECT = "testprojgce" 
ZONE = "europe-west1-b" 


### OAuth2 

credentials = AppAssertionCredentials(
scope='https://www.googleapis.com/auth/compute') 
auth_http = credentials.authorize(httplib2.Http()) 
compute = discovery.build('compute', 'v1', http=auth_http) 


# Create snapshot 
createsnapurl= "https://www.googleapis.com/compute/v1/projects/"+PROJECT+"/zones/"+ZONE+"/disks/testdisk1/createSnapshot" 
req=requests.post(createsnapurl) 
+0

请坚持“每个问题一个问题” - 这是非常基本的StackOverflow礼仪。仅关注问题1,是的:GAE的Web服务器的2.7 Python运行时通过您的WSGI应用程序(应用程序)连接到您的代码。yaml可以派发给其中的一个或多个),就像任何其他web服务器一样 - 任何您选择的任何Python框架都可以轻松实现这些(我建议您的用例是一个轻量级框架,例如猎鹰,烧瓶,瓶子,或webapp2,而不是像django或web2py这样的丰富而重的完整版本 - 当然,这是您的选择)。 –

+0

怀疑是否打开“这是一个2部分问题”或类似的东西。对不起,因为违反了礼仪。 – PyGAE

回答

2

1)据我所知,其实是在要求一个WSGI对象,因为它是如何应用到服务器环境连通。这个对象很容易使用Django,Flask,webapp2或其他框架,所以它不应该太难以得到。看一看:

https://github.com/GoogleCloudPlatform/python-docs-samples/tree/master/appengine

对于很多GAE配置样本。

2)现在推荐使用Application Default Credentials而不是AppAssertion凭据。

https://developers.google.com/identity/protocols/application-default-credentials

这是一个更容易的工作,并无缝地工作在GAE,GCE(假设你创建了合适的范围的情况下),MVMs。它也可以在您的本地环境中使用,您可以使用从gcloud init获得的默认“用户”帐户,也可以将GOOGLE_APPLICATION_CREDENTIALS环境变量指向JSON服务帐户凭据。在本地,我通常会建议下载一个JSON服务帐户并将该环境变量指向它,因为不是每个API都支持该用户帐户。

credentials = GoogleCredentials.get_application_default() 
compute_service = discovery.build(
    'compute', 'v1', credentials=credentials) 

注意你不需要指定范围,因为它会自动注入大多数API,但如果你丢失了一些范围有关的问题,请尝试使用“credentials.created_scoped”的方法。

最后,您几乎从不需要使用请求等直接与REST API进行交互,这是客户端库的主要观点。它确保您的http请求得到正确授权,并且您拥有语言级别的方法而不是URL字符串。相反,这样做:

jsonBody = { 
    #see request body section here for how to fill this in  #https://cloud.google.com/compute/docs/reference/latest/disks/createSnapshot 
} 
compute_service.disks().createSnapshot(project=project, zone=zone, disk=diskName, body=jsonBody) 

这可能不是确切的语法,发表评论,如果你不能得到它的工作,我会尝试修复它。

+0

比尔,非常感谢您的回复。显然客户端库方法是做事的首选方式,但我认为对于这种情况它们是相当有限的(列表,添加,删除,启动,停止 - https://cloud.google.com/compute/docs/tutorials/python -guide#nextsteps)。我没有看到任何'磁盘'方法。我假设需要'低级'API(https://cloud.google.com/compute/docs/reference/latest/disks/createSnapshot)。如果我错了,请纠正我。或者你的意思是我确实需要直接使用低级别的POST调用,但方式不同? – PyGAE

+0

@PyGAE,我遇到了类似的问题,但我还不确定。这可能是错误的发现文档被拉下来或错误的API版本的问题。如果你不能使用它们进行API调用,这肯定是客户端库的一个问题,你不应该使用请求。我正在圣诞假期,但当我回来时我会再看一次。 –

+0

我想我找到了。从API开发人员文档中并不那么直截了当。这里是'creatednapshot'方法(https://developers.google.com/resources/api-libraries/documentation/compute/v1/python/latest/compute_v1.disks.html#createSnapshot),这里是整个'disks' GCE的方法和客户端库方法 - https://developers.google.com/resources/api-libraries/documentation/compute/v1/python/latest/compute_v1.disks.html。我会尝试并报告回 – PyGAE