I am facing the following issue: I have deployed a Django-app to the Heroku and want to upload an image using file input. But it should not be storing there, it could be deleted after I reload the page, but I want to have URL to this image for that time. So, I have no idea how to do this at all, can someone help me?
-
The [Heroku docs](https://devcenter.heroku.com/articles/active-storage-on-heroku#ephemeral-disk) recommend amazon cloud storage – Sep 01 '21 at 20:32
-
@ChrisG thanks, but if possible I want to store it on the Heroku server. – Artem Sep 01 '21 at 20:35
-
You don't understand. Did you not read the docs I linked? You can absolutely store files on heroku for a few seconds or a few minutes. Just not long-term. The file is gone when the app is restarted. – Sep 01 '21 at 20:42
-
U cannot upload files in heroku. you have to upload your files in cloud, get the url and serve that url. use "cloudinary", it is free – Yilmaz Sep 06 '21 at 02:53
2 Answers
The Heroku filesystem is ephemeral - that means that any changes to the filesystem whilst the dyno is running only last until that dyno is shut down or restarted. Each dyno boots with a clean copy of the filesystem from the most recent deploy. This is similar to how many container based systems, such as Docker, operate.
In addition, under normal operations dynos will restart every day in a process known as "Cycling".
for more information read this documentation https://help.heroku.com/K1PPS2WM/why-are-my-file-uploads-missing-deleted
what i would instead of using aws s3 bucket use cloudinary which is fast free to use upto 25gb how to use it read this full documetation https://www.section.io/engineering-education/uploading-images-to-cloudinary-from-django-application/ and for static file use whitenoise which i am also using and recommending i hope this will help you to solve your problem

- 738
- 1
- 7
- 30
-
Thanks, but I don't want to store it for a long, just for a couple of seconds. Is it really so impossible? – Artem Sep 01 '21 at 20:38
-
did you write any logic in your code to delete that phot after certain period of time or you just wan it do this automatically – Shreyash mishra Sep 01 '21 at 20:40
-
Yeah, I have a script it works perfectly on the local machine, but when I try to do this on Heroku it could not find my file. – Artem Sep 01 '21 at 20:43
-
I try to do something like `default_storage.save('some_folder/uploaded_picture.png', ContentFile(picture.read()))` and I can't access it via URL `(https://myherokuapp/some_folder/uploaded_picture.png)` – Artem Sep 01 '21 at 20:47
-
@Artem i didn't understand you so you want to store your images for short time and then want to delete it completely so heroku will do the same thing for u if you are not using s3 or cloudinary then what the problem – Shreyash mishra Sep 01 '21 at 20:48
-
@Blackranger I guess the main problem is that I can not get access to it via URL or I do not understand how to do this – Artem Sep 01 '21 at 20:50
-
Heroku does not allow permanent storage of images. They say in an article:
Heroku has an “ephemeral” hard drive, this means that you can write files to disk, but those files will not persist after the application is restarted.
One temporary way around this is to store your images in your PostgreSQL Database (StackOverflow Thread). A better solution is to upload your images to an Amazon S3 Bucket and only store the image URLs in your database.

- 111
- 5
-
Thanks, but is it really impossible to store it in some "temp" folder for a couple of seconds? – Artem Sep 01 '21 at 20:40
-
Yes you can certainly upload files to the heroku filesystem. They just don't remain there. They are deleted as soon as you restart the server. – Rohan Hussain Sep 03 '21 at 18:29