cookiecutter-django icon indicating copy to clipboard operation
cookiecutter-django copied to clipboard

Add Digital Ocean spaces as storage option

Open jeanluc243 opened this issue 7 years ago • 8 comments

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)

jeanluc243 avatar Nov 27 '18 06:11 jeanluc243

See also: #1053

browniebroke avatar Mar 19 '19 09:03 browniebroke

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

vladdoster avatar Apr 14 '19 19:04 vladdoster

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

vladdoster avatar Apr 17 '19 03:04 vladdoster

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?

stefanitsky avatar Sep 27 '19 09:09 stefanitsky

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.

stefanitsky avatar Sep 27 '19 18:09 stefanitsky

Is this issue still open, if yes i would like to work on it

tuhinmallick avatar Jan 06 '23 05:01 tuhinmallick

I think so @tuhinmallick: Running into issues with digital oceans spaces, django 4.2 and cookie cutter. Looks like my images are being uploaded to one location but the object.attribute.url is pointing to a different location.

ethanitovitch avatar Oct 20 '23 20:10 ethanitovitch

Same as @ethanitovitch . This doesn't work in Django 4.2 and higher.

offsideAI avatar Jan 14 '24 09:01 offsideAI