我有一个在 Heroku 上托管的 Flask 应用程序,但希望从 Amazon S3 提供静态文件。
在我的模板中,我使用 url_for() 来引用静态文件。在 Flask 应用程序的初始化中,我想把
app = Flask(__name__, static_url_path="http://my-bucket.s3.amazonaws.com")
Run Code Online (Sandbox Code Playgroud)
确保使用http://my-bucket.s3.amazonaws.com/static/而不是 mysite.com/static/。但是我收到这个错误:
ValueError: urls must start with a leading slash
Run Code Online (Sandbox Code Playgroud)
如果我将其更改为带有前导斜杠的值,它就可以工作,但我希望静态 URL 指向 S3(一个外部域),因此它需要以 http:// 开头。
我究竟做错了什么?如何通过 Flask 和 Heroku 将 S3 用于静态文件?
当我实现静态服务器处理程序时,如果我访问根路径,它将显示整个目录,如下所示:
我的代码是:
package main
import (
"flag"
"log"
"net/http"
"strings"
)
func main() {
port := flag.String("p", "3000", "port to serve on")
directory := flag.String("d", ".", "the directory of static file to host")
flag.Parse()
http.Handle("/statics/", http.StripPrefix(strings.TrimRight("/statics/", "/"), http.FileServer(http.Dir(*directory))))
log.Printf("Serving %s on HTTP port: %s\n", *directory, *port)
log.Fatal(http.ListenAndServe(":"+*port, nil))
}
Run Code Online (Sandbox Code Playgroud)
通常我有这个问题正好相反!
在我的开发环境中,我的 Django 应用程序不会加载我的一些静态文件,特别是我自己添加的那些:也就是说,我添加到我的应用程序中的两个包(admin和ckeditor)都加载良好,但其中两个找不到我自己创建和链接的文件夹(img和css)。这是我的目录的地图:
root
|-- blog (this is the name of my app)
|-- mysite (name of my site)
|-- settings.py
|-- urls.py
|-- media
|-- static
|-- admin
|-- ckeditor
|-- css
|-- img
Run Code Online (Sandbox Code Playgroud)
如前所述,ckeditor和admin负载罚款,而有的则没有。以下是runserver调试模式下的输出示例(文件位于static/css/base.css我的文件树中):
GET /static/ckeditor/ckeditor/ckeditor.js HTTP/1.1" 200 690627
GET /static/admin/css/fonts.css HTTP/1.1" 200 423
GET /static/admin/css/widgets.css HTTP/1.1" 200 10340
GET /static/css/base.css HTTP/1.1" 404 1761
GET /static/img/brand.png HTTP/1.1" 404 1764
Run Code Online (Sandbox Code Playgroud)
以下是一些您可能感兴趣的其他信息:
这是一个非常简单的任务,我想在我的剃刀页面中使用图像,这些图像位于wwwroot/css/imageshtml 中,并像这样在 html 中使用它。
<img src="~/css/images/logo.png"/>
Run Code Online (Sandbox Code Playgroud)
在一个普通的 ASP.Net Core 应用程序中,我只需要添加app.UseStaticFiles()Configure 方法,但由于这是一个 blazor 托管应用程序,它没有任何改变。
我也尝试app.UseStaticFiles()在服务器项目中添加,但也没有成功。
如果您想自己尝试一下,只需从模板创建一个 blazor 托管的项目,然后像我一样尝试提供/显示图像。
我一直在尝试用 F# 创建一个非常简单的 MVC 项目。这是对我所做的事情的简要说明,我将不胜感激。
/ Root
| appsettings.json
| Views
| Home
| Index.cshtml // added as "Content", "Do not copy"
HomeController.fs
Startup.fs
Program.fs
Run Code Online (Sandbox Code Playgroud)
控制器非常简单:
/ Root
| appsettings.json
| Views
| Home
| Index.cshtml // added as "Content", "Do not copy"
HomeController.fs
Startup.fs
Program.fs
Run Code Online (Sandbox Code Playgroud)
然后,当我启动我的应用程序时,出现错误:
InvalidOperationException: The view 'Index' was not found. The following locations were searched:
/Views/Home/Index.cshtml
/Views/Shared/Index.cshtml
Run Code Online (Sandbox Code Playgroud)
所以即使我的 cshtml 文件在那里,它也不能被运行时找到。 …
这是一个简单的静态 FastAPI 应用程序。通过此设置,即使根路径预计返回 a FileResponse,custom.html应用程序仍会返回index.html。如何让根路径工作并渲染custom.html?
from fastapi import FastAPI
from fastapi.staticfiles import StaticFiles
from fastapi.responses import FileResponse
app = FastAPI()
app.mount(
"/",
StaticFiles(directory="static", html=True),
name="static",
)
@app.get("/")
async def index() -> FileResponse:
return FileResponse("custom.html", media_type="html")
Run Code Online (Sandbox Code Playgroud) 我正在构建一个Web应用程序,我必须处理国际字符(例如"J'aivisualillédesélèveàladôtule").某些数据位于文件系统上任意目录中的任意静态文本文件中.这些文件都是utf-8(感谢标准化!)
为了提供这些数据,我使用了一个带有ResourceHandler处理程序的嵌入式jetty.我没有任何web.xml文件.除了静态之外,我还有一堆通过servlet处理的restful API.
问题是,Jetty ResourceHandler类似乎没有随静态文件一起发送字符集Content-Type.如果我请求index.html, Content-Type是text/html.为了正确处理突出的角色,我希望它能成为现实Content-Type: text/html; charset=utf-8
对于具有utf-8的默认字符集的文件,text/html或者text/css,这很好,但是有些文本文件没有,并且被错误地解释为Windows-1252,并且重音字符变得乱码(我只是回到了QuébecLiquor Store,而不是魁北克酒商店).有没有办法指定默认字符集并告诉jetty始终发送它?像阿帕奇这样的东西AddDefaultCharset utf-8
从我读到的,静态文件应该由服务器直接提供,而不是使用Python和Django.但我需要限制文件访问上传它们的用户.遗憾的是,该文档没有关于提供用户在生产环境中上传的静态文件的部分.
如果我是对的,Facebook会使用难以猜测的长网址.这听起来像是一种合理的方法.如何在Django中自动生成长ID并将其用于上传的媒体文件?
我有一个节点App.配置为通过以下方式提供静态文件:
app.use(express.static(path.join(__dirname, '../public')));
我在其他路由上使用一些auth中间件.当我点击服务器上不存在的图像时出现问题.在这种情况下,快递似乎试图通过我对非静态内容的所有中间件发送该请求.有没有办法只发送404丢失的静态资产,而不是重新触发每个丢失文件的所有中间件?
我是新来的反应者,我正在尝试用它构建一个聊天应用程序。我使用了react-router来根据URL加载不同的组件。在我的React项目文件(client / src / index.js)中,代码如下:
import {BrowserRouter as Router, Route} from 'react-router-dom';
...
ReactDOM.render(
<Router>
<div>
<Route exact path='/' component={App} />
<Route path='/customer' component={CustomerPage} />
<Route path='/support/:support_id' component={SupportPage} />
</div>
</Router>,
document.getElementById('root')
);
...
Run Code Online (Sandbox Code Playgroud)
当我使用“ npm start”在react文件夹中启动它时,它可以找到。但是,当我运行“ npm run build”并通过express服务器提供静态文件时,它只能在“ /”路径中提供应用页面,而对于“ / customer”和“ / support /:support_id”路径,它将加载没有。
在快速服务器文件夹中,我通过以下方式加载静态文件:
服务器/ app.js:
...
var indexRouter = require('./routes/index');
app.use('/static', express.static(path.join(__dirname, '../client/build//static')));
app.use('/', indexRouter);
...
Run Code Online (Sandbox Code Playgroud)
服务器/路由/index.js:
...
router.get('/', function(req, res) {
res.sendFile('index.html', {root: path.join(__dirname, '../../client/build/')});
});
...
Run Code Online (Sandbox Code Playgroud)
任何帮助将不胜感激!
static-files ×10
asp.net-core ×2
django ×2
node.js ×2
python ×2
blazor ×1
c# ×1
express ×1
f# ×1
fastapi ×1
fileresponse ×1
flask ×1
go ×1
heroku ×1
http ×1
javascript ×1
middleware ×1
python-3.x ×1
random ×1
reactjs ×1
router ×1
security ×1
starlette ×1
utf-8 ×1