Google App Engine Python/Flask - 从 Google Cloud Storage 提供静态文件夹

m.w*_*325 6 python google-app-engine flask google-cloud-storage

我已经成功地将一个使用 Python 和 Flask 的网站部署到 Google App Engine 上,但是第一次加载页面时性能似乎很慢。我读到在 Cloud Storage 上托管一些静态文件可能会对此有所帮助,但我似乎无法让它工作。(www.example.com仅用于说明目的)

下面是 app.yaml 文件:

runtime: python27
api_version: 1
threadsafe: true
automatic_scaling:
  max_idle_instances: 2

handlers:
- url: /.*
  script:  main.app

env_variables:
    CLOUD_STORAGE_BUCKET: www.example.com\static

builtins:
- deferred: on
Run Code Online (Sandbox Code Playgroud)

我还创建了一个名为www.example.com的 Cloud Storage 存储桶,其中包含一个静态文件夹。我不知道我是否需要更改 jinja2 模板中的 url_for('static') 或者我需要做什么来解决这个问题。我不想上传到云存储只是想从那里指向我的网站资源。

任何帮助表示赞赏。顺便说一下,这是在标准环境中而不是 flex 上。

Pet*_*r W 0

假设您不固定使用云存储,但很乐意使用 Google App Engine 的内置静态文件服务功能,您可以app.yaml像这样编写文件\xe2\x80\xa6

\n
runtime: python39\n\nhandlers:\n  # This configures Google App Engine to serve the files in the app\'s static\n  # directory.\n- url: /static\n  static_dir: static\n\n  # This handler routes all requests not caught above to your main app. It is\n  # required when static routes are defined, but can be omitted (along with\n  # the entire handlers section) when there are no static files defined.\n- url: /.*\n  script: auto\n\n
Run Code Online (Sandbox Code Playgroud)\n

基于 Google 的示例:https://github.com/GoogleCloudPlatform/python-docs-samples/blob/edb0f178896b8d55a2f544b5fe6bbf7ed870a2e9/appengine/standard_python3/building-an-app/building-an-app-2/app.yaml

\n

(请注意,由于原来的问题已经存在多年了,我已经更新了 Python 的版本,并省略了一些 OP 的示例设置,这些设置对于本示例的工作来说是不必要的。)

\n