简体   繁体   English

Heroku 和 Django:如何使用文件服务器来存储和编辑用户文件

[英]Heroku and Django: How to use file server for storing and editing user files

I built a Django app that has some .txt files in a folder on under the project root dir as shown below.我构建了一个 Django 应用程序,它在项目根目录下的文件夹中有一些 .txt 文件,如下所示。

<pre>
[projectname]/
├── [projectname]/
│   ├── __init__.py
│   ├── settings.py
│   ├── urls.py
│   └── wsgi.py
└── manage.py
├── [<b> myfiles </b>]/
│   ├── file1.txt
│   ├── file2.txt
│   ├── file3.txt
│   └── file4.txt
└── [myApp]/
</pre>

I specify the files paths when I am running the django app on my local machine.当我在本地机器上运行 django 应用程序时,我指定了文件路径。 Now, I tried running my app on Heroku, but the not app does not work when I tried reference any of my files to do some processing.现在,我尝试在 Heroku 上运行我的应用程序,但是当我尝试引用我的任何文件进行一些处理时,not 应用程序不起作用。 Is there a way that I can use a file server (external or internal to Heroku) where I can store my files on and then update them in via the django app (by the users) and save them back to file server?有没有办法可以使用文件服务器(Heroku 的外部或内部),我可以在其中存储我的文件,然后通过 django 应用程序(由用户)更新它们并将它们保存回文件服务器?

In my app, when a user creates a new project, in the background a function will create a folder for the user and then copy the files in [myfiles] folder to the user's new project folder.在我的应用程序中,当用户创建新项目时,在后台一个函数将为用户创建一个文件夹,然后将 [myfiles] 文件夹中的文件复制到用户的新项目文件夹中。

Also, users can enter data through forms that I want to to save to .rdf files and then save to the file server.此外,用户可以通过我想要保存到 .rdf 文件然后保存到文件服务器的表单输入数据。

In the end, I the file system would look like this:最后,我的文件系统看起来像这样:

<pre>
[file system root]/
├── [<b> myfiles </b>]/
│   ├── file1.txt
│   ├── file2.txt
│   ├── file3.txt
│   └── file4.txt
├── [<b> user1 </b>]/
│   ├── file1.txt
│   ├── file2.txt
│   ├── file3.txt
│   └── graphfile1
├── [<b> user2 </b>]/
│   ├── file1.txt
│   ├── graphfile1
│   ├── file3.txt
│   └── file4.txt
├── [<b> user3 </b>]/
│   ├── file1.txt
│   ├── file2.txt
│   ├── graphfile1
│   └── file4.txt
....
</pre>

You need to use a service like S3, use django-storages package.你需要使用像 S3 这样的服务,使用 django-storages 包。

And one thing, in Heroku the data is not persistent.还有一件事,在 Heroku 中,数据不是持久的。 Every time the dyno is restart, the files restart to the original image too.每次重新启动dyno时,文件也会重新启动到原始图像。 More info here: https://help.heroku.com/K1PPS2WM/why-are-my-file-uploads-missing-deleted更多信息在这里: https : //help.heroku.com/K1PPS2WM/why-are-my-file-uploads-missing-deleted

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

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