我正在使用App Engine编写新的应用程序,并且正如文档建议不使用Blobstore API,我正在使用Google云端存储客户端(GCS).一切都很好,但我希望能够将"签名的URL"返回给客户,这样他们就可以获得GCS资源而无需通过应用程序.我相信这是signet网址的用途.
但是如何测试呢?我可以成功地从客户端测试GCS调用,但我不知道如何使用urlfetch测试客户端的HTTP调用.
以下是一个完整的测试用例,说明了我的问题:
import base64
import mimetypes
import urllib
import urllib2
from datetime import datetime, timedelta
import time
from google.appengine.api import app_identity
from google.appengine.datastore import datastore_stub_util
from google.appengine.ext import testbed
from google.appengine.ext import ndb
import unittest
import cloudstorage
# IS THIS RIGHT ?
GCS_API_ACCESS_ENDPOINT = 'http://localhost:8000/_ah/gcs'
def sign_url(bucket_object, expires_after_seconds=60):
""" cloudstorage signed url to download cloudstorage object without login
Docs : https://cloud.google.com/storage/docs/access-control?hl=bg#Signed-URLs
API : https://cloud.google.com/storage/docs/reference-methods?hl=bg#getobject
"""
# source: https://github.com/voscausa/appengine-gcs-signed-url/blob/05b8a93e2777679d40af62cc5ffce933216e6a85/sign_url.py
method = 'GET'
gcs_filename = urllib.quote(bucket_object)
content_md5, content_type = …
Run Code Online (Sandbox Code Playgroud) google-app-engine google-cloud-storage google-app-engine-python
我有一个 Python 应用程序,我想部署在 App Engine(第二代 Python 3.7)上,我在该应用程序上使用启用了域范围委派的服务帐户来访问用户数据。
在本地我这样做:
import google.auth
from apiclient.discovery import build
creds, project = google.auth.default(
scopes=['https://www.googleapis.com/auth/admin.directory.user', ],
)
creds = creds.with_subject(GSUITE_ADMIN_USER)
service = build('admin', 'directory_v1', credentials=creds)
Run Code Online (Sandbox Code Playgroud)
这很好用,据我所知,这是当前使用应用程序默认凭据时执行此操作的方法(在本地我定义了 GOOGLE_APPLICATION_CREDENTIALS)。
问题出在 GAE 上,部署后,调用会with_subject
引发:
AttributeError: 'Credentials' object has no attribute 'with_subject'
我已经在 GAE 服务帐户上启用了域范围的委派。
当我在本地使用的 GOOGLE_APPLICATION_CREDENTIALS 和 GAE 中的那些都是具有域范围委派的服务帐户时,它们之间有什么区别?
.with_subject()
GAE在哪里?
creds
收到的对象是 类型compute_engine.credentials.Credentials
。
完整追溯:
Traceback (most recent call last):
File "/env/lib/python3.7/site-packages/gunicorn/arbiter.py", line 583, in spawn_worker
worker.init_process()
File "/env/lib/python3.7/site-packages/gunicorn/workers/gthread.py", line 104, in init_process
super(ThreadWorker, …
Run Code Online (Sandbox Code Playgroud) python google-app-engine google-authentication google-api-python-client
我有一个非常简单的 python 函数:
def list_blobs(bucket, project)
storage_client = storage.Client(project=project)
bucket = storage_client.get_bucket(bucket)
blobs = bucket.list_blobs(prefix='basepath/', max_results=999999,
fields='items(name,md5Hash),nextPageToken')
r = [(b.name, b.md5_hash) for b in blobs]
Run Code Online (Sandbox Code Playgroud)
blob 列表包含 14599 个项目,此代码运行需要 7 秒。当进行分析时,大部分时间都浪费在从服务器读取数据上(有 16 次调用 page_iterator._next_page)。
那么,我该如何改进呢?迭代代码在库的深处,指向每个页面的指针都来自前一页,所以我看不出如何并行获取 16 个页面,这样我就可以减少这 7 秒。
我使用的是 python 3.6.8,
google-api-core==1.7.0
google-auth==1.6.2
google-cloud-core==0.29.1
google-cloud-storage==1.14.0
google-resumable-media==0.3.2
googleapis-common-protos==1.5.6
protobuf==3.6.1
Run Code Online (Sandbox Code Playgroud) python ×2