Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

OSError: [Errno 30] Read-only file system in Django on Heroku

I'm using Django 2.0 and Heroku to host the application.

My media directory settings are like

App/settings/production.py:

BASE_DIR = os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))

MEDIA_URL = '/media/'
MEDIA_ROOT = os.path.join(os.path.dirname(BASE_DIR), 'static_cdn', 'media_root')

I'm using gTTS to convert text to speech and save .mp3 file in the media directory:

tts_file_name = str(int(time.time())) + '.mp3'
joined_path = os.path.join(settings.MEDIA_ROOT, 'tts')
joined_path_with_file = os.path.join(joined_path, tts_file_name)

# create directory if does not exists
if not os.path.exists(joined_path):
    os.makedirs(joined_path)

tts = gTTS(text='Good morning', lang='en')
tts.save(joined_path_with_file)

# tts path to send to template
tts_media_url = os.path.join(settings.MEDIA_URL, 'tts', tts_file_name)

It is working fine on local system as I can change file permissions manually also.

But It is not working on Heroku and giving error:

OSError: [Errno 30] Read-only file system: '/static_cdn'

I tried to locate static_cdn by running heroku shell, but could not even found static_cdn in application path and root path. But it seems to be working as other uploading through form is working perfectly.

using Django model's upload_to is working and even directory is created in static_cdn.

How can I create directory in static_cdn on Heroku the same way Django does using model's upload_to?

like image 907
Anuj TBE Avatar asked Oct 21 '25 12:10

Anuj TBE


2 Answers

In my case, this error occurred because I set the STATIC_ROOT = '/static/'

This means it's looking at / root folder of the system and then static, which is obviously read-only,

changing it to STATIC_ROOT = 'static/' fixed my issue.

like image 67
amitnair92 Avatar answered Oct 23 '25 04:10

amitnair92


Changed MEDIA_ROOT path by removing additional os.path.dirname() and it is working now.

MEDIA_ROOT = os.path.join(BASE_DIR, 'static_cdn', 'media_root')
like image 30
Anuj TBE Avatar answered Oct 23 '25 05:10

Anuj TBE



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!