MEDIA_URL =‘/media/’ MEDIA_ROOT=os.path.join(BASE_DIR,‘media’)
how to link to my spaces so that files will be uploaded to spaces not the server.
staticfiles are working as stored on the spaces. but i dont know how to upload media files to storages.
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.
Just got this working last night, this is how I did it.
Add a
custom_storages.py
file in the same directoy asmanage.py
. This way you don’t need to import the module in yoursettings.py
file.custom_storages.py
I have my settings set up like Two Scoops of Django. Sorry I don’t know how to do the fancy file structure markup.
config —>settings ------>local.py ------>dev.py ------>prod.py
In your production settings, i.e
prod.py
, orsettings.py
, however you have it configured, you need the following:SECRET_KEY
at top of file above import statements.Then run:
That will copy all your static files to a folder like so
<your_space_name>/static/
Go to your admin interface or a page with file upload, and upload a test file. It should upload to
<your_space_name>/media/
One thing that did happen with mine was that it was adding in another
<your_space_name>
to the endpoint of the static and media files. So it looked like this:To get the
url
that Django rendered the same as the Fileurl
in the Space, I had to manually add in another/<your_space_name/
to the custom domain endpoint, i.eAWS_S3_CUSTOM_DOMAIN
. So it looked like this:AWS_S3_CUSTOM_DOMAIN = '<your_custom_domain_url>/<your_space_name>'
The following resources are helpful and contain everything you need to get it working:
If I understand you correctly, you want to upload the files programmatically to Spaces instead of the Droplet that you are using. You can use the Digital Ocean API to perform these actions, here is the reference API to upload an object (using HTTP PUT request). API exposes other functions that can be used to perform other options (delete, query, etc.).
Check this how-to guide about the Spaces API to learn about what is possible with Spaces.