问题
I've been working on uploading a file on Django. I have the following code to run things.
def handle_uploaded_file(file, filename):
if not os.path.exists('upload/'):
os.mkdir('upload/')
with open('upload/' + filename, 'wb+') as destination:
for chunk in file.chunks():
destination.write(chunk)
def upload_csv(request):
# Get the context from the request.
context = RequestContext(request)
if request.is_ajax():
if "POST" == request.method:
csv_file = request.FILES['file']
my_df = pd.read_csv(csv_file, header=0)
handle_uploaded_file(csv_file, str(csv_file))
.............................
.............................
As you can see above, I have been uploading files to the upload directory. However, my concern is that this type of method might not be that much efficient because each file is kept and stored in that folder. What if hundreds or thousands of files are uploaded each week? See this:
What would be the other way to efficiently and effectively upload files?
回答1:
It is considered best practice to store your uploaded files on a disk. There is no way around. You must provide enough disk space on your server. In addition, you could automatically block uploads if you are running out of disk space.
Storing files in a database should never happen. If it’s being considered as a solution for a problem, find a certified database expert and ask for a second opinion.
Source: Two Scopes of Django.
If you concerned about the file limit within a folder you could introduce subfolders to your upload
directory. For example upload/category/year/filename
. You can take advantage of the FileField.
def document_directory_path(instance, filename):
path = 'upload/{}/%Y/{}'.format(instance.category.slug, filename)
return datetime.datetime.now().strftime(path)
class Document(models.Model):
category = models.ForeignKey(Category, on_delete=models.CASCADE)
file = models.FileField(upload_to=document_directory_path)
def filename(self):
return os.path.basename(self.file.name)
Another similar example that groups your uploads by the user (uploader) id can be found here.
Last but not least, you have the option to move to a cloud provider. Which can be pricey if you expect thousands of files and a lot of traffic.
来源:https://stackoverflow.com/questions/48958888/uploading-files-in-django-how-to-avoid-the-increase-of-files-in-the-upload-dir