Difficulty to store staticfiles on S3 using STORAGES setting in django >=4.2
for file storage on AWS S3, django 4.2 introduced the STORAGES dictionary setting, replacing the individual DEFAULT_FILE_STORAGE and STATICFILES_STORAGE setting variables as explained on the django-storages documentation. I am posting this question because I do not find good resources about this "new" STORAGES dictionary setting.
The documentation is light unfortunately about what the content of this STORAGES dictionary ought to be:
STORAGES = {
"default": {
"BACKEND": "storages.backends.s3.S3Storage",
"OPTIONS": {
...your_options_here
},
},
}
adding that to...
put static files on S3 via collectstatic on Django >= 4.2 you'd include the
staticfiles
key (at the same level asdefault
)
Referring to another source, I defined the following STORAGES dictionary in my settings.py:
DEFAULT_STORAGE_BACKEND = "storages.backends.s3.S3Storage"
DEFAULT_STORAGE_OPTIONS = {
"access_key": os.getenv("S3_ACCESS_KEY"),
"secret_key": os.getenv("S3_SECRET_KEY"),
"bucket_name": os.getenv("S3_BUCKET"),
"region_name": os.getenv("S3_REGION"),
}
STORAGES = {
"default": {
"BACKEND": DEFAULT_STORAGE_BACKEND,
"OPTIONS": DEFAULT_STORAGE_OPTIONS,
},
"staticfiles": {
"BACKEND": "storages.backends.s3.S3StaticStorage"
}
}
But when trying to collect my static files, I get the following error:
2024-11-06T04:01:52.930704485Z Collecting static files...
2024-11-06T04:01:57.469881851Z Traceback (most recent call last):
2024-11-06T04:01:57.470527987Z File "/usr/local/lib/python3.10/runpy.py", line 196, in _run_module_as_main
2024-11-06T04:01:57.474490708Z return _run_code(code, main_globals, None,
2024-11-06T04:01:57.475537977Z File "/usr/local/lib/python3.10/runpy.py", line 86, in _run_code
2024-11-06T04:01:57.475962333Z exec(code, run_globals)
2024-11-06T04:01:57.476460840Z File "/opt/project/src/manage.py", line 22, in <module>
2024-11-06T04:01:57.476786833Z main()
2024-11-06T04:01:57.476935378Z File "/opt/project/src/manage.py", line 18, in main
2024-11-06T04:01:57.477209035Z execute_from_command_line(sys.argv)
2024-11-06T04:01:57.477350462Z File "/root/.cache/pypoetry/virtualenvs/cooking-core-gHi8t1rX-py3.10/lib/python3.10/site-packages/django/core/management/__init__.py", line 442, in execute_from_command_line
2024-11-06T04:01:57.477690188Z utility.execute()
2024-11-06T04:01:57.477839648Z File "/root/.cache/pypoetry/virtualenvs/cooking-core-gHi8t1rX-py3.10/lib/python3.10/site-packages/django/core/management/__init__.py", line 436, in execute
2024-11-06T04:01:57.478160867Z self.fetch_command(subcommand).run_from_argv(self.argv)
2024-11-06T04:01:57.478305339Z File "/root/.cache/pypoetry/virtualenvs/cooking-core-gHi8t1rX-py3.10/lib/python3.10/site-packages/django/core/management/base.py", line 413, in run_from_argv
2024-11-06T04:01:57.478613220Z self.execute(*args, **cmd_options)
2024-11-06T04:01:57.479559221Z File "/root/.cache/pypoetry/virtualenvs/cooking-core-gHi8t1rX-py3.10/lib/python3.10/site-packages/django/core/management/base.py", line 459, in execute
2024-11-06T04:01:57.479948258Z output = self.handle(*args, **options)
2024-11-06T04:01:57.480146888Z File "/root/.cache/pypoetry/virtualenvs/cooking-core-gHi8t1rX-py3.10/lib/python3.10/site-packages/django/contrib/staticfiles/management/commands/collectstatic.py", line 209, in handle
2024-11-06T04:01:57.481017538Z collected = self.collect()
2024-11-06T04:01:57.481477067Z File "/root/.cache/pypoetry/virtualenvs/cooking-core-gHi8t1rX-py3.10/lib/python3.10/site-packages/django/contrib/staticfiles/management/commands/collectstatic.py", line 135, in collect
2024-11-06T04:01:57.481974899Z handler(path, prefixed_path, storage)
2024-11-06T04:01:57.482124185Z File "/root/.cache/pypoetry/virtualenvs/cooking-core-gHi8t1rX-py3.10/lib/python3.10/site-packages/django/contrib/staticfiles/management/commands/collectstatic.py", line 378, in copy_file
2024-11-06T04:01:57.483142887Z self.storage.save(prefixed_path, source_file)
2024-11-06T04:01:57.483306465Z File "/root/.cache/pypoetry/virtualenvs/cooking-core-gHi8t1rX-py3.10/lib/python3.10/site-packages/django/core/files/storage/base.py", line 49, in save
2024-11-06T04:01:57.484601885Z name = self._save(name, content)
2024-11-06T04:01:57.484841220Z File "/root/.cache/pypoetry/virtualenvs/cooking-core-gHi8t1rX-py3.10/lib/python3.10/site-packages/storages/backends/s3.py", line 558, in _save
2024-11-06T04:01:57.486763818Z obj = self.bucket.Object(name)
2024-11-06T04:01:57.487602843Z File "/root/.cache/pypoetry/virtualenvs/cooking-core-gHi8t1rX-py3.10/lib/python3.10/site-packages/storages/backends/s3.py", line 514, in bucket
2024-11-06T04:01:57.488243924Z self._bucket = self.connection.Bucket(self.bucket_name)
2024-11-06T04:01:57.488420489Z File "/root/.cache/pypoetry/virtualenvs/cooking-core-gHi8t1rX-py3.10/lib/python3.10/site-packages/boto3/resources/factory.py", line 528, in create_resource
2024-11-06T04:01:57.488809043Z return partial(
2024-11-06T04:01:57.488961679Z File "/root/.cache/pypoetry/virtualenvs/cooking-core-gHi8t1rX-py3.10/lib/python3.10/site-packages/boto3/resources/base.py", line 123, in __init__
2024-11-06T04:01:57.489248355Z raise ValueError(f'Required parameter {identifier} not set')
2024-11-06T04:01:57.489510399Z ValueError: Required parameter name not set
It is somehow similar to the error described here, but this page is a bug report and the bug seems to be solved.
> anyone knows what name
I am not setting?
python = "^3.10"
django = "^5.1.2"
django-storages = "^1.14.4"
boto3 = "^1.35.54"