【问题标题】:APP.YAML: Combine different URLs-same file, static file serving & source code access in app.yamlAPP.YAML:在 app.yaml 中组合不同的 URL——同一个文件、静态文件服务和源代码访问
【发布时间】:2015-02-19 08:51:12
【问题描述】:

我现在有很多关于app.yaml的问题,找了又找,但是没找到这个。

TLDR:请先阅读完整答案,这不是您的标准 application_readable:true。 我基本上想通过不同的路径访问同一个文件,例如/static/img/pic.jpg 和 /img/pic.jpg

用例

我构建了一个烧瓶应用程序(基于 fsouza 的工作),我尝试构建一个适用于 gae 的烧瓶缩略图扩展(因为它是一个只读 FS,我分叉了 flask-thumbnails 并且目前尝试展开它。)

所以我需要:

  • 通过 python 访问我的静态文件,以便我可以读取 img 并即时制作缩略图。 网址是例如。 /STATIC/IMG/PIC.JPG
  • 仍然通过 app.yaml 传递其他图片、css、js。 网址是例如。 /IMG/PIC.JPG

什么不工作:

它在本地工作,部署后将无法工作。我认为 app.yaml 并没有像应该的那样被 dev_appserver.py 严格执行。

我可以让其中一种方案发挥作用。这是我的 app.yaml 目前的样子:

builtins:
- appstats: on
- admin_redirect: on
- deferred: on
- remote_api: on

- url: /css
  static_dir: application/static/css 

- url: /js
  static_dir: application/static/js 

- url: /img
  static_dir: application/static/img

- url: /static
  static_dir: application/static
  application_readable: true

- url: .*
  script: run.application.app

我也试过这个:

- url: /css/(.*)
  static_files: css/\1
  upload: css/(.*) 

- url: /js/(.*)
  static_files: js/\1
  upload: js/(.*) 

- url: /img/(.*)
  static_files: img/\1
  upload: img/(.*) 

当我注释掉特定的 js,css,img 内容时,应用程序可以访问 application/static/img 中的 img 并从中制作缩略图。但是带有例如的网址不会提供 /img/dont.need.thumbnail.jpg。

当我评论这部分时:

- url: /static
  static_dir: application/static
  application_readable: true

img,css,js 得到应有的服务。

有人可以帮我吗?我做错了什么?

app.yaml url 是递归的吗?

当前解决方法:

我目前的解决方法是,我只需在 python 应用程序中添加几个 url 路由。但这效率不高,我怀疑它会花费我更多的 CPU 时间并且速度更慢。 例如

app.add_url_rule('/img/<path>', 'static_img_files', view_func=views.static_img_files)
def static_img_files(path):
    return static_files("img/"+path)

奖励提示:

如果你只是 git push-to-deploy

application_readable: true

不会工作,因此一旦您在 gae 服务器上测试它,python 应用程序就无法访问静态图像,而不再是本地。你必须通过应用引擎启动器来部署它(仅这一点我花了很长时间才发现)

【问题讨论】:

    标签: python google-app-engine app.yaml google-app-engine-python


    【解决方案1】:

    答案很简单:让每个访问路径都为 application_readable, 因为负面权限比正面权限强。

    - url: /img
      static_dir: application/static/img
      application_readable: true           # <---- !!!!!
    
    - url: /static
      static_dir: application/static
      application_readable: true
    

    所以有点吃我自己的话,这是 application_readable:true 的简单案例 :)

    【讨论】:

      【解决方案2】:

      您正在寻找的答案在Configuring with app.yaml 文档的Static directory handlers 部分。

      寻找application_readable。将该属性设置为 true 可以让您两全其美,但会牺牲配额(因为以这种方式标记的静态文件需要上传到两个不同的地方)。

      更新了一个工作示例

      我已将其简化为基本要素。

      app.yaml

      application: example
      version: 1
      runtime: python27
      api_version: 1
      threadsafe: true
      
      handlers:
      - url: /dir1
        static_dir: dir1
        mime_type: text/plain
      
      - url: /dir2
        static_dir: dir2
        mime_type: text/plain
        application_readable: true
      
      - url: .*
        script: main.app
      

      main.py

      import webapp2
      
      class Test(webapp2.RequestHandler):
        def get(self):
          self.response.headers['Content-Type'] = 'text/plain'
          for path in ['dir1/file1.txt', 'dir2/file2.txt']:
            try:
              with open(path) as f:
                self.response.out.write(f.read())
            except IOError:
              pass
      
      app = webapp2.WSGIApplication([(r'/', Test)])
      

      dir1/file1.txt

      Content1
      

      dir2/file2.txt

      Content2
      

      您应该能够导航到 /dir1/file1.txtdir2/file2.txt 并查看它们的内容,但导航到 / 只能看到后一个文件。为了简单起见,我使用文本文件而不是图像;那个细节应该不重要。

      (我在 Linux 上使用 GAE SDK 1.9.17)

      【讨论】:

      • 您好,首先感谢您的回答。但正如我在问题中所写的那样,我已经在使用它。我想要的是 /static/img/pic.jpg 可以由应用程序和客户端访问,并且 /img/pic.jpg 也可以访问(仅由客户端/浏览器)。你能帮我解决这个用例吗?
      • 这是一个非常简单的工作示例。你可以从这个开始,首先验证它是否有效,然后慢慢地将它弯曲到你想要的方向。如果它在某个时候中断,请仔细查看您的最后一步。
      • 感谢您承诺回答,但请阅读,您的问题。我有一个不同的用例:我基本上想通过不同的路由访问同一个文件,例如您的示例 /static/dir1/file.txt (应用程序和客户端可访问)和 /dir1/file.txt (仅限客户端)。两条路线中的 static_dir 将指向同一目录:例如。静态/dir1
      • 谢谢你的推动,在思考如何把我的问题说清楚的同时,我想到了我的问题的答案。如果我想通过不同的路由访问同一个文件,但不将 application_readable:true 设置为每条路由,它将无法正常工作,因为负面的访问权限会覆盖正面的访问权限
      猜你喜欢
      • 2022-01-03
      • 2013-03-28
      • 2015-12-03
      • 1970-01-01
      • 2017-08-08
      • 2012-11-27
      • 2011-07-01
      • 2011-07-11
      • 1970-01-01
      相关资源
      最近更新 更多