2

I am building an application that allows users to upload images. I am using django-storage with S3 boto to store the images in S3. I am very new to Django and have a few questions.

  • Are the temporary files created during upload deleted automatically or should I delete them? If yes, then how?
  • If the files are created in memory are they automatically deleted from memory?
  • When I create a Django Imagefile does it create a temporary file?
  • Should I explicitly close the file after opening it? I tried doing so but got an error close message. How do I do it?

Simple examples would be appreciated.

Jacinda
  • 4,932
  • 3
  • 26
  • 37
Saransh Mohapatra
  • 9,430
  • 10
  • 39
  • 50

1 Answers1

1

Quoting from the Django documentation:

Where uploaded data is stored

Before you save uploaded files, the data needs to be stored somewhere.

By default, if an uploaded file is smaller than 2.5 megabytes, Django will hold the entire contents of the upload in memory. This means that saving the file involves only a read from memory and a write to disk and thus is very fast.

However, if an uploaded file is too large, Django will write the uploaded file to a temporary file stored in your system’s temporary directory. On a Unix-like platform this means you can expect Django to generate a file called something like /tmp/tmpzfp6I6.upload. If an upload is large enough, you can watch this file grow in size as Django streams the data onto disk.

These specifics – 2.5 megabytes; /tmp; etc. – are simply “reasonable defaults”. Read on for details on how you can customize or completely replace upload behavior.

source: https://docs.djangoproject.com/en/dev/topics/http/file-uploads/

Community
  • 1
  • 1
gitaarik
  • 42,736
  • 12
  • 98
  • 105
  • So does it mean that I have to delete this directory from time to time?? – Saransh Mohapatra May 23 '13 at 15:23
  • 1
    in linux or unix-like systems the `/tmp` directory gets automatically cleared from time to time, so you don't have to worry about it – gitaarik May 23 '13 at 15:23
  • You only answered my one question that too half. Please help me with the other question. What if I create ImageFile object, is it save somewhere?? – Saransh Mohapatra May 23 '13 at 15:24
  • A File object in django is just an object that helps you manage the file. Generally you won't create this object yourself but get it from Django, and then the file already exists. – gitaarik May 23 '13 at 15:27
  • But what if I want to create one? Than how would I create one? And where can I store it? Will I be able to store it anywhere I want to? – Saransh Mohapatra May 23 '13 at 17:19
  • Why would you want to create a django file object? You can also use python's normal file handling functions – gitaarik May 23 '13 at 20:11