GAE 运行 Python 脚本和 GCE 权限
GAE Running Python script and GCE permissions
GAE 是否需要某种 WSGI (https://cloud.google.com/appengine/docs/python/tools/webapp/running)?这像 HTTPD 的 CGI 配置吗?
即,在 app.yaml
中我必须有 script.app
并引用 app
到 wsgi/webapp 对象?
正在尝试使用 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 快照。
我不明白如何为我的 POST
引用 compute
对象,以使身份验证工作(现在未授权)。
这是我的脚本(由于测试太多 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)
1) 据我所知,WSGI 对象实际上是必需的,因为它是应用程序与服务器环境通信的方式。使用 Django、Flask、webapp2 或其他框架可以轻松获得此对象,因此获得它应该不会太难。看看:
https://github.com/GoogleCloudPlatform/python-docs-samples/tree/master/appengine
大量 GAE 配置示例。
2) 现在建议使用应用程序默认凭据,而不是 AppAssertion 凭据。
https://developers.google.com/identity/protocols/application-default-credentials
使用它更容易,并且可以无缝地在 GAE、GCE(假设您创建了具有正确范围的实例)、MVM 上工作。它也适用于您的本地环境,使用您从 gcloud init
获得的默认 "user" 帐户,或者您可以将 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)
这可能不是确切的语法,如果您不能让它工作,请发表评论,我会尝试修复它。
GAE 是否需要某种 WSGI (https://cloud.google.com/appengine/docs/python/tools/webapp/running)?这像 HTTPD 的 CGI 配置吗? 即,在
app.yaml
中我必须有script.app
并引用app
到 wsgi/webapp 对象?正在尝试使用
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 快照。
我不明白如何为我的 POST
引用 compute
对象,以使身份验证工作(现在未授权)。
这是我的脚本(由于测试太多 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)
1) 据我所知,WSGI 对象实际上是必需的,因为它是应用程序与服务器环境通信的方式。使用 Django、Flask、webapp2 或其他框架可以轻松获得此对象,因此获得它应该不会太难。看看:
https://github.com/GoogleCloudPlatform/python-docs-samples/tree/master/appengine
大量 GAE 配置示例。
2) 现在建议使用应用程序默认凭据,而不是 AppAssertion 凭据。
https://developers.google.com/identity/protocols/application-default-credentials
使用它更容易,并且可以无缝地在 GAE、GCE(假设您创建了具有正确范围的实例)、MVM 上工作。它也适用于您的本地环境,使用您从 gcloud init
获得的默认 "user" 帐户,或者您可以将 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)
这可能不是确切的语法,如果您不能让它工作,请发表评论,我会尝试修复它。