简体   繁体   English

无法在弹性beantalk上用瓶子加载静态文件

[英]Can't load static files with bottle on elastic beanstalk

After some help , I got my bottle application running with Elastic Beanstalk. 寻求帮助后 ,我使我的瓶子应用程序与Elastic Beanstalk一起运行。 Well almost - I still can't see any static files. 好吧,几乎-我仍然看不到任何静态文件。 I have followed the instructions here by inserting a file called python.conf in directory .ebextensions with this in it: 我按照此处的说明在目录.ebextensions中插入了一个名为python.conf的文件,并在其中插入了文件:

option_settings:
  "aws:elasticbeanstalk:container:python:staticfiles":
    "/static/": "static/"

Unfortunately that did not work. 不幸的是,这没有用。 My files are in folders under /static (for example, /static/js, /static/css etc). 我的文件位于/ static下的文件夹中(例如/ static / js,/ static / css等)。 In my bottle application, I was serving static files as per the answer here , like this: 在我的Bottle应用程序中,我按照此处的答案提供静态文件,如下所示:

@route('/static/:path#.+#')
def server_static(path):
    return static_file(path, root='./static')

It all works when I run it locally. 当我在本地运行时,所有这些都可以工作。 Does anyone know what I'm doing wrong? 有人知道我在做什么错吗? Should I be following a different procedure because my static files are in subfolders, or because I'm using bottle instead of flask? 是因为我的静态文件位于子文件夹中,还是因为我使用Bottle而不是flask,我应该遵循不同的步骤吗? I'm aware of a similar question that has been asked here for flask, but there has been no answer (and for all I know it could be a totally different issue). 我知道这里曾问过一个类似的烧瓶问题,但没有任何答案(就我所知,这可能是完全不同的问题)。 Thanks a lot, Alex 非常感谢Alex

I'm sure it isn't the most elegant solution, but I solved this by putting all of my static files in an S3 bucket and using that in all of my pages. 我确定这不是最优雅的解决方案,但我通过将所有静态文件放入S3存储桶并在我的所有页面中使用它来解决了这一问题。

I didn't really have that many static files to begin with so it wasn't a very big deal. 我真的没有那么多静态文件开始,所以这不是什么大问题。 I just made a variable of the S3 bucket url: 我只是对S3存储桶url做了一个变量:

S3Static = r'mystaticbucket.s3-us-west-2.amazonaws.com'

Passed it to my bottle templates, and changed the links from: 将其传递到我的瓶子模板,并更改了以下链接:

<link rel="stylesheet" type="text/css" href = "/static/css/MarmoStyle.css" >

to

<link rel="stylesheet" type="text/css" href = "{{S3Static}}/static/css/Style.css">

(ie just added {{S3Static}} before the path) If you're using a static file in a separate %included header template like I am, you have to pass the S3Static variable to the template like this: (即,仅在路径之前添加{{S3Static}})如果在像我一样的单独的%included头模板中使用静态文件,则必须将S3Static变量传递给模板,如下所示:

%include header.tpl S3Static=S3Static

And that was about it. 就是这样。 I know this won't be an ideal solution for everyone and there are probably better ways to do it, but it's worked for me so far. 我知道这对于每个人来说都不是理想的解决方案,也许有更好的方法可以做到这一点,但是到目前为止,它对我来说是有效的。 Thanks, Alex 谢谢,亚历克斯

You should really look into python's whitenoise module. 您应该真正研究python的whitenoise模块。 Then you only have one line you have to change and the URL routing is handled by whitenoise next to bottle. 这样一来,您只需要更改一行就可以了,URL路由由瓶旁边的白噪声处理。 Essentially you can keep the url the same as your domain, even if whitenoise is pulling from someplace else. 从本质上讲,即使白色噪声从其他地方提取,您也可以将URL与域保持相同。 PLus it's designed to cache your static data. PLus旨在缓存您的静态数据。 Definitely should be using it. 绝对应该使用它。 It takes like 3 lines to setup, and you can remove bottle's static folder route. 设置过程大约需要3行,您可以删除瓶子的静态文件夹路线。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM