使用Django在Heroku部署中上传文件 [英] File uploads in Heroku deployment with Django

查看:92
本文介绍了使用Django在Heroku部署中上传文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我终于能够建立我正在从事的本地+产品测试项目.

So I was finally able to set up local + prod test project I'm working on.

# wsgi.py 
from dj_static import Cling, MediaCling

application = Cling(MediaCling(get_wsgi_application()))
application = DjangoWhiteNoise(application)

我使用 dj_static 和Postgres用于本地+产品.起初一切都很好...静态文件,文件上传.

I set up static files using whitenoise (without any problems) and media (file uploads) using dj_static and Postgres for local + prod. Everything works fine at first... static files, file uploads.

但是在Heroku测功机重新启动后,我丢失了所有文件上传.我的问题是---由于我是从Django应用而不是从S3提供媒体文件,因此dyno重新启动是否也将所有内容都清除掉了?

But after the Heroku dynos restart I lose all the file uploads. My question is, --- Since I'm serving the media files from the Django app instead of something like S3, does the dyno restart wipe all that out too?

PS:我知道我可以在AWS等平台上做到这一点,但我只想知道是否就是我丢失所有上传内容的原因.

PS: I'm aware I can do this with AWS, etc, but I just want to know if thats the reason I'm losing all the uploads.

推荐答案

由于我是从Django应用而不是从S3提供媒体文件,因此dyno重新启动也会将所有内容全部清除吗?

Since I'm serving the media files from the Django app instead of something like S3, does the dyno restart wipe all that out too?

是!这是正确的.根据 Heroku文档:

Yes!. That's right. According to the Heroku docs:

每个dyno都有自己的临时文件系统,其中包含最新部署的代码的新副本.

Each dyno gets its own ephemeral filesystem, with a fresh copy of the most recently deployed code.

另请参见答案和结论:对于媒体文件(上传的文件),您必须使用某些外部服务(例如S3或其他服务). whitenoise仅用于静态文件.请参见此处为什么whitenoise不适合为用户服务-上传的(媒体)文件.

Conclusion: For media files (the uploaded ones), you must use some external service (like S3 or something). whitenoise is just for static files. See here why whitenoise is not suitable for serving user-uploaded (media) files.

这篇关于使用Django在Heroku部署中上传文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