Report this

What is the reason for this report?

Storing Media Files on Space/AWS Bucket in Django project doesn't work

Posted on July 12, 2020

Hi there,

I followed a long this tutorial but it seems it doesn’t work. The media files are not rendered due to bad URL I guess.

I hope you dont mind if I reference the related stackoverflow post to my issue: https://stackoverflow.com/questions/62859990/storing-django-media-files-on-digitalocean-aws-doesnt-work-due-to-bad-url?noredirect=1#comment111160066_62859990

Any advise on what to fix is highly appreciated :)

Thank you and best regards, Jonas



This textbox defaults to using Markdown to format your answer.

You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!

These answers are provided by our Community. If you find them useful, show some love by clicking the heart. If you run into issues leave a comment, or add your own answer to help others.

Hi there,

It looks like that the SO question is no longer available, so I could share some general suggestions based on the information here.

Here are some common troubleshooting steps:

  1. Correct AWS/Spaces Credentials:

    • Ensure the AWS_ACCESS_KEY_ID, AWS_SECRET_ACCESS_KEY, AWS_STORAGE_BUCKET_NAME, and AWS_S3_ENDPOINT_URL in your settings.py are correctly set.
  2. Correct Bucket and Object Permissions:

    • Ensure that your DigitalOcean Space is publicly accessible, or the files inside are set to be publicly readable.
  3. Correct URLs:

    • Ensure the STATIC_URL is correctly formatted and pointing to the right space and folder.
    • Make sure that the actual file paths in your Space are corresponding to what Django is generating for your static URLs.
    • Test URLs directly in your browser to see if they point to actual files in your Space.
  4. Middleware and Apps Order:

    • In your settings.py, ensure storages is properly placed in INSTALLED_APPS.
  5. Boto3 and Django-Storages Version:

    • Sometimes specific versions of boto3 and django-storages are needed. Make sure they are up-to-date, or check if there are known issues with the versions you are using.
  6. Check your Static File Referencing in Templates:

    • Ensure that in your HTML templates the static files are referenced correctly using {% static 'path_to_file' %} and ensure that 'django.contrib.staticfiles' is in INSTALLED_APPS.
  7. Server Error Logs:

    • Check your server error logs to identify if there are any issues when trying to retrieve static files. This may provide additional insights into what’s going wrong.
  8. Browser Developer Tools:

    • Use your browser developer tools to inspect network requests for your static files and see what kind of HTTP status codes are being returned.
  9. SSL Certificate:

    • If you are enforcing HTTPS, ensure your SSL certificate is valid.

Some additional things that you could check are:

  • You can use python manage.py shell to debug your storage backend and try to retrieve a file URL. Example:

    from django.core.files.storage import default_storage
    default_storage.url('path_to_your_file')
    

    This should return the full URL to your file in the storage backend. Make sure this URL is valid and reachable.

  • Test static serving with Django’s default backend to ensure the problem is not with the Django application. If the files are served correctly with Django’s default backend, it points towards a configuration issue with django-storages and DigitalOcean Spaces.

Based on your findings from the above checklist and further debugging, you may need to:

  • Adjust the configuration in settings.py.
  • Update file/folder permissions in your DigitalOcean Space.
  • Update the boto3 and django-storages packages.

Best,

Bobby

The developer cloud

Scale up as you grow — whether you're running one virtual machine or ten thousand.

Get started for free

Sign up and get $200 in credit for your first 60 days with DigitalOcean.*

*This promotional offer applies to new accounts only.