Cookiecutter-django: Add Digital Ocean spaces as storage option

Created on 27 Nov 2018  路  5Comments  路  Source: pydanny/cookiecutter-django

Description

those who do not use AWS s3 but use Digitalocean spaces have trouble deploying static and media, the configurations are a little different.

Solution

AWS_S3_ENDPOINT_URL = env(DJANGO_AWS_S3_ENDPOINT_URL)
AWS_LOCATION = env(DJANGO_AWS_LOCATION)
enhancement hacktoberfest help wanted

Most helpful comment

I think i found a solution, but need to be reproduced again. First of all need to update DO settings (Spaces -> Select a space -> Settings -> CORS Configurations):
2019-09-27-210821_548x363_scrot

My .envs/.production/.django:

# DigitalOcean Spaces
DJANGO_AWS_ACCESS_KEY_ID=key is here (removed)
DJANGO_AWS_SECRET_ACCESS_KEY=key is here (removed)
DJANGO_AWS_STORAGE_BUCKET_NAME=storage name
#DJANGO_AWS_S3_REGION_NAME=fra1
DJANGO_AWS_S3_ENDPOINT_URL=https://(storage name).fra1.digitaloceanspaces.com
DJANGO_AWS_LOCATION=fra1

My production config settings for STATIC and MEDIA (config/settings/production.py):

# STORAGES
# ------------------------------------------------------------------------------
# https://django-storages.readthedocs.io/en/latest/#installation
INSTALLED_APPS += ["storages"]  # noqa F405
# https://django-storages.readthedocs.io/en/latest/backends/amazon-S3.html#settings
AWS_ACCESS_KEY_ID = env("DJANGO_AWS_ACCESS_KEY_ID")
# https://django-storages.readthedocs.io/en/latest/backends/amazon-S3.html#settings
AWS_SECRET_ACCESS_KEY = env("DJANGO_AWS_SECRET_ACCESS_KEY")
# https://django-storages.readthedocs.io/en/latest/backends/amazon-S3.html#settings
AWS_STORAGE_BUCKET_NAME = env("DJANGO_AWS_STORAGE_BUCKET_NAME")
# https://django-storages.readthedocs.io/en/latest/backends/amazon-S3.html#settings
AWS_QUERYSTRING_AUTH = False
# DO NOT change these unless you know what you're doing.
_AWS_EXPIRY = 60 * 60 * 24 * 7
# https://django-storages.readthedocs.io/en/latest/backends/amazon-S3.html#settings
AWS_S3_OBJECT_PARAMETERS = {
    "CacheControl": f"max-age={_AWS_EXPIRY}, s-maxage={_AWS_EXPIRY}, must-revalidate"
}
#  https://django-storages.readthedocs.io/en/latest/backends/amazon-S3.html#settings
AWS_DEFAULT_ACL = None
# https://django-storages.readthedocs.io/en/latest/backends/amazon-S3.html#settings
AWS_S3_REGION_NAME = env("DJANGO_AWS_S3_REGION_NAME", default=None)
AWS_S3_ENDPOINT_URL = env('DJANGO_AWS_S3_ENDPOINT_URL')
AWS_LOCATION = env('DJANGO_AWS_LOCATION')

# STATIC
# ------------------------------------------------------------------------------
STATICFILES_STORAGE = "config.settings.production.StaticRootS3Boto3Storage"
STATIC_URL = f"https://{AWS_STORAGE_BUCKET_NAME}.s3.amazonaws.com/static/"

# MEDIA
# ------------------------------------------------------------------------------
# region http://stackoverflow.com/questions/10390244/
# Full-fledge class: https://stackoverflow.com/a/18046120/104731
from storages.backends.s3boto3 import S3Boto3Storage  # noqa E402


class StaticRootS3Boto3Storage(S3Boto3Storage):
    location = "static"
    default_acl = "public-read"


class MediaRootS3Boto3Storage(S3Boto3Storage):
    location = "media"
    default_acl = "public-read"
    #file_overwrite = False


# endregion
DEFAULT_FILE_STORAGE = "config.settings.production.MediaRootS3Boto3Storage"
MEDIA_URL = f"https://{AWS_STORAGE_BUCKET_NAME}.s3.amazonaws.com/media/"

I don't know, what is right solution, because i also removed file_overwrite = False.

