- Use the following link to create space: https://cloud.digitalocean.com/spaces/new
- Choose nearest region in the Region section.
- Check "Enable CDN" option to get fastest delivery of web assests.
- Now input a unique name relavent to your project in the "Bucket Name" section.
- Now create the space. This will take a couple of seconds to get the space created.
- Copy the Origin endpoint (in top right corner of the Space details page) and note it down somewhere else.
- Use the following link to generate access key: https://cloud.digitalocean.com/account/api/spaces
- Now use a convenient name in the name section of access key (try to use a relavant name of your project).
- After creation you should see 2 keys. One is
Access Key
and another one isSecret Key
. - Copy those and note somewhere else.
-
Install
boto3
anddjango-storages
by the following command. (Activate your virtual env if you using virtual env)$ pip3 install boto3 django-storages
-
Add
storages
in theINSTALLED_APPS
section insidesettions.py
file. -
Create a new file named
storage_backends.py
in the same folder wheresettings.py
is placed and place the following content there:from storages.backends.s3boto3 import S3Boto3Storage class StaticRootS3BotoStorage(S3Boto3Storage): location = "static" default_acl = 'public-read' class MediaRootS3BotoStorage(S3Boto3Storage): location = "media"
-
Add the following lines at the bottom of your
settings.py
file:AWS_ACCESS_KEY_ID = '<PLACE YOUR ACCESS KEY HERE>' AWS_SECRET_ACCESS_KEY = '<PLACE YOUR SECRET KEY HERE>' AWS_STORAGE_BUCKET_NAME = '<PLACE YOUR BUCKET NAME>' # e.g: project-bucket AWS_LOCATION = '<PLACE YOUR ORIGIN ENDPOINT HERE>' # e.g: https://project-bucket.sgp1.digitaloceanspaces.com AWS_S3_ENDPOINT_URL = '<PLACE YOUR ORIGIN ENDPOINT HERE and remove bucket name>' # e.g: https://sgp1.digitaloceanspaces.com AWS_S3_OBJECT_PARAMETERS = { "CacheControl": "max-age=86400", "ACL": "public-read" } DEFAULT_FILE_STORAGE = 'project.storage_backends.MediaRootS3BotoStorage' STATICFILES_STORAGE = 'project.storage_backends.StaticRootS3BotoStorage'
-
As we have changed the default storage you have to collect staticfiles and place in the bucket by following command:
$ python3 manage.py collectstatic --noinput
-
From now on, the static and media files will be stored in cloud,