Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save FaridLU/b0ac94d1fe069eea4209ff50cf1eb97b to your computer and use it in GitHub Desktop.
Save FaridLU/b0ac94d1fe069eea4209ff50cf1eb97b to your computer and use it in GitHub Desktop.

Create Space in DigitalOcean and setup necessary keys and access point.

  1. Use the following link to create space: https://cloud.digitalocean.com/spaces/new
  2. Choose nearest region in the Region section.
  3. Check "Enable CDN" option to get fastest delivery of web assests.
  4. Now input a unique name relavent to your project in the "Bucket Name" section.
  5. Now create the space. This will take a couple of seconds to get the space created.
  6. Copy the Origin endpoint (in top right corner of the Space details page) and note it down somewhere else.

Create Access keys for DigitalOcean Space

  1. Use the following link to generate access key: https://cloud.digitalocean.com/account/api/spaces
  2. Now use a convenient name in the name section of access key (try to use a relavant name of your project).
  3. After creation you should see 2 keys. One is Access Key and another one is Secret Key.
  4. Copy those and note somewhere else.

Setup your django project with the necessary changes

  1. Install boto3 and django-storages by the following command. (Activate your virtual env if you using virtual env) $ pip3 install boto3 django-storages

  2. Add storages in the INSTALLED_APPS section inside settions.py file.

  3. Create a new file named storage_backends.py in the same folder where settings.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"
    
  4. 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'
    
  5. 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
    
  6. From now on, the static and media files will be stored in cloud,

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment