Google 带有云存储的 App Engine 无法在本地运行
Google App Engine with Cloud Storage not Working Locally
我是 运行 Google Appengine 上的一个 Wordpress 站点。它使用 wordpress 的 GAE 插件。媒体库在 appengine 服务器上工作,但不在本地工作。大多数图像都一样,除非它们有硬编码 links。我收到大量 404 错误...
http://localhost:8080/_ah/gcs/<BUCKET_NAME>/image.png Failed to load resource: the server responded with a status of 404 (Not Found)
这里 link 在 gae 服务器上工作:
http://<BUCKET_NAME>.storage.googleapis.com/image.png
我在本地 运行 我的应用程序是这样的:
dev_appserver.py --php_executable_path=/Applications/GoogleAppEngineLauncher.app/Contents/Resources/GoogleAppEngine-default.bundle/Contents/Resources/php55/php-cgi .
似乎 google 的 python 脚本没有正确地将 link 转发到实际的真实存储桶....有什么想法吗?
这是预期的行为:
link http://<BUCKET_NAME>.storage.googleapis.com/image.png
是生产 link,请求由生产服务器提供。
link http://localhost:8080/_ah/gcs/<BUCKET_NAME>/image.png
是本地link,devserver 必须实际包含<BUCKET_NAME>/image.png
。该对象很可能只存储在生产环境中,当试图在本地机器上找到它时会导致 404。
作为一种解决方案,您可以考虑简单地 运行 针对生产云存储进行本地测试,以节省将所有存储项目传输(并保持同步)到本地的成本,这几乎与这种更简单的替代方案相比,网络传输和开发时间的成本肯定更高。
不确定这是否是同一个问题,但我发现来自 dev_appserver.py 的所有 gcs 请求都被路由到
http://localhost:8080/_ah/gcs/<BUCKET_NAME>/<OBJECT_NAME>
... 相对于 "real" gcs url,例如
https://www.googleapis.com/storage/v1/b/<BUCKET_NAME>/<OBJECT_NAME>
...因此在对 gcs 对象发出 GET 请求时导致 404 错误。
TLDR
我可以通过简单地设置访问令牌来解决这个问题,例如
cloudstorage.common.set_access_token("<TOKEN>")
*请参阅下面的 set_access_token
文档字符串,了解如何获取访问令牌。
一旦我这样做了,我所有的 gcs 请求都被正确路由。
详情
在深入了解 appengine-gcs-client
的 source code 之后,如果您想使用 dev_appserver 访问 live/remote 内容,您似乎必须先设置访问令牌在 gcs.
def set_access_token(access_token):
"""Set the shared access token to authenticate with Google Cloud Storage.
When set, the library will always attempt to communicate with the
real Google Cloud Storage with this token even when running on dev appserver.
Note the token could expire so it's up to you to renew it.
When absent, the library will automatically request and refresh a token
on appserver, or when on dev appserver, talk to a Google Cloud Storage
stub.
Args:
access_token: you can get one by run 'gsutil -d ls' and copy the
str after 'Bearer'.
"""
cloudstorage.storage_api 中的一些附加 hints/options:
def _get_storage_api(retry_params, account_id=None):
"""Returns storage_api instance for API methods.
Args:
retry_params: An instance of api_utils.RetryParams. If none,
thread's default will be used.
account_id: Internal-use only.
Returns:
A storage_api instance to handle urlfetch work to GCS.
On dev appserver, this instance will talk to a local stub by default.
However, if you pass the arguments --appidentity_email_address and
--appidentity_private_key_path to dev_appserver.py it will attempt to use
the real GCS with these credentials. Alternatively, you can set a specific
access token with common.set_access_token. You can also pass
--default_gcs_bucket_name to set the default bucket.
"""
杂项信息
pip 库
google-api-python-client==1.6.4
GoogleAppEngineCloudStorageClient==1.9.22.1
gcloud 库
Google Cloud SDK 200.0.0
alpha 2018.04.30
app-engine-python 1.9.69
app-engine-python-extras 1.9.69
beta 2018.04.30
bq 2.0.33
cloud-datastore-emulator 1.4.1
core 2018.04.30
gsutil 4.31
我是 运行 Google Appengine 上的一个 Wordpress 站点。它使用 wordpress 的 GAE 插件。媒体库在 appengine 服务器上工作,但不在本地工作。大多数图像都一样,除非它们有硬编码 links。我收到大量 404 错误...
http://localhost:8080/_ah/gcs/<BUCKET_NAME>/image.png Failed to load resource: the server responded with a status of 404 (Not Found)
这里 link 在 gae 服务器上工作:
http://<BUCKET_NAME>.storage.googleapis.com/image.png
我在本地 运行 我的应用程序是这样的:
dev_appserver.py --php_executable_path=/Applications/GoogleAppEngineLauncher.app/Contents/Resources/GoogleAppEngine-default.bundle/Contents/Resources/php55/php-cgi .
似乎 google 的 python 脚本没有正确地将 link 转发到实际的真实存储桶....有什么想法吗?
这是预期的行为:
link http://<BUCKET_NAME>.storage.googleapis.com/image.png
是生产 link,请求由生产服务器提供。
link http://localhost:8080/_ah/gcs/<BUCKET_NAME>/image.png
是本地link,devserver 必须实际包含<BUCKET_NAME>/image.png
。该对象很可能只存储在生产环境中,当试图在本地机器上找到它时会导致 404。
作为一种解决方案,您可以考虑简单地 运行 针对生产云存储进行本地测试,以节省将所有存储项目传输(并保持同步)到本地的成本,这几乎与这种更简单的替代方案相比,网络传输和开发时间的成本肯定更高。
不确定这是否是同一个问题,但我发现来自 dev_appserver.py 的所有 gcs 请求都被路由到
http://localhost:8080/_ah/gcs/<BUCKET_NAME>/<OBJECT_NAME>
... 相对于 "real" gcs url,例如
https://www.googleapis.com/storage/v1/b/<BUCKET_NAME>/<OBJECT_NAME>
...因此在对 gcs 对象发出 GET 请求时导致 404 错误。
TLDR
我可以通过简单地设置访问令牌来解决这个问题,例如
cloudstorage.common.set_access_token("<TOKEN>")
*请参阅下面的 set_access_token
文档字符串,了解如何获取访问令牌。
一旦我这样做了,我所有的 gcs 请求都被正确路由。
详情
在深入了解 appengine-gcs-client
的 source code 之后,如果您想使用 dev_appserver 访问 live/remote 内容,您似乎必须先设置访问令牌在 gcs.
def set_access_token(access_token):
"""Set the shared access token to authenticate with Google Cloud Storage.
When set, the library will always attempt to communicate with the
real Google Cloud Storage with this token even when running on dev appserver.
Note the token could expire so it's up to you to renew it.
When absent, the library will automatically request and refresh a token
on appserver, or when on dev appserver, talk to a Google Cloud Storage
stub.
Args:
access_token: you can get one by run 'gsutil -d ls' and copy the
str after 'Bearer'.
"""
cloudstorage.storage_api 中的一些附加 hints/options:
def _get_storage_api(retry_params, account_id=None):
"""Returns storage_api instance for API methods.
Args:
retry_params: An instance of api_utils.RetryParams. If none,
thread's default will be used.
account_id: Internal-use only.
Returns:
A storage_api instance to handle urlfetch work to GCS.
On dev appserver, this instance will talk to a local stub by default.
However, if you pass the arguments --appidentity_email_address and
--appidentity_private_key_path to dev_appserver.py it will attempt to use
the real GCS with these credentials. Alternatively, you can set a specific
access token with common.set_access_token. You can also pass
--default_gcs_bucket_name to set the default bucket.
"""
杂项信息
pip 库
google-api-python-client==1.6.4
GoogleAppEngineCloudStorageClient==1.9.22.1
gcloud 库
Google Cloud SDK 200.0.0
alpha 2018.04.30
app-engine-python 1.9.69
app-engine-python-extras 1.9.69
beta 2018.04.30
bq 2.0.33
cloud-datastore-emulator 1.4.1
core 2018.04.30
gsutil 4.31