All 5 comments

See also: #1053

Is this still a requested feature @jeanluc243? I could knock this out this week.

Have static working and triaging one last bug with media. Will push this week

Have static working and triaging one last bug with media. Will push this week

Same problem. I don't know why, but media folder files always is private files. I tried to change class rights, but nothing changed:

class MediaRootS3Boto3Storage(S3Boto3Storage):  
    location = "media"  
    default_acl = "public-read"  
    file_overwrite = False  

Any ideas?

I think i found a solution, but need to be reproduced again. First of all need to update DO settings (Spaces -> Select a space -> Settings -> CORS Configurations):
2019-09-27-210821_548x363_scrot

My .envs/.production/.django:

# DigitalOcean Spaces
DJANGO_AWS_ACCESS_KEY_ID=key is here (removed)
DJANGO_AWS_SECRET_ACCESS_KEY=key is here (removed)
DJANGO_AWS_STORAGE_BUCKET_NAME=storage name
#DJANGO_AWS_S3_REGION_NAME=fra1
DJANGO_AWS_S3_ENDPOINT_URL=https://(storage name).fra1.digitaloceanspaces.com
DJANGO_AWS_LOCATION=fra1

My production config settings for STATIC and MEDIA (config/settings/production.py):

# STORAGES
# ------------------------------------------------------------------------------
# https://django-storages.readthedocs.io/en/latest/#installation
INSTALLED_APPS += ["storages"]  # noqa F405
# https://django-storages.readthedocs.io/en/latest/backends/amazon-S3.html#settings
AWS_ACCESS_KEY_ID = env("DJANGO_AWS_ACCESS_KEY_ID")
# https://django-storages.readthedocs.io/en/latest/backends/amazon-S3.html#settings
AWS_SECRET_ACCESS_KEY = env("DJANGO_AWS_SECRET_ACCESS_KEY")
# https://django-storages.readthedocs.io/en/latest/backends/amazon-S3.html#settings
AWS_STORAGE_BUCKET_NAME = env("DJANGO_AWS_STORAGE_BUCKET_NAME")
# https://django-storages.readthedocs.io/en/latest/backends/amazon-S3.html#settings
AWS_QUERYSTRING_AUTH = False
# DO NOT change these unless you know what you're doing.
_AWS_EXPIRY = 60 * 60 * 24 * 7
# https://django-storages.readthedocs.io/en/latest/backends/amazon-S3.html#settings
AWS_S3_OBJECT_PARAMETERS = {
    "CacheControl": f"max-age={_AWS_EXPIRY}, s-maxage={_AWS_EXPIRY}, must-revalidate"
}
#  https://django-storages.readthedocs.io/en/latest/backends/amazon-S3.html#settings
AWS_DEFAULT_ACL = None
# https://django-storages.readthedocs.io/en/latest/backends/amazon-S3.html#settings
AWS_S3_REGION_NAME = env("DJANGO_AWS_S3_REGION_NAME", default=None)
AWS_S3_ENDPOINT_URL = env('DJANGO_AWS_S3_ENDPOINT_URL')
AWS_LOCATION = env('DJANGO_AWS_LOCATION')

# STATIC
# ------------------------------------------------------------------------------
STATICFILES_STORAGE = "config.settings.production.StaticRootS3Boto3Storage"
STATIC_URL = f"https://{AWS_STORAGE_BUCKET_NAME}.s3.amazonaws.com/static/"

# MEDIA
# ------------------------------------------------------------------------------
# region http://stackoverflow.com/questions/10390244/
# Full-fledge class: https://stackoverflow.com/a/18046120/104731
from storages.backends.s3boto3 import S3Boto3Storage  # noqa E402


class StaticRootS3Boto3Storage(S3Boto3Storage):
    location = "static"
    default_acl = "public-read"


class MediaRootS3Boto3Storage(S3Boto3Storage):
    location = "media"
    default_acl = "public-read"
    #file_overwrite = False


# endregion
DEFAULT_FILE_STORAGE = "config.settings.production.MediaRootS3Boto3Storage"
MEDIA_URL = f"https://{AWS_STORAGE_BUCKET_NAME}.s3.amazonaws.com/media/"

I don't know, what is right solution, because i also removed file_overwrite = False.

Was this page helpful?
0 / 5 - 0 ratings