1

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

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

用例

我构建了一个烧瓶应用程序(根据fsouza的工作),我尝试为烧瓶构建一个缩略图扩展,它将在 gae 上工作(因为它是一个只读 FS,我分叉了烧瓶缩略图,目前正在尝试扩展它。)

所以我需要:

  • 通过 python 访问我的静态文件,这样我就可以读取 img 并即时制作缩略图。网址是例如。/静态/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 应用程序将无法访问静态图像。您必须通过应用程序引擎启动器部署它(仅此一项就花了我很长时间才发现)

4

2 回答 2

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案例:)

于 2014-12-21T10:05:07.420 回答
0

您正在寻找的答案在使用 app.yaml 配置文档的静态目录处理程序部分中。

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

更新了一个工作示例

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

应用程序.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

主文件

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)])

目录 1/文件 1.txt

Content1

目录2/文件2.txt

Content2

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

(我在 Linux 上使用 GAE SDK 1.9.17)

于 2014-12-20T18:03:11.293 回答