0

Cursor ai called it a "strange problem", so we're back to the original home of solutions.

I upgraded django from 2.2 to 5.2, and upgraded my storages, tinymce,.. etc modules along with it.

i added a new app after the upgrade, and everything worked well in development. The issue is, when i run collectstatic in production, the static and media files that have already been synced from previous deployments remain available, but some of the static and media subfolders/files from recent upgrades do not get uploaded - even as they are all have the same static and media root directory. How is it that django-storages can "pick and choose" the folders to create within an identified static or media directory?

i'm not sure what other code to include to explain this further, but if anyone needs to see something else, let me know and i'll add code snippets. Thanks.

my settings

from configparser import RawConfigParser
from .base import *

DEBUG = False


ALLOWED_HOSTS = ['www.mywebsite.com']

CSRF_COOKIE_SECURE = True

SECURE_SSL_REDIRECT = True
CSRF_COOKIE_SECURE = True
SESSION_COOKIE_SECURE = True
SECURE_HSTS_SECONDS = 31536000  # 1 year
SECURE_HSTS_INCLUDE_SUBDOMAINS = True
SECURE_HSTS_PRELOAD = True



config = RawConfigParser()
config.read('/dir/ect/ory/file')

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.postgresql',
        'NAME': config.get('section', 'DB_NAME'),
        'USER': config.get('section', 'DB_USER'),
        'PASSWORD': config.get('section', 'DB_PASS'),
        'HOST': 'localhost',
        'PORT': '5432',
    }
}

# AWS S3 Configuration
AWS_ACCESS_KEY_ID = config.get('section','KEY_ID')
AWS_SECRET_ACCESS_KEY = config.get('section', 'SCRT_KEY')
AWS_STORAGE_BUCKET_NAME = config.get('section', 'BUCKET_NAME')
AWS_S3_REGION_NAME = 'us-east-1'
AWS_S3_FILE_OVERWRITE = False
AWS_S3_VERIFY = True
AWS_S3_CUSTOM_DOMAIN = f'{AWS_STORAGE_BUCKET_NAME}.s3.amazonaws.com'
# For static files (public)
AWS_QUERYSTRING_AUTH = False

# Static files configuration

STORAGES = {
    'staticfiles': {
        'BACKEND': 'storages.backends.s3boto3.S3Boto3Storage',
        'OPTIONS': {
            'location': 'static',  # or your desired S3 folder
        },
    },
    'default': {
        'BACKEND': 'storages.backends.s3boto3.S3Boto3Storage',
        'OPTIONS': {
            'location': 'media',
        },
    },
}


STATIC_URL = f'https://{AWS_S3_CUSTOM_DOMAIN}/static/'

# Media files configuration

MEDIA_URL = f'https://{AWS_S3_CUSTOM_DOMAIN}/media/'

# Cache control
AWS_S3_OBJECT_PARAMETERS = {
    'CacheControl': 'max-age=86400',
}

1 Answer 1

0

Make sure your DEBUG = False , Django stores the static files in the local files instead of uploading to the S3 while in development mode. If this is not the case, please hide your credentials or any private implementations and upload full code of settings.py to make the problem more clear to understand. Your current implementation looks fine for django 4.2 and later versions.

Standard Implementation should look like this:


DEBUG = False

ALLOWED_HOSTS = ['my-instance.us-east-1.elasticbeanstalk.com', '127.0.0.1']

AWS_STORAGE_BUCKET_NAME = "my-s3-bucket"
AWS_S3_REGION_NAME = "us-east-1"
AWS_S3_CUSTOM_DOMAIN = f'{AWS_STORAGE_BUCKET_NAME}.s3.{AWS_S3_REGION_NAME}.amazonaws.com'

# Optional but recommended:
AWS_DEFAULT_ACL = None  # Prevents "Access Denied" errors
AWS_QUERYSTRING_AUTH = False  # Removes ?AWSAccessKeyId=... from URLs

MEDIA_URL = f'https://{AWS_S3_CUSTOM_DOMAIN}/'

# Configuration of default storages for Django >4.2 versions
STORAGES = {
    "default" : {
        "BACKEND" : "storages.backends.s3boto3.S3Boto3Storage",
        },
    "staticfiles" : {
        "BACKEND" : "storages.backends.s3boto3.S3Boto3Storage"
    }

}


# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/5.1/howto/static-files/

STATIC_URL = f'https://{AWS_S3_CUSTOM_DOMAIN}/static/'

Also, make sure you have storages in your INSTALLED_APPS to function well. In my case, I was logged in with terminal using aws configure (boto3) due to which I didn't need AWS key and credentials here. But if you are not, make sure to include AWS credentials. Additionally, add your IP address in your inbound rules of security group for EC2 that runs your Django application.

Sign up to request clarification or add additional context in comments.

2 Comments

thank you. i've updated my settings to more or less match yours, nothing changed.
I am sorry to hear that. I can see in your updated code, there is issue with your AWS URLs (if it is most recent one). In AWS_S3_CUSTOM_DOMAIN you are only using your bucket name. However, the URL must include your region name as well, and it should look like my-s3-bucket.s3.us-east-1.amazonaws.com as you can see in my code above. Please work around that and let me know if it helps.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.