django heroku media files 404 error

后端 未结 2 1187
情话喂你
情话喂你 2021-01-03 12:59

I recently deployed a Django app to Heroku and uploaded some media files and everything seemed to work fine, until yesterday when i tried to access the application again and

2条回答
  •  太阳男子
    2021-01-03 13:49

    Heroku dynos are of limited lifespan, and when they die and get replaced (which happens automatically) any files within them are lost, including any files you uploaded via Django. What you want to do is to set up Django's media handling to put the files somewhere more permanent (which will also allow you to use multiple dynos at once, which is how Heroku tackles horizontal scaling). I tend to use Amazon S3 for this, so my configuration looks a little like:

    AWS_STORAGE_BUCKET_NAME = "your_bucket"
    DEFAULT_FILE_STORAGE = 'storages.backends.s3boto.S3BotoStorage'
    MEDIA_URL = "https://%s.s3.amazonaws.com/" % os.environ['AWS_STORAGE_BUCKET_NAME']
    MEDIA_ROOT = ''
    AWS_ACCESS_KEY_ID = "your_access_key_id"
    AWS_SECRET_ACCESS_KEY = "your_secret_access_key"
    

    This is using django-storages and boto to provide a Django storage layer using Amazon S3.

    Note that this "pass-through" access for S3 may be inappropriate depending on your application. There are some notes on working with S3 in Heroku's devcenter that may help.

提交回复
热议问题